Click to See Complete Forum and Search --> : Need Help On Shutting Down PC Through APIs


Airkiller1
January 22nd, 2006, 03:18 AM
Hello,Everyone:
I want to write a program to shut down my computer through Windows API,but met several questions.
Of course I know it's important to get the privilege for the current process before using the'ExitWindows' function .So I wrote some lines according to guides on Internet.
My question is Why 'OpenThreadToken' function can't be achieved?I doubt if the codes I wrote are correct.Would you offer some advice?Your answers are welcomed!!! (The attachment is resource file.)
thanks
Airkiller1

golanshahar
January 22nd, 2006, 04:14 AM
look at this code:

void ShutDown()
{
HANDLE hToken =0;
TOKEN_PRIVILEGES tkp ={0};

DWORD pid = ::GetCurrentProcessId();
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS,0,pid);

int ret = ::OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);

if( ret ==0 )
return;

::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0);

::ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
}


Cheers

Airkiller1
January 24th, 2006, 05:16 AM
Thank you.
I confuse the concept
'thread' with 'process'.
Here is another way to achieve it.