ログを出力するだけの簡単なサービスです。デバッグログは、DebugViewで確認することができます。 サービスの登録と削除はscコマンドで行います。サービスの開始、終了、一時停止、再開はコントロールパネルのサービスから行えます。
>sc create TestService binPath= c:\test\TestService.exe displayname= "TestService"
[SC] CreateService SUCCESS
>sc delete TestService
[SC] CreateService SUCCESS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | #include <stdio.h> #include <tchar.h> #include <locale.h> #include <iostream> #include <windows.h> /* デバッグログの出力 */ void DebugLog ( LPTSTR szFormat // フォーマット , ... // パラメータ ) { TCHAR waBuf[ 4096 ] = { 0 }; va_list args; // 可変引数の展開 va_start ( args, szFormat ); _vsntprintf_s( waBuf, _countof( waBuf ), szFormat, args ); va_end ( args ); // ログの出力 ::OutputDebugString( waBuf ); } // サービス名称 #define SERVICE_NAME L"TestService" // サービスハンドル SERVICE_STATUS_HANDLE hServiceStatus = NULL; // サービス停止中フラグ BOOL bServiceStop = FALSE; // 一時停止中フラグ BOOL bPause = FALSE; /* サービスハンドラ */ DWORD WINAPI ServiceHandlerProc ( DWORD dwControl // 制御コード , DWORD dwEventType // イベントのタイプ , LPVOID lpEventData // イベントのデータ , LPVOID lpContext // Context ) { SERVICE_STATUS tServiceStatus; // サービスステータス tServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; tServiceStatus.dwWin32ExitCode = NO_ERROR; tServiceStatus.dwServiceSpecificExitCode = 0; tServiceStatus.dwCheckPoint = 1; tServiceStatus.dwWaitHint = 3000; tServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; switch ( dwControl ) { case SERVICE_CONTROL_STOP: //---------------------------------- // サービスの停止 //---------------------------------- { DebugLog( L "SERVICE_CONTROL_STOP¥r¥n" ); /* サービスのステータス更新(停止保留中) */ tServiceStatus.dwCurrentState = SERVICE_STOP_PENDING; if ( 0 == ::SetServiceStatus ( hServiceStatus, &tServiceStatus ) ) { // エラー DebugLog( L "SetServiceStatus err = 0x%08x¥r¥n" , ::HRESULT_FROM_WIN32( ::GetLastError() ) ); break ; } DebugLog ( L "SERVICE_STOP_PENDING¥n" ); // SERVICE SPECIFIC STOPPING CODE HERE. bServiceStop = TRUE; // 停止待ち Sleep (3 * 1000); /* サービスのステータス更新(停止) */ tServiceStatus.dwCurrentState = SERVICE_STOPPED; tServiceStatus.dwCheckPoint = 0; tServiceStatus.dwWaitHint = 0; tServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; if ( 0 == ::SetServiceStatus( hServiceStatus, &tServiceStatus ) ) { // エラー DebugLog( L "SetServiceStatus err = 0x%08x¥r¥n" , ::HRESULT_FROM_WIN32( ::GetLastError() ) ); break ; } DebugLog ( L "SERVICE_STOPPED¥n" ); } break ; case SERVICE_CONTROL_PAUSE: //---------------------------------- // サービスの一時停止 //---------------------------------- { DebugLog (TEXT( "SERVICE_CONTROL_PAUSE¥n" )); /* サービスのステータス更新(一時停止保留中) */ tServiceStatus.dwCurrentState = SERVICE_PAUSE_PENDING; if ( 0 == SetServiceStatus( hServiceStatus, &tServiceStatus ) ) { // エラー DebugLog( L "SetServiceStatus err = 0x%08x¥r¥n" , ::HRESULT_FROM_WIN32( ::GetLastError() ) ); break ; } DebugLog ( L "SERVICE_PAUSE_PENDING¥n" ); // 一時停止中 bPause = TRUE; /* サービスのステータス更新(一時停止) */ tServiceStatus.dwCurrentState = SERVICE_PAUSED; tServiceStatus.dwCheckPoint = 0; tServiceStatus.dwWaitHint = 0; tServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE; if ( 0 == SetServiceStatus( hServiceStatus, &tServiceStatus ) ) { // エラー DebugLog( L "SetServiceStatus err = 0x%08x¥r¥n" , ::HRESULT_FROM_WIN32( ::GetLastError() ) ); break ; } DebugLog ( L "SERVICE_PAUSED¥n" ); } break ; case SERVICE_CONTROL_CONTINUE: //---------------------------------- // サービスの再開 //---------------------------------- { DebugLog ( L "SERVICE_CONTROL_CONTINUE¥n" ); /* サービスのステータス更新(再開保留中) */ tServiceStatus.dwCurrentState = SERVICE_START_PENDING; if ( 0 == ::SetServiceStatus( hServiceStatus, &tServiceStatus ) ) { // エラー DebugLog( L "SetServiceStatus err = 0x%08x¥r¥n" , ::HRESULT_FROM_WIN32( ::GetLastError() ) ); break ; } DebugLog ( L "SERVICE_START_PENDING¥n" ); // 実行中 bPause = FALSE; /* サービスのステータス更新(再開保留中) */ // Set RUNNING status. tServiceStatus.dwCurrentState = SERVICE_RUNNING; tServiceStatus.dwCheckPoint = 0; tServiceStatus.dwWaitHint = 0; tServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE; if ( 0 == ::SetServiceStatus( hServiceStatus, &tServiceStatus ) ) { // エラー DebugLog( L "SetServiceStatus err = 0x%08x¥r¥n" , ::HRESULT_FROM_WIN32( ::GetLastError() ) ); break ; } DebugLog ( L "SERVICE_RUNNING¥n" ); } break ; default : return ERROR_CALL_NOT_IMPLEMENTED; } return NO_ERROR; } /* サービスメイン */ VOID WINAPI ServiceMain ( DWORD dwArgc , PTSTR * pszArgv ) { DebugLog ( L "SERVICE(START)¥n" ); // サービスハンドラの登録 hServiceStatus = ::RegisterServiceCtrlHandlerEx( SERVICE_NAME // サービス名称 , ServiceHandlerProc // サービスハンドラ , NULL // Context ); if ( NULL == hServiceStatus ) { // エラー DebugLog( L "RegisterServiceCtrlHandlerEx err = 0x%08x¥r¥n" , ::HRESULT_FROM_WIN32( ::GetLastError() ) ); return ; } SERVICE_STATUS tServiceStatus; // サービス状態 tServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; tServiceStatus.dwWin32ExitCode = NO_ERROR; tServiceStatus.dwServiceSpecificExitCode = 0; /* サービスのステータス更新(開始保留中) */ tServiceStatus.dwCurrentState = SERVICE_START_PENDING; tServiceStatus.dwCheckPoint = 1; tServiceStatus.dwWaitHint = 1000; tServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; if ( 0 == ::SetServiceStatus ( hServiceStatus, &tServiceStatus ) ) { // エラー DebugLog( L "SetServiceStatus err = 0x%08x¥r¥n" , ::HRESULT_FROM_WIN32( ::GetLastError() ) ); return ; } DebugLog ( L "SERVICE_START_PENDING¥n" ); /* サービスのステータス更新(開始) */ tServiceStatus.dwCurrentState = SERVICE_RUNNING; tServiceStatus.dwCheckPoint = 0; tServiceStatus.dwWaitHint = 0; tServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE; if ( 0 == ::SetServiceStatus ( hServiceStatus, &tServiceStatus ) ) { // エラー DebugLog( L "SetServiceStatus err = 0x%08x¥r¥n" , ::HRESULT_FROM_WIN32( ::GetLastError() ) ); return ; } DebugLog ( L "SERVICE_RUNNING¥n" ); /* サービスメイン処理 */ while ( !bServiceStop ) { // 一時停止中じゃ無ければ実行 if ( !bPause ) { // RUNの最中だけ実行 ::OutputDebugString( L "SERVICE(RUN)" ); } ::Sleep( 1000 ); } DebugLog ( L "SERVICE(END)¥n" ); } /* サービス */ int _tmain ( int argc , _TCHAR* argv[] ) { /* サービステーブル */ static SERVICE_TABLE_ENTRY taServiceTable[] = { { SERVICE_NAME, ServiceMain } , { NULL, NULL } }; /* サービスプロセスのメインスレッドをサービス制御マネージャに接続し、 そのスレッドを呼び出し側プロセス用のサービス制御ディスパッチャス レッドにする。 */ if ( 0 == ::StartServiceCtrlDispatcher( taServiceTable ) ) { // エラー DebugLog( L "StartServiceCtrlDispatcher err = 0x%08x¥r¥n" , ::HRESULT_FROM_WIN32( ::GetLastError() ) ); return ( -1 ); } // 処理結果を返す return ( 0 ); } |
[5592] SERVICE(START) [5592] SERVICE_START_PENDING [5592] SERVICE_RUNNING [5592] SERVICE(RUN) [5592] SERVICE(RUN) [5592] SERVICE(RUN) [5592] SERVICE(RUN) [5592] SERVICE(RUN) [5592] SERVICE(RUN) [5592] SERVICE(RUN) [5592] SERVICE(RUN) [5592] SERVICE_CONTROL_STOP [5592] SERVICE_STOP_PENDING [5592] SERVICE(END) [5592] SERVICE_STOPPED [7600] SERVICE(START) [7600] SERVICE_START_PENDING [7600] SERVICE_RUNNING [7600] SERVICE(RUN) [7600] SERVICE(RUN) [7600] SERVICE(RUN) [7600] SERVICE(RUN) [7600] SERVICE(RUN) [7600] SERVICE_CONTROL_PAUSE [7600] SERVICE_PAUSE_PENDING [7600] SERVICE_PAUSED [7600] SERVICE_CONTROL_CONTINUE [7600] SERVICE_START_PENDING [7600] SERVICE_RUNNING [7600] SERVICE(RUN) [7600] SERVICE(RUN) [7600] SERVICE(RUN) [7600] SERVICE(RUN) [7600] SERVICE(RUN) [7600] SERVICE(RUN) [7600] SERVICE(RUN) [7600] SERVICE_CONTROL_STOP [7600] SERVICE_STOP_PENDING [7600] SERVICE(END)