Programmers Reference - CDevice (1)
class CNTService : public CObject
#include "ConSys.h"
class CONLIB_EXT CNTService : public CObject { public: CNTService(const char* szServiceName); // Initializes a interface object to the service. // The szService name is the name used to identify the servce in the // service control manager, ex. 'ConSys Kernel'. virtual ~CNTService(); BOOL ParseStandardArgs(int argc, char* argv[]); BOOL IsInstalled(CString machine = NULL); // Returns TRUE if the service is installed on machine. // If the machine is turned off, the method returns FALSE. BOOL Install(DWORD startMode = SERVICE_DEMAND_START, DWORD serviceType = SERVICE_WIN32, CString machine = NULL, CString filePath = NULL); // This method registers the service on machine. For use of startMode // and service type please refer to the SDK. If the file path on the // target machine is different than on this machine a filepath is // nessasery BOOL Uninstall(CString machine = NULL); // Returns TRUE is the removal of the service succeeded. If the service // is running, the service is stopped before removal. BOOL StartService(DWORD dwNumServiceArgs = 0, LPCTSTR *lpServiceArgVectors = NULL, CString machine = NULL); // Starts the service on machine. The service must be installed. void LogEvent(WORD wType, DWORD dwID, const char* pszS1 = NULL, const char* pszS2 = NULL, const char* pszS3 = NULL); // Logs a event in the event log. BOOL StartServiceDispatcher(); // Starts the service dispatcher. void SetStatus(DWORD dwState); // Set the execution status of the service BOOL Initialize(); virtual void Run(); // This method is called by the main thread. Use this method // to do the real work, and return when the service closes. virtual BOOL OnInit(); // Overwrite this method to implement initialization of the service. // Called in the context of the main thread. virtual void OnStop(); // This method is called when the service recived a SERVICE_CONTROL_STOP // control message. virtual void OnInterrogate(); // This method is called when the service recived a SERVICE_CONTROL_INTERROGATE // control message. virtual void OnPause(); // This method is called when the service recived a SERVICE_CONTROL_PAUSE // control message. virtual void OnContinue(); // This method is called when the service recived a SERVICE_CONTROL_CONTINUE // control message. virtual void OnShutdown(); // This method is called when the service recived a SERVICE_CONTROL_SHUTDOWN // control message. virtual BOOL OnUserControl(DWORD dwOpcode); // This method is called when the service recived a message larger than // SERVICE_CONTROL_USER. The actual message s passed in dwOpcode // static member functions static void WINAPI ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv); static void WINAPI Handler(DWORD dwOpcode); // data members char m_szServiceName[64]; int m_iMajorVersion; int m_iMinorVersion; SERVICE_STATUS_HANDLE m_hServiceStatus; SERVICE_STATUS m_Status; CBoolean m_closing; // static data static CNTService* m_pThis; // nasty hack to get object ptr private: HANDLE m_hEventSource; };
The CNTService is based on the example in the Microsoft Development Network. The class is modified for handling servicces on remote computers, and other functionality is allso improved.
For a description on haw to use the CNTService please refer to The NT Service Cookbook.
A service based on the CNTService class is only intended for running in a single instance. Running the class in more than one instance, will result in errors.
The methods of the class con be devided into two categorys. The forst category containing IsInstalled, Install, Uninstall, and StartService, is intended for use by the setup program handling the service setup. The other group is used when implementing the service. This group contains the methods LogEvent, StartServiceDispatcher, SetStatus, Initialize, Run, OnInit, OnStop, OnInterrogate, OnPause, OnContinue, OnShutdown, and OnUserControl.
References:
Last Modified 10 January 2019