Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shutdown...?

Started by sathenzar Mar 6, 2004 at 10:19 AM 10 replies 750+ views
Original Post
sathenzar
sathenzar
Hey, does anyone know the command to shutpdwn your computer with VC++ 6.0? Thanks
ver_T_ex
ver_T_ex
This question was already posed some times ago: I just say: ExitWindowsEx().... Look it up in the MSDN , it''S quite easy!
Xiachunyi
Xiachunyi
Darn beat me to it!

The Win32 API function: " ExitWindowsEx", although be sure your application has the privilege before invoking it.

[edited by - Xiachunyi on March 6, 2004 11:33:51 AM]
sathenzar
sathenzar
Nothing happens here''s my code:
//---------------------------------------------------------------------------

#include <iostream.h>
#include <windows.h>
#include <windowsx.h>

int main(void)
{
ExitWindowsEx(EWX_SHUTDOWN, NULL);
return(0);
}
Lantz
Lantz
To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege.

[edited by - Lantz on March 6, 2004 12:06:24 PM]
PlayGGY
PlayGGY
I would doubt a console application could call it, anyways.
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
Lantz
Lantz
quote:
Original post by PlayGGY
I would doubt a console application could call it, anyways.

Actually that shouldn''t make any difference, windows does not differ between console apps and regular ones.
sathenzar
sathenzar
How would this function work? I''m looking and I don''t knwo how to set it up
Lantz
Lantz
You could try something like this (i havent tried it):

HANDLE token;
OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token );

TOKEN_PRIVILEGES p;
p.PrivilegeCount = 1;
p.Privileges[0].Luid = 0;
p.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( token, false, &p, 0, NULL, 0 );

CloseHandle( token );
sathenzar
sathenzar
Nothing happpens again. Thsi is th ecode I have so far:

HANDLE token;
OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token );
TOKEN_PRIVILEGES p;
p.PrivilegeCount = 1;
p.Privileges[0].Luid;
p.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( token, false, &p, 0, NULL, 0 );
CloseHandle( token );
ExitWindowsEx(EWX_POWEROFF, NULL);
Lantz
Lantz
Oops, forgot the actual privilege we want to enable.
Try this:

HANDLE token;
OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token );

LUID luid;
LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME, &luid );

TOKEN_PRIVILEGES p;
p.PrivilegeCount = 1;
p.Privileges[0].Luid = luid;
p.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( token, false, &p, 0, NULL, 0 );
CloseHandle( token );
ExitWindowsEx(EWX_POWEROFF, NULL);


And if it doesn''t work check the return values.
lc_gta
lc_gta
you''re still not setting the proper privileges. The following code from msdn should get you where you want to be.


HANDLE hToken;
TOKEN_PRIVILEGES tkp;

// Get a token for this process.


if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
error("OpenProcessToken");

// Get the LUID for the shutdown privilege.


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

tkp.PrivilegeCount = 1; // one privilege to set

tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get the shutdown privilege for this process.


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

// Cannot test the return value of AdjustTokenPrivileges.


if (GetLastError() != ERROR_SUCCESS)
error("AdjustTokenPrivileges");

// Shut down the system and force all applications to close.


if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
error("ExitWindowsEx");

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.