CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: winAPI

  1. #1
    Join Date
    Mar 2003
    Location
    Warsaw
    Posts
    2

    winAPI

    How to restart or close windows using winAPI

  2. #2
    Join Date
    Mar 2003
    Location
    Warsaw
    Posts
    2
    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);

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...
    Code:
    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()'...
    Code:
    // To shutdown
    ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
    
    // To reboot
    ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0);

  4. #4
    Join Date
    Apr 2003
    Posts
    2

    That worked perfect Andreas!!!!!!!!!!

    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

  5. #5
    Join Date
    Dec 2001
    Location
    Dallas, Tx, USA (originally fromIndia)
    Posts
    154
    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.

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...

  7. #7
    Join Date
    Apr 2003
    Posts
    2
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured