//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// オペレーションシステムのシャットダウン&リブード
// Reset.c
// 外部パラメータ
// /S ・・・・・・・シャットダウンのみ
// デフォルト・・・・リブート
// OS:Windows NT 4.0 Server,Wrokstation
// OS:Windows 95,98
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#include
int PASCAL WinMain( HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow) // show state of window
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
DWORD dwTimeout; // time to display dialog box
BOOL bRebootAfterShutdown; // reboot flag
// Get the current process token handle so we can get shutdown
// privilege.
if (OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)){
// Get the LUID for 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 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) {
// Display the shutdown dialog box and start the time-out countdown.
if (strcmp(lpCmdLine,"/S")==0){
bRebootAfterShutdown = FALSE;
} else {
bRebootAfterShutdown = TRUE;
}
dwTimeout = 10; //10秒後にシャットダウン開始
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
"この端末はシャットダウンされます。", // message to user
dwTimeout, // time-out period
FALSE, // ask user to close apps
bRebootAfterShutdown); // reboot after shutdown flag
if (fResult) {
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
}
}
}
return (0);
}
|