views:

306

answers:

4

Hello all

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 .

+3  A: 

You could use ShellExecute() to call shutdown.exe

Jacob
+1 - seems so much simpler.
selbie
+4  A: 

http://msdn.microsoft.com/en-us/library/aa376868%28VS.85%29.aspx

Try

ExitWindowsEx(EWX_POWEROFF, 0);
Daniel Mošmondor
you told me exactly what i wrote above .And i got an error 0x522 -A required privilege is not held by the client. Exactly the problem that i had at the first place and my question about that parameter .thanks for help
Night Walker
I like this answer, but according to the docs you shouldn't be using 0 for that second parameter. Among other evils, it may report the shutdown as "unplanned" on reboot, and thus slow things down. The list of possible values is available at http://msdn.microsoft.com/en-us/library/aa376885(VS.85).aspx . I'd suggest SHTDN_REASON_MAJOR_APPLICATION.
T.E.D.
+2  A: 

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.

T.E.D.
I liked your example code even made it work under gcc .Any idea what is the diff between this code and the solution that Jacob recommended down the page ?thanks
Night Walker
Heh. Thanks, but for the record it is Microsoft's code, not mine.
T.E.D.
I wouldn't use Jacob's method unless there was no other good way. It might also work, but it creates a whole separate process, then loads a command interprer into it, then goes out and does directory searches to find the shutdown.exe executable, and then starts a third process to load and run that in. The extra work might not be that noticeable when you are shutting down anyway, but it's the principle of the matter...
T.E.D.
+1  A: 
// ==========================================================================
// 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;
    }
}
Lior Kogan