指定のWindowsサービスを停止する。
環境:QT5.5
インクルードファイル
#include
#include
コード
SC_HANDLE OpenSCManager;
SC_HANDLE schService;
SERVICE_STATUS_PROCESS ssp;
DWORD dwBytesNeeded;
DWORD dwRet;
QString strService = "testService"; // 停止するサービス名
LPCTSTR lpSrvName = reinterpret_cast(strService.utf16());
//*** ローカルコンピュータ上のサービス制御マネージャに接続
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if ( NULL == schSCManager ) {
qDebug().noquote() << "Error:" << GetLastError() << endl;
return false;
}
//*** サービスに接続
schService = OpenService(schSCManager, lpSrvName, SERVICE_ALL_ACCESS);
if ( NULL == schService )
{
CloseServiceHandle(schSCManager);
qDebug().noquote() << "Error:" << GetLastError() << endl;
return false;
}
//*** サービスの状態を確認
if ( !QueryServiceStatusEx(schService,
SC_STATUS_PROCESS_INFO,
(LPBYTE)&ssp,
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded) ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
qDebug().noquote() << "Error:" << GetLastError() << endl;
return false;
}
// サービスが停止している場合は処理終了
if ( SERVICE_STOPPED == ssp.dwCurrentState ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
qInfo().noquote() << "Service is stopped." << endl;
return true;
}
// サービスが終了待ちの場合
DWORD dwStartTime = GetTickCount();
DWORD dwTimeout = 30000;
while ( SERVICE_STOP_PENDING == ssp.dwCurrentState ) {
qInfo().noquote() << "Service stop pending." << endl;
Sleep( ssp.dwWaitHint/10 );
// サービスの状態を確認する
if ( !QueryServiceStatusEx(schService,
SC_STATUS_PROCESS_INFO,
(LPBYTE)&ssp,
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded) ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
qDebug().noquote() << "Error:" << GetLastError() << endl;
return false;
}
// サービス停止(正常終了
if ( SERVICE_STOPPED == ssp.dwCurrentState ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
qInfo().noquote() << "Service is Stopped." << endl;
writeLog(StrMsg, LOG_INFO);
return true;
}
// タイムアウト
if ( GetTickCount() - dwStartTime > dwTimeout ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
qInfo().noquote() << "Service stop timed out." << endl;
return false;
}
}
//*** サービス停止処理
if ( !ControlService(schService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp) ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
qDebug().noquote() << "Error:" << GetLastError() << endl;
return false;
}
// サービスが停止するまで待つ
while ( SERVICE_STOPPED != ssp.dwCurrentState ) {
Sleep( ssp.dwWaitHint );
// サービスの状態確認
if ( !QueryServiceStatusEx(schService,
SC_STATUS_PROCESS_INFO,
(LPBYTE)&ssp,
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded) ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
qDebug().noquote() << "Error:" << GetLastError() << endl;
return false;
}
// サービス停止(正常終了
if ( SERVICE_STOPPED == ssp.dwCurrentState ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
qInfo().noquote() << "Service stopped successfully." << endl;
break;
}
// タイムアウト
if ( GetTickCount() - dwStartTime > dwTimeout ) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
qInfo().noquote() << "Service stop timed out." << endl;
return false;
}
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return true;
コメント