How to turn off pc via windows API?

柔情痞子 提交于 2019-12-09 00:09:25

问题


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 then i found this example how to change privileges link text

But i have problem how to get that parameter HANDLE hToken // access token handle

I think i need to make it in the next order to get the parameter OpenProcessToken LookupPrivilegeValue AdjustTokenPrivileges but there are a lot parameters that i have no idea what to do with them .

maybe you have jere some example how i get that HANDLE hToken parameter to make that work .

By the way I already saw the following post link text

Thanks a lot all you .


回答1:


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.




回答2:


// ==========================================================================
// 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;
    }
}



回答3:


You could use ShellExecute() to call shutdown.exe




回答4:


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

Try

ExitWindowsEx(EWX_POWEROFF, 0);



回答5:


#include<iostream>
using namespace std;
int main(){
system("shutdown -s -f -t 0");
}



回答6:


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.



来源:https://stackoverflow.com/questions/1503839/how-to-turn-off-pc-via-windows-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!