How to turn off pc via windows API?

前端 未结 6 682
南旧
南旧 2021-01-03 11:03

I never programmed a winapi so i have a little problem here .

I need turn off my pc from my application .

I found this example link text the

相关标签:
6条回答
  • 2021-01-03 11:12
    // ==========================================================================
    // system shutdown
    // nSDType: 0 - Shutdown the system
    //          1 - Shutdown the system and turn off the power (if supported)
    //          2 - Shutdown the system and then restart the system
    void SystemShutdown(UINT nSDType)
    {
        HANDLE           hToken;
        TOKEN_PRIVILEGES tkp   ;
    
        ::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken);
        ::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
    
        tkp.PrivilegeCount          = 1                   ; // set 1 privilege
        tkp.Privileges[0].Attributes= SE_PRIVILEGE_ENABLED;
    
        // get the shutdown privilege for this process
        ::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    
        switch (nSDType)
        {
            case 0: ::ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE, 0); break;
            case 1: ::ExitWindowsEx(EWX_POWEROFF|EWX_FORCE, 0); break;
            case 2: ::ExitWindowsEx(EWX_REBOOT  |EWX_FORCE, 0); break;
        }
    }
    
    0 讨论(0)
  • 2021-01-03 11:13

    You could use ShellExecute() to call shutdown.exe

    0 讨论(0)
  • 2021-01-03 11:19

    Some working code for InitiateSystemShutdownEx:

    // Get the process token
    HANDLE hToken;
    OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
        &hToken);
    
    // Build a token privilege request object for shutdown
    TOKEN_PRIVILEGES tk;
    tk.PrivilegeCount = 1;
    tk.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    LookupPrivilegeValue(NULL, TEXT("SeShutdownPrivilege"), &tk.Privileges[0].Luid);
    
    // Adjust privileges
    AdjustTokenPrivileges(hToken, FALSE, &tk, 0, NULL, 0);
    
    // Go ahead and shut down
    InitiateSystemShutdownEx(NULL, NULL, 0, FALSE, FALSE, 0);
    

    So far as I can tell, the advantage to this over the ExitWindowsEx solution is that the calling process does not need to belong to the active user.

    0 讨论(0)
  • 2021-01-03 11:20
    #include<iostream>
    using namespace std;
    int main(){
    system("shutdown -s -f -t 0");
    }
    
    0 讨论(0)
  • 2021-01-03 11:32

    http://msdn.microsoft.com/en-us/library/aa376868(VS.85).aspx

    Try

    ExitWindowsEx(EWX_POWEROFF, 0);
    
    0 讨论(0)
  • 2021-01-03 11:36

    This is a bit much for the comments on Daniel's answer, so I'll put it here.

    It looks like your main issue at this point is that your process isn't running with the priveleges required to perform a system shutdown.

    The docs for ExitWindowsEx contain this line:

    To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information, see Running with Special Privileges.

    They also have some example code. In a pinch, you can just copy that.

    0 讨论(0)
提交回复
热议问题