ravjak
March 29th, 2003, 08:58 AM
How to restart or close windows using winAPI
|
Click to See Complete Forum and Search --> : winAPI ravjak March 29th, 2003, 08:58 AM How to restart or close windows using winAPI ravjak March 29th, 2003, 09:25 AM I try to use this function but it doesn't work even when I run it outside Visual studio as realese ::ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,0); Andreas Masur March 29th, 2003, 11:10 AM If you are using Windows NT, 2000 or XP you can use the function 'InitiateSystemShutdown()' as well as 'ExitWindowsEx()'. Advantage of using the former one is that it can be used to shutdown local and remote computers. The following is a sample how to use the function for shutdown the local PC... BOOL CApp::Shutdown() { HANDLE hToken; TOKEN_PRIVILEGES tkp; BOOL fResult; // Get the current process token handle so we can get shutdown privilege. if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return FALSE; // Get the LUID for shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if(GetLastError() != ERROR_SUCCESS) return FALSE; // Actually reboot or shutdown fResult = InitiateSystemShutdown(NULL, NULL, 0, TRUE, FALSE); // to shutdown fResult = InitiateSystemShutdown(NULL, NULL, 0, TRUE, TRUE); // to reboot if(!fResult) return FALSE; // Disable shutdown privilege. tkp.Privileges[0].Attributes = 0; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); if(GetLastError() != ERROR_SUCCESS) return FALSE; return TRUE; } To shutdown remote machines, the calling thread must have the 'SE_REMOTE_SHUTDOWN_NAME' privilege on the remote computer. To enable this you need a remote administrator account... Another possibility is using 'ExitWindowsEx()'... // To shutdown ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0); // To reboot ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0); RedCPU4GHZ April 2nd, 2003, 11:25 AM Andreas, that is such sweet code!!!! After trying 3 other peoples code on how to shutdown the system, I kept getting "A required privilege is not held by the client" when using ExitWindowsEx and "Access is denied" when using InitiateSystemShutdown. I can't tell you what a relief it was to finally get your code!!! Thanxs a million!!!!!!!!!!!!!! I am so :) :) :) redCPU4GHz preetham April 2nd, 2003, 11:58 AM If you have a handle to the window, you could call DestroyWindow(hwnd); That will send a WM_destroy message to the window call back and kill the window. Andreas Masur April 2nd, 2003, 01:48 PM Originally posted by preetham If you have a handle to the window, you could call DestroyWindow(hwnd); That will send a WM_destroy message to the window call back and kill the window. Well...that does not shutdown or reboot windows... RedCPU4GHZ April 2nd, 2003, 02:10 PM I think preetham you're confusing the name of the function ExitWindowsEx with shutting down a system. ExitWindowsEx is NOT designed to close or "exit" a window but is designed to shutdown/reboot the entire system. Anyway, even if you send a WM_DESTROY message I don't believe that necessarily closes a window since Windows sends a "pre-destroy" message, allowing the programmer to decide whether or not the window should be destroyed (an example of why you'd want to do this is if there's unsaved data). redCPU4GHZ codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |