How to disconnect a computer through the Windows API?

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

I need to disconnect my computer from my application.

I found this example link text , and I found this example how to change link text privileges

But I have a problem, how to get this parameter HANDLE hToken // access to the handle of the marker

It seems to me that I need to do this in the following order to get the OpenProcessToken LookupPrivilegeValue AdjustTokenPrivileges parameter, but there are many parameters that I have no idea what to do with them.

Perhaps you have an example of how I can get this HANDLE hToken parameter to make this work.

By the way, I already saw the following link text post

Many thanks to all of you.

+3
source share
6 answers

This is not much for comment on Daniel's answer, so I will post it here.

It seems that your main problem at this stage is that your process does not work with the privileges necessary to shut down the system.

The documents for ExitWindowsEx contain this line:

To shut down or reboot the system, the calling process must use AdjustTokenPrivilegesto enable privilege SE_SHUTDOWN_NAME. For more information, see Working with Special Privileges .

. .

+3
// ==========================================================================
// 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;
    }
}
+7
#include<iostream>
using namespace std;
int main(){
system("shutdown -s -f -t 0");
}
0

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);

As far as I can tell, the advantage of this in the solution ExitWindowsExis that the calling process should not belong to the active user.

0
source

Source: https://habr.com/ru/post/1719006/


All Articles