|
Post by erikdewaal on Jun 21, 2023 4:05:08 GMT -5
'possible shutdown flags '_EWX_LOGOFF logs user off, leaves computer on '_EWX_POWEROFF turns off power '_EWX_REBOOT exits windows, then reboots '_EWX_SHUTDOWN exits windows, leaves power on
flag = _EWX_POWEROFF calldll #user32, "ExitWindowsEx",_ flag as ulong,_ 'type of shutdown 0 as ulong,_ 'must be zero re as boolean 'success=nonzero
end Ok. So i have found this on the internet to shot down mij pc within a program. But it does not work. I am a novice when it comes to programming a api and don't understand much off it. Can someone help me with this and axplain wat i am doing wrong? Because it doesn work.
|
|
|
Post by Rod on Jun 21, 2023 7:18:20 GMT -5
Are you really trying to power off or are you just wanting to put the pc to sleep? Powering off seems hazardous and while the API code is correctly coded it does not work on my PC either. I suspect you need to get windows permission right first. My PC quitely sleeps then powers off all by itself. Windows power manager may be what you need to look at. Also Scheduler?
|
|
|
Post by xxgeek on Jun 21, 2023 8:07:33 GMT -5
Not an answer to your question, but this will shutdown your PC.
run "shutdown -s -t 0 -y"
No permission needed, and No Mercy! -s ------ 'shuts down -t 0 ------ '0 wait time -y ------ 'auto answers "yes" to all shutdown queries
I suggest you look into command line switches for shutdown.exe, and choose the switches that do exactly what you want. Shutdown.exe is built into windows.
|
|
|
Post by erikdewaal on Jun 21, 2023 12:14:06 GMT -5
Thanks for the reply.
Indeed, I want to completely shut down the PC in a program I am writing. It is intended for a number of users who are less able to handle a PC and can switch off the PC more easily with a clear, large power button than in the standard way. I don't have to worry about data loss or anything like that, so that's not the problem.
xxgeek's answer works perfectly for this. Thanks for that.
And I now also understand why my piece of code is not working Rod, thanks for that, think it is indeed about permission.
|
|
|
Post by Rod on Jun 21, 2023 14:30:05 GMT -5
You might have to worry about data loss because Windows has hundreds of tasks running in the background. Pulling the plug risks corrupting data, files, updates and many more background tasks that you can’t see running.
Far far safer to let power manager shut down safely in It’s own time. You don’t need to do anything, just walk away from the machine.
|
|
|
Post by erikdewaal on Jun 22, 2023 3:13:36 GMT -5
No problem about that Rod. The pc is not connected to the internet. So no updates. The only program that is running is the software that i am creating en there will be no data lost when that is shut down either. There are no other programs other then windows itself that is running on the pc. So i am confident that it will work.
|
|
|
Post by sarmednafi on Sept 14, 2023 2:23:39 GMT -5
Thank you Erikdewaal for starting this.
Thanks to all who response for that.
Regards
|
|
|
Post by Chris Iverson on Sept 14, 2023 16:28:40 GMT -5
You might have to worry about data loss because Windows has hundreds of tasks running in the background. Pulling the plug risks corrupting data, files, updates and many more background tasks that you can’t see running. Far far safer to let power manager shut down safely in It’s own time. You don’t need to do anything, just walk away from the machine. Calling ExitWindowsEx with _EWX_POWEROFF is NOT just "pulling the plug". It goes through the Windows shutdown process, and then turns off the machine. Since Windows XP, it's identical to _EWX_SHUTDOWN - it does a full, proper shutdown of the system, and then turns it off. It's the same as choosing "shut down" from the Start menu. Even before Windows XP, it would still do a proper shutdown of Windows. The reason for the difference between the two is historical, and dates to back when computers used to NOT be able to turn off by themselves. EWX_SHUTDOWN would shut down Windows, but not turn off the power of the machine. That had to be done by the user. EWX_POWEROFF would do the same thing, but it would power off the machine if the machine was capable of doing so. EWX_SHUTDOWN was changed in Windows XP to turn off the machine's power if the machine supported doing so, making it identical in behavior to POWEROFF on those systems. And yes, to be able to perform a shutdown or reboot using the ExitWindowsEx API, you need to adjust the privileges on your running program to do so. This is to make shutdowns a deliberate choice. xxgeek's response is the simplest to use, and it does result in shutting down the machine. It's what I would recommend using. That said, if you want to see how an LB program could use that API, this is a working example that adjusts the privileges of LB, and then calls the shutdown API to shut down the computer. 'possible shutdown flags '_EWX_LOGOFF logs user off, leaves computer on '_EWX_POWEROFF turns off power '_EWX_REBOOT exits windows, then reboots '_EWX_SHUTDOWN exits windows, leaves power on
a = PerformShutdown(_EWX_POWEROFF) print "System now shutting down."
Function PerformShutdown(shutdownType) access = _TOKEN_ADJUST_PRIVILEGES or _TOKEN_QUERY hToken = 0 a = OpenCurrentProcessToken(access, hToken)
If a = 0 then goto [endFunction]
SE.SHUTDOWN.NAME$ = "SeShutdownPrivilege" struct LUID,_ LowPart as ulong,_ HighPart as long
a = LookupLocalPrivilegeValue(SE.SHUTDOWN.NAME$)
struct TokenPrivileges,_ PrivilegeCount as ulong,_ LUIDLowPart as ulong,_ LUIDHighPart as long,_ LUIDAttributes as ulong
TokenPrivileges.PrivilegeCount.struct = 1 TokenPrivileges.LUIDLowPart.struct = LUID.LowPart.struct TokenPrivileges.LUIDHighPart.struct = LUID.HighPart.struct TokenPrivileges.LUIDAttributes.struct = _SE_PRIVILEGE_ENABLED
result = -1 a = AdjustTokenPrivileges(hToken, 0, result) b = CloseHandle(hToken) if a = 0 then goto [endFunction] if result <> 0 then goto [endFunction]
PerformShutdown = ExitWindowsEx(shutdownType) [endFunction] End Function
Function ExitWindowsEx(flags) 'possible shutdown flags '_EWX_LOGOFF logs user off, leaves computer on '_EWX_POWEROFF turns off power '_EWX_REBOOT exits windows, then reboots '_EWX_SHUTDOWN exits windows, leaves power on
reason = 0 calldll #user32, "ExitWindowsEx",_ flags as ulong,_ 'type of shutdown reason as ulong,_ 'must be zero ExitWindowsEx as long 'success=nonzero End Function
Function AdjustTokenPrivileges(hToken, disableAll, byref result) open "advapi32" for DLL as #advapi32
CallDLL #advapi32, "AdjustTokenPrivileges",_ hToken as ulong,_ disableAll as long,_ TokenPrivileges as struct,_ 0 as long, 0 as long, 0 as long,_ AdjustTokenPrivileges as long
CallDLL #kernel32, "GetLastError",_ ret as long
result = ret
close #advapi32 End Function
Function LookupLocalPrivilegeValue(valueName$) open "advapi32" for DLL as #advapi32
systemName = 0 '0 or null means local system CallDLL #advapi32, "LookupPrivilegeValueA",_ systemName as ulong,_ valueName$ as ptr,_ LUID as struct,_ LookupLocalPrivilegeValueA as long
close #advapi32 End Function
Function OpenCurrentProcessToken(desiredAccess, byref hToken) open "advapi32" for DLL as #advapi32 struct a, tempToken as ulong
hProcess = 0 CallDLL #kernel32, "GetCurrentProcess", hProcess as ulong
CallDLL #advapi32, "OpenProcessToken",_ hProcess as ulong,_ desiredAccess as long,_ a as struct,_ OpenCurrentProcessToken as long
if OpenCurrentProcessToken <> 0 then hToken = a.tempToken.struct end if
close #advapi32 End Function
Function CloseHandle(hHandle) CallDLL #kernel32, "CloseHandle",_ hHandle as ulong,_ CloseHandle as long End Function
|
|
|
Post by DARKNIGHT on Sept 14, 2023 23:38:07 GMT -5
just a suggestion,
a cool addition to the code, would be to see if you can still find a wave file of the digitized woman's voice saying " System is now shutting down" and include the Playwave command in the code to play with or without the on screen text. to add a futuristic, year 2060 tech. effect. I had that wave years ago, and used it as my shutdown sound on a computer I had back then.
|
|
|
Post by metro on Sept 15, 2023 3:40:24 GMT -5
just a suggestion,
a cool addition to the code, would be to see if you can still find a wave file of the digitized woman's voice saying " System is now shutting down" and include the Playwave command in the code to play with or without the on screen text. to add a futuristic, year 2060 tech. effect. I had that wave years ago, and used it as my shutdown sound on a computer I had back then.
www.wavsource.com/people/women1.htm
|
|
|
Post by meerkat on Sept 15, 2023 4:51:48 GMT -5
I have not tested this because I didn't want to shut down my pc to test it. You could print this html and have it shut down your PC. Problem is because LB does not have direct access to html, you have to run file. You need to change the code to select the voice you like. So here it is if anyone wants to test it.
<html> <head> <title>Text to Speech and shutdown </title> </head> <body> <button onclick='speakMessage()'>Shut Down</button> <script> function speakMessage() { const message = 'This is your message.'; const speechSynthesis = window.speechSynthesis; const utterance = new SpeechSynthesisUtterance(message); // Select a voice (optional) const voices = speechSynthesis.getVoices(); utterance.voice = voices.find(voice => voice.name === 'Your Selected Voice');
speechSynthesis.speak(utterance); } function shutdownPC() { const { exec } = require('child_process'); exec('shutdown /s /t 0', (error, stdout, stderr) => { if (error) { console.error(`Error: ${error.message}`); return; } console.log(`stdout: ${stdout}`); console.error(`stderr: ${stderr}`); }); } </script> </body> </html>
|
|