Blame | Last modification | View Log
/*Module : SERV.HPurpose: Defines the interface for a number of MFC classes whichencapsulate the whole area of services in NT.Created: PJN / 14-07-1998Copyright (c) 1998 - 2001 by PJ Naughter.All rights reserved.Classes provided include:CEventLogRecord: A simple C++ class which adds some helpful functionsto the underlying EVENTLOGRECORD structure.CNTEventLogSource: A C++ wrapper for the "Server" side of the Event LogAPI's. You would normally use this class in the code when installing,uninstalling your service or when reporting to the event log whileyour service is running.*/#include "StdAfx.h"#include "afxmt.h"#include "ntserv_msg.h"#ifndef __NTSERV_H__#define __NTSERV_H__//Define some values if the "NTSERV_DO_NEW_WINSVC_DEFINES" define is present. This means that the class will//at least compile even when you do not have the latest and greatest Platform SDK installed such as when using//the code with VC 5 or VC 6 on their own./*#ifdef NTSERV_DO_NEW_WINSVC_DEFINES//The following defines are taken from the Platform SDK header file "WinSvc.h"typedef struct _SERVICE_STATUS_PROCESS {DWORD dwServiceType;DWORD dwCurrentState;DWORD dwControlsAccepted;DWORD dwWin32ExitCode;DWORD dwServiceSpecificExitCode;DWORD dwCheckPoint;DWORD dwWaitHint;DWORD dwProcessId;DWORD dwServiceFlags;} SERVICE_STATUS_PROCESS, *LPSERVICE_STATUS_PROCESS;typedef struct _ENUM_SERVICE_STATUS_PROCESSA {LPSTR lpServiceName;LPSTR lpDisplayName;SERVICE_STATUS_PROCESS ServiceStatusProcess;} ENUM_SERVICE_STATUS_PROCESSA, *LPENUM_SERVICE_STATUS_PROCESSA;typedef struct _ENUM_SERVICE_STATUS_PROCESSW {LPWSTR lpServiceName;LPWSTR lpDisplayName;SERVICE_STATUS_PROCESS ServiceStatusProcess;} ENUM_SERVICE_STATUS_PROCESSW, *LPENUM_SERVICE_STATUS_PROCESSW;#ifdef UNICODEtypedef ENUM_SERVICE_STATUS_PROCESSW ENUM_SERVICE_STATUS_PROCESS;typedef LPENUM_SERVICE_STATUS_PROCESSW LPENUM_SERVICE_STATUS_PROCESS;#elsetypedef ENUM_SERVICE_STATUS_PROCESSA ENUM_SERVICE_STATUS_PROCESS;typedef LPENUM_SERVICE_STATUS_PROCESSA LPENUM_SERVICE_STATUS_PROCESS;#endif // UNICODEtypedef DWORD (WINAPI *LPHANDLER_FUNCTION_EX)(DWORD dwControl,DWORD dwEventType,LPVOID lpEventData,LPVOID lpContext);typedef enum _SC_STATUS_TYPE {SC_STATUS_PROCESS_INFO = 0} SC_STATUS_TYPE;typedef enum _SC_ENUM_TYPE {SC_ENUM_PROCESS_INFO = 0} SC_ENUM_TYPE;#define SERVICE_CONFIG_DESCRIPTION 1#define SERVICE_CONFIG_FAILURE_ACTIONS 2#define SERVICE_CONTROL_PARAMCHANGE 0x00000006#define SERVICE_CONTROL_NETBINDADD 0x00000007#define SERVICE_CONTROL_NETBINDREMOVE 0x00000008#define SERVICE_CONTROL_NETBINDENABLE 0x00000009#define SERVICE_CONTROL_NETBINDDISABLE 0x0000000A#define SERVICE_CONTROL_DEVICEEVENT 0x0000000B#define SERVICE_CONTROL_HARDWAREPROFILECHANGE 0x0000000C#define SERVICE_CONTROL_POWEREVENT 0x0000000D#ifdef NTSERV_EXCLUDE_VC6_WINSVC_DEFINEStypedef enum _SC_ACTION_TYPE {SC_ACTION_NONE = 0,SC_ACTION_RESTART = 1,SC_ACTION_REBOOT = 2,SC_ACTION_RUN_COMMAND = 3} SC_ACTION_TYPE;typedef struct _SC_ACTION {SC_ACTION_TYPE Type;DWORD Delay;} SC_ACTION, *LPSC_ACTION;typedef struct _SERVICE_FAILURE_ACTIONSA {DWORD dwResetPeriod;LPSTR lpRebootMsg;LPSTR lpCommand;DWORD cActions;#ifdef MIDL_PASS[size_is(cActions)]#endifSC_ACTION * lpsaActions;} SERVICE_FAILURE_ACTIONSA, *LPSERVICE_FAILURE_ACTIONSA;typedef struct _SERVICE_FAILURE_ACTIONSW {DWORD dwResetPeriod;LPWSTR lpRebootMsg;LPWSTR lpCommand;DWORD cActions;#ifdef MIDL_PASS[size_is(cActions)]#endifSC_ACTION * lpsaActions;} SERVICE_FAILURE_ACTIONSW, *LPSERVICE_FAILURE_ACTIONSW;#ifdef UNICODEtypedef SERVICE_FAILURE_ACTIONSW SERVICE_FAILURE_ACTIONS;typedef LPSERVICE_FAILURE_ACTIONSW LPSERVICE_FAILURE_ACTIONS;#elsetypedef SERVICE_FAILURE_ACTIONSA SERVICE_FAILURE_ACTIONS;typedef LPSERVICE_FAILURE_ACTIONSA LPSERVICE_FAILURE_ACTIONS;#endif // UNICODE#endif //#ifdef NTSERV_EXCLUDE_VC6_WINSVC_DEFINES//// Info levels for ChangeServiceConfig2 and QueryServiceConfig2//#define SERVICE_CONFIG_DESCRIPTION 1#define SERVICE_CONFIG_FAILURE_ACTIONS 2#ifdef NTSERV_EXCLUDE_VC6_WINSVC_DEFINES//// Service description string//typedef struct _SERVICE_DESCRIPTIONA {LPSTR lpDescription;} SERVICE_DESCRIPTIONA, *LPSERVICE_DESCRIPTIONA;//// Service description string//typedef struct _SERVICE_DESCRIPTIONW {LPWSTR lpDescription;} SERVICE_DESCRIPTIONW, *LPSERVICE_DESCRIPTIONW;#ifdef UNICODEtypedef SERVICE_DESCRIPTIONW SERVICE_DESCRIPTION;typedef LPSERVICE_DESCRIPTIONW LPSERVICE_DESCRIPTION;#elsetypedef SERVICE_DESCRIPTIONA SERVICE_DESCRIPTION;typedef LPSERVICE_DESCRIPTIONA LPSERVICE_DESCRIPTION;#endif // UNICODE#endif //#ifdef NTSERV_EXCLUDE_VC6_WINSVC_DEFINES#endif //NTSERV_DO_NEW_WINSVC_DEFINES*///An encapsulation of the APIs used to register, unregister,//write, install and uninstall Event log entries i.e. the//server side to the Event log APIsclass CNTEventLogSource{public://Constructors / DestructorsCNTEventLogSource(LPCTSTR lpUNCServerName, LPCTSTR lpSourceName);~CNTEventLogSource();//Methodsoperator HANDLE() const;BOOL Attach(HANDLE hEventSource);HANDLE Detach();BOOL Register(LPCTSTR lpUNCServerName, // server name for sourceLPCTSTR lpSourceName // source name for registered handle);BOOL Deregister();BOOL Report(WORD wType, // event type to logWORD wCategory, // event categoryDWORD dwEventID, // event identifierPSID lpUserSid, // user security identifier (optional)WORD wNumStrings, // number of strings to merge with messageDWORD dwDataSize, // size of binary data, in bytesLPCTSTR* lpStrings, // array of strings to merge with messageLPVOID lpRawData // address of binary data);BOOL Report(WORD wType, DWORD dwEventID);BOOL Report(WORD wType, LPCTSTR lpszString);BOOL Report(WORD wType, DWORD dwEventID, LPCTSTR lpszString);BOOL Report(WORD wType, DWORD dwEventID, LPCTSTR lpszString1, LPCTSTR lpszString2);BOOL Report(WORD wType, DWORD dwEventID, LPCTSTR lpszString1, LPCTSTR lpszString2, int len2);BOOL Report(WORD wType, DWORD dwEventID, LPCTSTR lpszString1, LPCTSTR lpszString2, DWORD dwCode, BOOL bReportAsHex=FALSE);BOOL Report(WORD wType, DWORD dwEventID, LPCTSTR lpszString, DWORD dwCode, BOOL bReportAsHex=FALSE);BOOL Report(WORD wType, DWORD dwEventID, DWORD dwCode, BOOL bReportAsHex=FALSE);static BOOL Install(LPCTSTR lpSourceName, LPCTSTR lpEventMessageFile, DWORD dwTypesSupported);static BOOL Uninstall(LPCTSTR lpSourceName);protected:static BOOL GetStringArrayFromRegistry(HKEY hKey, LPCTSTR lpszEntry, CStringArray& array);static BOOL SetStringArrayIntoRegistry(HKEY hKey, LPCTSTR lpszEntry, const CStringArray& array);HANDLE m_hEventSource;friend class CNTService;CString m_sServerName;CString m_sSourceName;CCriticalSection m_csReport; //Critical section to protect multiple threads calling Report at the one time};/*//A friendlier way of handling EVENTLOGRECORD structures.class CEventLogRecord{public://Constructors / DestructorsCEventLogRecord();CEventLogRecord(const CEventLogRecord& record);CEventLogRecord(const EVENTLOGRECORD* pRecord);//MethodsCEventLogRecord& operator=(const CEventLogRecord& record);//DataDWORD m_dwRecordNumber;CTime m_TimeGenerated;CTime m_TimeWritten;DWORD m_dwEventID;WORD m_wEventType;WORD m_wEventCategory;CByteArray m_UserSID;CStringArray m_Strings;CByteArray m_Data;CString m_sSourceName;CString m_sComputerName;};*/#endif //__NTSERV_H__