14 #define SERVICE_CONTROL_BEEP 0x000004B0 15 #define SERVICE_CONTROL_STOP_POL 0x000004B1 38 m_Status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
39 m_Status.dwCurrentState = SERVICE_STOPPED;
40 m_Status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
42 m_Status.dwServiceSpecificExitCode = 0;
50 DebugMsg(
"CNTService::~CNTService()" );
68 if ( _stricmp( argv[1],
"-v" ) == 0 )
72 printf(
"The service is %s installed\n",
IsInstalled() ?
"currently" :
"not" );
75 else if ( _stricmp( argv[1],
"-i" ) == 0 )
91 printf(
"%s failed to install. Error %u\n",
m_szServiceName, GetLastError() );
96 else if ( _stricmp( argv[1],
"-u" ) == 0 )
109 char szFilePath[MAX_PATH];
110 ::GetModuleFileName( NULL, szFilePath,
sizeof( szFilePath ) );
111 printf(
"%s removed. (You must delete the file (%s) yourself.)\n",
m_szServiceName,
116 printf(
"Could not remove %s. Error %u\n",
m_szServiceName, GetLastError() );
121 else if ( _stricmp( argv[1],
"-s" ) == 0 )
124 printf(
"Running as a service.\n" );
128 printf(
"Unable to run as a service.\n" );
145 BOOL bResult = FALSE;
148 SC_HANDLE hSCM = ::OpenSCManager( NULL,
150 SC_MANAGER_ALL_ACCESS );
154 SC_HANDLE hService = ::OpenService( hSCM,
m_szServiceName, SERVICE_QUERY_CONFIG );
158 ::CloseServiceHandle( hService );
161 ::CloseServiceHandle( hSCM );
170 SC_HANDLE hSCM = ::OpenSCManager( NULL,
172 SC_MANAGER_ALL_ACCESS );
177 char szFilePath[MAX_PATH];
178 ::GetModuleFileName( NULL, szFilePath,
sizeof( szFilePath ) );
179 strcat( szFilePath,
" -s" );
184 SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
185 SERVICE_DEMAND_START,
186 SERVICE_ERROR_NORMAL, szFilePath, NULL, NULL, NULL, NULL, NULL );
189 ::CloseServiceHandle( hSCM );
198 strcpy( szKey,
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\" );
200 if (::RegCreateKey( HKEY_LOCAL_MACHINE, szKey, &hKey ) != ERROR_SUCCESS )
202 ::CloseServiceHandle( hService );
203 ::CloseServiceHandle( hSCM );
208 ::RegSetValueEx( hKey,
"EventMessageFile", 0, REG_EXPAND_SZ, (CONST BYTE*)szFilePath,
209 static_cast<DWORD>( strlen( szFilePath ) + 1 ) );
212 DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
213 ::RegSetValueEx( hKey,
"TypesSupported", 0, REG_DWORD, (CONST BYTE*)&dwData,
sizeof(
DWORD ) );
214 ::RegCloseKey( hKey );
219 ::CloseServiceHandle( hService );
220 ::CloseServiceHandle( hSCM );
227 SC_HANDLE hSCM = ::OpenSCManager( NULL,
229 SC_MANAGER_ALL_ACCESS );
233 BOOL bResult = FALSE;
237 if (::DeleteService( hService ) )
246 ::CloseServiceHandle( hService );
249 ::CloseServiceHandle( hSCM );
266 for ( WORD i = 0; i < 3; i++ )
295 DebugMsg(
"Calling StartServiceCtrlDispatcher()" );
296 BOOL b = ::StartServiceCtrlDispatcher( st );
299 DWORD rc = GetLastError();
300 DebugMsg(
"StartServiceCtrlDispatcher returned false: %d", rc );
302 DebugMsg(
"Returned from StartServiceCtrlDispatcher()" );
312 pService->
DebugMsg(
"Entering CNTService::ServiceMain()" );
314 pService->
m_Status.dwCurrentState = SERVICE_START_PENDING;
328 pService->
m_Status.dwWin32ExitCode = 0;
329 pService->
m_Status.dwCheckPoint = 0;
337 pService->
DebugMsg(
"Leaving CNTService::ServiceMain()" );
355 DebugMsg(
"Entering CNTService::Initialize()" );
364 m_Status.dwWin32ExitCode = GetLastError();
377 DebugMsg(
"Leaving CNTService::Initialize()" );
388 DebugMsg(
"Entering CNTService::Run()" );
397 DebugMsg(
"Leaving CNTService::Run()" );
410 pService->
DebugMsg(
"CNTService::Handler(%lu)", dwOpcode );
413 case SERVICE_CONTROL_STOP:
414 pService->
SetStatus( SERVICE_STOP_PENDING );
420 case SERVICE_CONTROL_PAUSE:
424 case SERVICE_CONTROL_CONTINUE:
428 case SERVICE_CONTROL_INTERROGATE:
432 case SERVICE_CONTROL_SHUTDOWN:
453 pService->
m_Status.dwCurrentState );
473 DebugMsg(
"CNTService::OnInterrogate()" );
479 DebugMsg(
"CNTService::OnPause()" );
485 DebugMsg(
"CNTService::OnContinue()" );
491 DebugMsg(
"CNTService::OnShutdown()" );
505 pService->
SetStatus( SERVICE_STOP_PENDING );
511 DebugMsg(
"CNTService::OnUserControl(%8.8lXH)", dwOpcode );
518 DebugMsg(
"CNTService::OnUserControl(%8.8lXH)", dwOpcode );
531 va_start( arglist, pszFormat );
532 vsprintf( &buf[strlen( buf )], pszFormat, arglist );
535 OutputDebugString( buf );
virtual void OnInterrogate()
BOOL ParseStandardArgs(int argc, char *argv[])
typedef DWORD(WINAPI *__SymGetOptions)(VOID)
void SetStatus(DWORD dwState)
virtual void OnContinue()
void DebugMsg(const char *pszFormat,...)
typedef BOOL(WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess
SERVICE_STATUS_HANDLE m_hServiceStatus
void LogEvent(WORD wType, DWORD dwID, const char *pszS1=NULL, const char *pszS2=NULL, const char *pszS3=NULL)
static CNTService * m_pThis
CNTService(const char *szServiceName)
static void WINAPI Handler(DWORD dwOpcode)
#define SERVICE_CONTROL_USER
static void WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
virtual BOOL OnUserControl(DWORD dwOpcode)
#define SERVICE_CONTROL_BEEP
virtual void OnShutdown()
#define EVMSG_CTRLHANDLERNOTINSTALLED
#define SERVICE_CONTROL_STOP_POL