VC9.0 ATL向导生成 Windows服务,修改启动类型和备注

class CIMServerModule : public CAtlServiceModuleT< CIMServerModule, IDS_SERVICENAME > { private: CUdpListener m_oUdpListener; public : CIMServerModule() { m_status.dwControlsAccepted |= SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; } DECLARE_LIBID(LIBID_IMServerLib) DECLARE_REGISTRY_APPID_RESOURCEID(IDR_IMSERVER, "{87327DB3-313E-43B4-9CA6-139A2F742EDF}") HRESULT InitializeSecurity() throw() { // TODO : Call CoInitializeSecurity and provide the appropriate security settings for // your service // Suggested - PKT Level Authentication, // Impersonation Level of RPC_C_IMP_LEVEL_IDENTIFY // and an appropiate Non NULL Security Descriptor. CSecurityDescriptor sd; sd.InitializeFromThreadToken(); HRESULT hr = CoInitializeSecurity( sd , -1 , NULL , NULL , RPC_C_AUTHN_LEVEL_PKT , RPC_C_IMP_LEVEL_IMPERSONATE , NULL , EOAC_NONE , NULL ); return S_OK; } HRESULT PreMessageLoop(int nShowCmd) throw() { HRESULT hr = S_OK; hr = CAtlServiceModuleT<CIMServerModule,IDS_SERVICENAME>::PreMessageLoop(nShowCmd); hr = CoResumeClassObjects(); if (SUCCEEDED(hr)) { if( !m_oUdpListener.Start() ) { RevokeClassObjects(); return E_FAIL; } } return hr; } HRESULT PostMessageLoop() throw() { HRESULT hr = S_OK; hr = CAtlServiceModuleT< CIMServerModule,IDS_SERVICENAME>::PostMessageLoop(); if (SUCCEEDED(hr)) { m_oUdpListener.Stop(); } return hr; } HRESULT RegisterAppId(bool bService=false) { HRESULT hr = S_OK; hr = CAtlServiceModuleT< CIMServerModule,IDS_SERVICENAME>::RegisterAppId(bService); if (!bService) return hr; SC_HANDLE hSCM = NULL; SC_LOCK sclLock = NULL; SC_HANDLE hService = NULL; try { hSCM = ::OpenSCManager(NULL, NULL, GENERIC_WRITE | SC_MANAGER_LOCK); if (hSCM == NULL) { OutputDebugString( _T("Error, OpenSCManager failed.")); hr = E_FAIL; goto lblEnd; } sclLock = LockServiceDatabase(hSCM); if(sclLock == NULL) { OutputDebugString( _T("Error, LockServiceDatabase failed.")); hr = E_FAIL; goto lblEnd; } hService = OpenService( hSCM , m_szServiceName , SERVICE_CHANGE_CONFIG ); if (hService == NULL) { OutputDebugString( _T("Error, OpenService failed.")); hr = E_FAIL; goto lblEnd; } if (! ChangeServiceConfig( hService , SERVICE_WIN32_OWN_PROCESS , SERVICE_AUTO_START , SERVICE_NO_CHANGE , NULL , NULL , NULL , NULL , NULL , NULL , NULL ) ) { OutputDebugString( _T("Error, ChangeServiceConfig failed.")); hr = E_FAIL; goto lblEnd; } SERVICE_DESCRIPTION sdBuf; memset(&sdBuf, 0, sizeof(sdBuf)); sdBuf.lpDescription = _T("呼叫中心即时通信服务"); if( !ChangeServiceConfig2( hService, SERVICE_CONFIG_DESCRIPTION, &sdBuf) ) { OutputDebugString( _T("Error, ChangeServiceConfig2 failed.")); hr = E_FAIL; goto lblEnd; } } catch(...) { OutputDebugString( _T("Error, Unknown exception.")); hr = E_FAIL; goto lblEnd; } lblEnd: if (sclLock) UnlockServiceDatabase(sclLock); if (hService) ::CloseServiceHandle(hService); if (hSCM) ::CloseServiceHandle(hSCM); return hr; } };

 

 

安装的批处理:

IMServer.exe -/Service net start IMServer cmd.exe

 

卸载的批处理

net stop IMServer IMServer.exe /Unregserver

你可能感兴趣的:(windows,manager,service,Security,null,Authentication)