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
|
#include "stdafx.h" #include "resource.h" #include "MyService_i.h"
#include <stdio.h>
class CMyServiceModule : public ATL::CAtlServiceModuleT< CMyServiceModule, IDS_SERVICENAME > { public : DECLARE_LIBID(LIBID_MyServiceLib) DECLARE_REGISTRY_APPID_RESOURCEID(IDR_MYSERVICE, "{38D52E46-9042-4AC9-9D57-4A13E350B4EF}") HRESULT InitializeSecurity() throw() {
return S_OK; } HRESULT RegisterAppId(bool bService = false) throw (); void ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv); HRESULT PostMessageLoop(); void Handler(DWORD dwOpcode); void OnStop(); void OnPause(); void OnContinue(); void OnInterrogate(); void OnShutdown(); HRESULT PreMessageLoop(int nShowCmd);
};
CMyServiceModule _AtlModule;
extern "C" int WINAPI _tWinMain(HINSTANCE , HINSTANCE , LPTSTR , int nShowCmd) { return _AtlModule.WinMain(nShowCmd); }
HRESULT CMyServiceModule::RegisterAppId(bool bService ) throw () { HRESULT hr = S_OK; BOOL res = __super ::RegisterAppId(bService); if (bService) { if (IsInstalled()) { SC_HANDLE hSCM = ::OpenSCManagerW(NULL, NULL, SERVICE_CHANGE_CONFIG); SC_HANDLE hService = NULL; if (hSCM == NULL) { hr = ATL::AtlHresultFromLastError(); } else { hService = ::OpenService(hSCM, m_szServiceName, SERVICE_CHANGE_CONFIG); if (hService != NULL) { ::ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_AUTO_START, NULL, NULL, NULL, NULL, NULL, NULL, NULL, m_szServiceName);
SERVICE_DESCRIPTION Description; TCHAR szDescription[1024]; ZeroMemory(szDescription, 1024); ZeroMemory(&Description, sizeof (SERVICE_DESCRIPTION)); lstrcpy(szDescription, _T("测试服务描述信息" )); Description.lpDescription = szDescription; ::ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &Description); ::CloseServiceHandle(hService); } else { hr = ATL::AtlHresultFromLastError(); } ::CloseServiceHandle(hSCM); } } } return hr; }
void CMyServiceModule::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv) {
__super::ServiceMain(dwArgc,lpszArgv); }
HRESULT CMyServiceModule::PostMessageLoop() { return CAtlServiceModuleT::PostMessageLoop(); }
void CMyServiceModule::Handler(DWORD dwOpcode) { __super::Handler(dwOpcode); }
void CMyServiceModule::OnStop() throw() {
__super::OnStop(); }
void CMyServiceModule::OnPause() throw() { __super::OnPause(); }
void CMyServiceModule::OnContinue() throw() { __super::OnContinue(); }
void CMyServiceModule::OnInterrogate() throw() { __super::OnInterrogate();
}
void CMyServiceModule::OnShutdown() throw() { __super::OnShutdown(); }
HRESULT CMyServiceModule::PreMessageLoop(int nShowCmd) { HRESULT res= CAtlServiceModuleT::PreMessageLoop(nShowCmd); if(SUCCEEDED(res)){
}
return res; }
|