Windowsのリブートと終了



Windowsをプログラムからシャットダウンしたり、
リブートさせたりするときに使用します。

Windows NT, Windows 2000

Windows 95, 98, Me




Windows NT, Windows 2000

Windows NT および Windows 2000の場合は、Windowsを
終了またはリブートさせようとするプロセスの権限を
操作してあげる必要があります。

int sub_systm_shudown() 
{
	HANDLE			hdl ;
	HANDLE			phl ;
	LUID			pul ;
	TOKEN_PRIVILEGES	tkp ;
	TOKEN_PRIVILEGES	dkp ;
	DWORD			lkp ;

	// ***
	// ***	Load Privilege Level
	// ***

	hdl = GetCurrentProcess() ;

    	OpenProcessToken( hdl, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &phl ) ;

	LookupPrivilegeValue( NULL, "SeShutdownPrivilege", &pul ) ;

	// ***
	// ***	Change Level
	// ***

	tkp.PrivilegeCount		= 1 ;
	tkp.Privileges[0].Luid		= pul ;
	tkp.Privileges[0].Attributes	= SE_PRIVILEGE_ENABLED ;

	lkp = sizeof(dkp) ;

	AdjustTokenPrivileges( phl, FALSE, &tkp, sizeof(tkp), &dkp, &lkp ) ;

	// ***
	// ***	Reboot Windows
	// ***

	ExitWindowsEx ( EWX_REBOOT | EWX_FORCE, 0xFFFF ) ;

	return TRUE ;
}



ExitWindowsEx関数の第一パラメータには、以下のパラメータを指定できます。

EWX_REBOOTWindowsをリブート
EWX_LOGOFFカレントユーザーをログオフ
EWX_SHUTDOWNWindowsを終了
EWX_POWEROFFWindowsを終了後電源OFF



Windows 95, 98, Me

Windows 95,98,Meの場合は、上記のサンプルのExitWindowsEx API
だけを呼び出せばOKです。


戻る