#include <windows.h>
void StartScheduler( void )
{
SC_HANDLE hSC = NULL;
SC_HANDLE hSchSvc = NULL;
// サービスマネージャへの接続
hSC = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if ( hSC == NULL ) {
return;
}
// サービスの開始とステータスの取得を前提とした
// サービスハンドルの取得
hSchSvc = OpenService(
hSC,
"Schedule",
SERVICE_START | SERVICE_QUERY_STATUS
);
// サービスマネージャの切断
CloseServiceHandle( hSC );
if ( hSchSvc == NULL ) {
return;
}
SERVICE_STATUS SvcStatus;
// ステータスの取得
if ( QueryServiceStatus( hSchSvc, &SvcStatus ) == FALSE ) {
CloseServiceHandle( hSchSvc );
return;
}
// 実行中かどうかの確認
if ( SvcStatus.dwCurrentState == SERVICE_RUNNING ) {
CloseServiceHandle( hSchSvc );
return;
}
// サービスの開始
if ( StartService( hSchSvc, 0, NULL ) == FALSE ) {
CloseServiceHandle( hSchSvc );
return;
}
// サービスハンドルのクローズ
CloseServiceHandle( hSchSvc );
}