How to shutdown Windows. If you want to shut down Windows for any reason by code (i.e. you are creating an installer). To do so you will have to use the ExitWindowsEx() function from Windows API. The following parameters are allowed: EWX_FORCE - Shuts down Windows without waiting for applications that need user input (i.e. "Save file?"). This command will shut down the system pretty much no matter what ;-> EWX_LOGOFF - Logs the current user off. EWX_POWEROFF - Shuts the system down and turns off the power. The power will not be shutdown on older (486, early Pentium) systems due to the lack of a ATX motherboard. In this case it will just shut the system down with the "It is now safe to turn the computer off" message. EWX_REBOOT - Shuts down Windows and reboots. May be useful when installing shared DLL files. EWX_SHUTDOWN - Shuts down the system when all applications have closed. If an application is waiting for user input the system will wait until it is taken care of. It iwll not shutdown anything by force. Check out EWX_FORCE to shut down the system regardless of apps waiting for input.
Example: ExitWindowsEx(EWX_REBOOT,0); // Reboot the system
function DelphiExitWindows( Flags : Word):Boolean; var iVersionInfo: TOSVersionInfo; iToken : THandle; iPriveleg : TTokenPrivileges; iaresult : Integer; begin
Result:=FALSE; FillChar (iPriveleg, SizeOf (iPriveleg), #0); iVersionInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo); GetVersionEx(iVersionInfo); if iVersionInfo.dwPlatformId <> VER_PLATFORM_WIN32_NT then Result:=ExitWindowsEx (Flags, 0) else if OpenProcessToken (GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, iToken) then if LookupPrivilegeValue (NIL,'SeShutdownPrivilege', iPriveleg.Privileges[0].Luid) then begin iPriveleg.PrivilegeCount := 1; iPriveleg.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED; if AdjustTokenPrivileges (iToken,False,iPriveleg, Sizeof(iPriveleg),iPriveleg,iaresult) then Result:=ExitWindowsEx (Flags, 0); end; end;
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.