Question

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 .

Was it helpful?

Solution

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.

OTHER TIPS

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

You could use ShellExecute() to call shutdown.exe

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top