• Advertisement

Archived

This topic is now archived and is closed to further replies.

Shutdown...?

This topic is 5100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, does anyone know the command to shutpdwn your computer with VC++ 6.0? Thanks

Share this post


Link to post
Share on other sites
Advertisement
This question was already posed some times ago: I just say: ExitWindowsEx().... Look it up in the MSDN , it''S quite easy!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Nothing happens here''s my code:
//---------------------------------------------------------------------------

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

int main(void)
{
ExitWindowsEx(EWX_SHUTDOWN, NULL);
return(0);
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
How would this function work? I''m looking and I don''t knwo how to set it up

Share this post


Link to post
Share on other sites
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 );

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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");

Share this post


Link to post
Share on other sites

  • Advertisement