How to turn off pc via windows API?

前端 未结 6 869
南旧
南旧 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: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.

提交回复
热议问题