Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

windows shutdown

Status
Not open for further replies.

tougo

Technical User
Sep 9, 2002
27
GB
i used these fuctions so as to enable log off /reboot and shutdown through my program but it seems that only log off is fuctioning. why is that? even if i force a shutdown it still just logs off... (windows 2000)..if it has to do with the windows authoring and permissions how can i bypass them?

void __fastcall TForm1::Logoff1Click(TObject *Sender)
{
ExitWindowsEx(EWX_LOGOFF, 0);
}
//----------------------------------------------------------

void __fastcall TForm1::Reboot1Click(TObject *Sender)
{
ExitWindowsEx(EWX_REBOOT, 0);
}
//----------------------------------------------------------

void __fastcall TForm1::Shutdown1Click(TObject *Sender)
{
ExitWindowsEx(EWX_POWEROFF, 0);
}
//-------------
 
You have probably figured this out by now, but if you haven't here what I had to do to shutdown. You need to set a "Token Privilege" then call the shutdown. In this example I force all the open apps to close. If you don't want to force them use EWX_SHUTDOWN. If your system supports the power-off feature, the power is also turned off.

HANDLE hToken;
TOKEN_PRIVILEGES tkp;

// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
ShowMessage("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)
ShowMessage("AdjustTokenPrivileges");

// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
ShowMessage("ExitWindowsEx");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top