/*------------------------------------------------------------------------- * MTAKeepAliveThread *------------------------------------------------------------------------- * Purpose: * Thread controlling-function that initializes the COM MTA, registers * the server's externally-creatable class objects and sleeps. When the * quit event is signaled, the thread wakes up, revokes the class * objects, and uninitializes the COM MTA. */ void CServiceModule::MTAKeepAliveThread() { // Enter the COM MTA TCCoInit init(COINIT_MULTITHREADED); m_hrMTAKeepAlive = init; // Register the externally-creatable class objects if (init.Succeeded()) m_hrMTAKeepAlive = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE); // Signal the 'Ready' event assert(!m_shevtMTAReady.IsNull()); SetEvent(m_shevtMTAReady); RETURN_FAILED_VOID(m_hrMTAKeepAlive); // Wait for the 'Exit' event assert(!m_shevtMTAExit.IsNull()); WaitForSingleObject(m_shevtMTAExit, INFINITE); // Note: the following call was moved here from the ReceiveThread function // in FedSrv.cpp so that it will always be called from within the MTA. // Destroy all sessions now--before GetAGCGlobal() becomes invalid CAdminSession::DestroyAllSessions(); // Revoke the class objects _Module.RevokeClassObjects(); // CoUninitialize called automatically upon leaving scope (using TCCoInit) }
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /* hPrevInstance */, LPTSTR lpCmdLine, int /* nShowCmd */) { _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME); _Module.m_bService = TRUE; TCHAR szTokens[] = _T("-/"); TCHAR *szContext; LPTSTR lpszToken = _tcstok_s(lpCmdLine, szTokens, &szContext); while (lpszToken != NULL) { if (_tcsicmp(lpszToken, _T("UnregServer"))==0) return _Module.UnregisterServer(); if (_tcsicmp(lpszToken, _T("RegServer"))==0) return _Module.RegisterServer(TRUE); if (_tcsicmp(lpszToken, _T("Embedding"))==0) _Module.m_bService = FALSE; if (_tcsicmp(lpszToken, _T("LocalServer"))==0) { _Module.SetupAsLocalServer(); return 0; } lpszToken = _tcstok_s(NULL, szTokens, &szContext); } _Module.Start(); // When we get here, the service has been stopped return _Module.m_status.dwWin32ExitCode; }
void CServiceModule::Run() { _Module.dwThreadID = GetCurrentThreadId(); HRESULT hr = CoInitialize(NULL); // If you are running on NT 4.0 or higher you can use the following call // instead to make the EXE free threaded. // This means that calls come in on a random RPC thread // HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); _ASSERTE(SUCCEEDED(hr)); // This provides a NULL DACL which will allow access to everyone. CSecurityDescriptor sd; sd.InitializeFromThreadToken(); hr = CoInitializeSecurity(sd, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); _ASSERTE(SUCCEEDED(hr)); hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE); _ASSERTE(SUCCEEDED(hr)); LogEvent(_T("Service started")); if (m_bService) SetServiceStatus(SERVICE_RUNNING); MSG msg; while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg); _Module.RevokeClassObjects(); CoUninitialize(); }
int WINAPI ___tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/) { lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT _Module.Init(hInstance, _T("CallMeSleep"), NULL); _Module.m_bService = TRUE; TCHAR szTokens[] = _T("-/"); LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens); while (lpszToken != NULL) { if (lstrcmpi(lpszToken, _T("delete"))==0) return _Module.UnregisterServer(); // Register as Local Server if (lstrcmpi(lpszToken, _T("install"))==0) return _Module.RegisterServer(TRUE, FALSE); if(lstrcmpi(lpszToken, _T("time"))==0) { } lpszToken = FindOneOf(lpszToken, szTokens); } _Module.Start(); // When we get here, the service has been stopped return _Module.m_status.dwWin32ExitCode; }
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/) { lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_SENDERSERVERLib); _Module.m_bService = TRUE; TCHAR szTokens[] = _T("-/"); LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens); while (lpszToken != NULL) { if (lstrcmpi(lpszToken, _T("UnregServer"))==0) return _Module.UnregisterServer(); // Register as Local Server if (lstrcmpi(lpszToken, _T("RegServer"))==0) return _Module.RegisterServer(TRUE, FALSE); // Register as Service if (lstrcmpi(lpszToken, _T("Service"))==0) return _Module.RegisterServer(TRUE, TRUE); lpszToken = FindOneOf(lpszToken, szTokens); } // Are we Service or Local Server CRegKey keyAppID; LONG lRes = keyAppID.Open(HKEY_CLASSES_ROOT, _T("AppID"), KEY_READ); if (lRes != ERROR_SUCCESS) return lRes; CRegKey key; lRes = key.Open(keyAppID, _T("{87AB90ED-EFD4-4A6B-9CC7-6C87D5704FED}"), KEY_READ); if (lRes != ERROR_SUCCESS) return lRes; TCHAR szValue[_MAX_PATH]; DWORD dwLen = _MAX_PATH; lRes = key.QueryValue(szValue, _T("LocalService"), &dwLen); _Module.m_bService = FALSE; if (lRes == ERROR_SUCCESS) _Module.m_bService = TRUE; _Module.Start(); // When we get here, the service has been stopped return _Module.m_status.dwWin32ExitCode; }
void RegisterMonolithicDPlay() { HKEY hKey = NULL; const char * szKey = "CLSID\\{DA9CABC6-C724-4265-A61D-6E78EB2042B4}\\InprocServer32"; // check to see if it's already registered, and if so, do nothing, otherwise register it if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_CLASSES_ROOT, szKey, 0, KEY_READ, &hKey)) { // Get the module path TCHAR szFilePath[_MAX_PATH * 2], szDrive[_MAX_DRIVE], szDir[_MAX_DIR * 2]; ::GetModuleFileName(NULL, szFilePath, sizeofArray(szFilePath)); _tsplitpath(szFilePath, szDrive, szDir, NULL, NULL); _tmakepath(szFilePath, szDrive, szDir, NULL, NULL); int cch = _tcslen(szFilePath); if (TEXT('\\') == szFilePath[cch - 1]) szFilePath[cch - 1] = TEXT('\0'); // Update the registry from the DPMono.rgs USES_CONVERSION; _ATL_REGMAP_ENTRY regmap[] = { {L"MODULE_PATH", T2COLE(szFilePath)}, {NULL , NULL}, }; _Module.UpdateRegistryFromResource(IDR_DPMONO, true, regmap); } else { RegCloseKey(hKey); } }
static DWORD WINAPI EXEThreadProc(LPVOID lpParameter) { HRESULT hr; _Module.InitPIDs(); debugf("Initializing...\n"); hr = FedSrv_Init(); if (SUCCEEDED(hr)) { g.bRestarting = false; HANDLE hEventArray[] = { g.hKillReceiveEvent, hThread }; DWORD dwAwaker; do { dwAwaker = MsgWaitForMultipleObjects(2, hEventArray, FALSE, INFINITE, 0); if (dwAwaker == WAIT_OBJECT_0 + 1) { debugf("!!! oh no!\n"); break; } } while (dwAwaker != WAIT_OBJECT_0); FedSrv_Terminate(); } else { debugf("\rInitialization failed. (%x)\n", hr); return (DWORD)-1; } return (DWORD)0; }
void CServiceModule::Run() { _Module.dwThreadID = GetCurrentThreadId(); //HRESULT hr = CoInitialize(NULL); // If you are running on NT 4.0 or higher you can use the following call // instead to make the EXE free threaded. // This means that calls come in on a random RPC thread //Imago changed 6/25/08 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); _ASSERTE(SUCCEEDED(hr)); // This provides a NULL DACL which will allow access to everyone. CSecurityDescriptor sd; sd.InitializeFromThreadToken(); hr = CoInitializeSecurity(sd, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); _ASSERTE(SUCCEEDED(hr)); hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE); _ASSERTE(SUCCEEDED(hr)); LogEvent(EVENTLOG_INFORMATION_TYPE, LE_Started); if (m_bService) SetServiceStatus(SERVICE_RUNNING); { TRef<CLobbyApp> plobbyapp = new CLobbyApp(this); if SUCCEEDED(plobbyapp->Init()) plobbyapp->Run(); else LogEvent(EVENTLOG_ERROR_TYPE, LE_StartFailed); }
BOOL CMfc1App::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. _Module.Init(ObjectMap, this->m_hInstance, IDS_SERVICENAME, NULL); _Module.m_bService = TRUE; bool uninstall = false; if( __argc > 1 ) { for(int i=0; i<__argc; i++) { if( _stricmp(__argv[i], "/l") == 0) { i++; _Module.m_szUrl = std::string(__argv[i]); } else if( _stricmp(__argv[i], "/u") == 0 ) { uninstall = true; } else if( _stricmp(__argv[i], "/t") == 0 ) { i++; _Module.m_szTarget = std::string(__argv[i]); } } } if(uninstall) { _Module.Uninstall(); return FALSE; } _Module.Start(); // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }
void CServiceModule::StopAllsrv() { #if defined(SRV_PARENT) || defined(SRV_CHILD) if (g.hKillReceiveEvent) SetEvent(g.hKillReceiveEvent); #else if (_Module.IsInstalledAsService()) _Module.ServiceControl(SERVICE_CONTROL_STOP); else { if (g.hKillReceiveEvent) SetEvent(g.hKillReceiveEvent); // if(m_hEventStopRunningAsEXE) // SetEvent(m_hEventStopRunningAsEXE); } #endif }
void CServiceModule::Run() { HRESULT hr; _Module.dwThreadID = GetCurrentThreadId(); // If you are running on NT 4.0 or higher you can use the following call // to make your service free threaded. // This means that calls come in on a random RPC thread hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); // Otherwise, for single threaded use // hr = CoInitialize(NULL); ATLASSERT(SUCCEEDED(hr)); // In NT 4.0 and higher where DCOM is supported you may wish to initialize // the security layer. The following code will give you a DCOM service which // would allow ALL users access to objects CSecurityDescriptor sd; sd.InitializeFromThreadToken(); hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL); ATLASSERT(SUCCEEDED(hr)); // If you wish to have your object creatable remotely using DCOM use the following hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE); // Otherwise, only local activation // hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE); ATLASSERT(SUCCEEDED(hr)); LogEvent(_T("Service started")); SetServiceStatus(SERVICE_RUNNING); MSG msg; while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg); _Module.RevokeClassObjects(); CoUninitialize(); }
HRESULT CServiceModule::UnregisterServer() { // Enter the COM MTA TCCoInit init(COINIT_MULTITHREADED); if (init.Failed()) return init; // Initialize the ATL module HRESULT hr = _Module.Init(g.hInst); // init COM and/or NT service stuff ZSucceeded(hr); // Create the component category manager CComPtr<ICatRegister> spCatReg; hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_ALL, IID_ICatRegister, (void**)&spCatReg); ZSucceeded(hr); if (SUCCEEDED(hr)) { // Unregister our component category CATID catid = CATID_AllegianceAdmin; spCatReg->UnRegisterCategories(1, &catid); spCatReg = NULL; } // Remove service entries UpdateRegistryFromResource(IDR_AllSrv, FALSE); // uninstall service if (IsInServiceControlManager()) { RemoveService(); } // Remove object entries CComModule::UnregisterServer(TRUE); // Terminate the ATL module _Module.Term(); return hr; }
void WINAPI MPServiceMain(DWORD dwArgc, LPTSTR* lpszArgv) { MainThreadId = ::GetCurrentThreadId(); ZeroMemory(&ServiceStatus, sizeof(ServiceStatus)); ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; ServiceStatus.dwCurrentState = SERVICE_STOPPED; ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; hServiceStatus = RegisterServiceCtrlHandler(c_szSvcName, ServiceHandler); SetServiceStatus(SERVICE_START_PENDING); hShutdownEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL); DWORD ThreadId = 0; hThread = ::CreateThread(NULL, NULL, ThreadProc, NULL, NULL, &ThreadId); MSG msg; while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg); SetEvent(hShutdownEvent); WaitForSingleObject(hThread, INFINITE); ::CloseHandle(hThread); ::CloseHandle(hShutdownEvent); SetServiceStatus(SERVICE_STOPPED); _Module.BreakChildren(); }
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/) { #if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED) HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); #else HRESULT hr = CoInitialize(NULL); #endif // SetMiniDumpDefaultCrashHandler(); _ASSERTE(SUCCEEDED(hr)); lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT _Module.m_bService = FALSE; TCHAR szTokens[] = _T("-/"); LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens); while (lpszToken != NULL) { if (lstrcmpi(lpszToken, _T("Install"))==0) { _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_TRANSPORTLib); _Module.m_bService = TRUE; // CRouter::Install(); return _Module.Install(); } if (lstrcmpi(lpszToken, _T("Uninstall"))==0) { _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_TRANSPORTLib); _Module.m_bService = TRUE; // CRouter::Uninstall(); return _Module.Uninstall(); } if (lstrcmpi(lpszToken, _T("UnregServer"))==0) { _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_TRANSPORTLib); _Module.m_bService = TRUE; _Module.UpdateRegistryFromResource(IDR_Transport, FALSE); return _Module.UnregisterServer(); } if (lstrcmpi(lpszToken, _T("RegServer"))==0) { _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_TRANSPORTLib); _Module.m_bService = TRUE; _Module.UpdateRegistryFromResource(IDR_Transport, TRUE); return _Module.RegisterServer(TRUE,TRUE); } if (lstrcmpi(lpszToken, _T("Debug"))==0) { _Module.CComModule::Init(ObjectMap, hInstance, &LIBID_TRANSPORTLib); _Module.Start(); _Module.Term(); ::CoUninitialize(); return 0; } lpszToken = FindOneOf(lpszToken, szTokens); } _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_TRANSPORTLib); _Module.m_bService = TRUE; _Module.Start(); _Module.Term(); ::CoUninitialize(); // When we get here, the service has been stopped return _Module.m_status.dwWin32ExitCode; }
void CServiceModule::Run() { _Module.dwThreadID = GetCurrentThreadId(); _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); CEgTcpDriver::Init(); if(!CEgLibTraceManager::g_spTrace) { CEgTpTracePtr spTrace = CEgTpTracePtr(new CEgTpTrace()); if(spTrace) { spTrace->ReadTraceSettings(); CEgLibTraceManager::g_spTrace = boost::shared_static_cast<CEgLibTraceManager>(spTrace); } } // If you are running on NT 4.0 or higher you can use the following call // instead to make the EXE free threaded. // This means that calls come in on a random RPC thread // HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); HRESULT hRes; if(m_bService) { // This provides a NULL DACL which will allow access to everyone. CSecurityDescriptor sd; hRes = sd.InitializeFromThreadToken(); if(FAILED(hRes)) { Trace(1,_T("Transport Service Run"),_T("InitializeFromThreadToken error %X"),hRes); } hRes = CoInitializeSecurity(sd, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); if(FAILED(hRes)) { Trace(1,_T("Transport Service Run"),_T("CoInitializeSecurity error %X"),hRes); } _ASSERTE(SUCCEEDED(hRes)); hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED); if(FAILED(hRes)) { Trace(1,_T("Transport Service Run"),_T("RegisterClassObjects error %X"),hRes); } _ASSERTE(SUCCEEDED(hRes)); hRes = CoResumeClassObjects(); if(FAILED(hRes)) { Trace(1,_T("Transport Service Run"),_T("CoResumeClassObjects error %X"),hRes); } } else { #if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED) hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED); _ASSERTE(SUCCEEDED(hRes)); hRes = CoResumeClassObjects(); #else hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE); _ASSERTE(SUCCEEDED(hRes)); #endif } DoSettings(); //EgAssert(g_pHost); //EgAssert(g_pRouter); if (m_bService) SetServiceStatus(SERVICE_RUNNING); CEgLibTraceManager::Trace(LogError,_T("Transport Service Run"),_T("Transport Service started")); m_isRunning = TRUE; MSG msg; while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg); m_isRunning = FALSE; _Module.RevokeClassObjects(); CEgLibTraceManager::Trace(LogError,_T("Transport Service Run"),_T("Transport Service stopped")); if(CEgLibTraceManager::g_spTrace!=NULL) CEgLibTraceManager::g_spTrace.reset(); if (m_bService) SetServiceStatus(SERVICE_STOPPED); CEgTcpDriver::Exit(); }
void WINAPI CServiceModule::_Handler(DWORD dwOpcode) { _Module.Handler(dwOpcode); }
void WINAPI CServiceModule::_ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv) { _Module.ServiceMain(dwArgc, lpszArgv); }
void stopFrommyInter() { _Module.SetServiceStatus(SERVICE_STOP_PENDING); PostThreadMessage(_Module.dwThreadID, WM_QUIT, 0, 0); }
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/) { lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, IDS_SERVICEDISPLAYNAME, &LIBID_VIRTUOSOSINKLib); _Module.m_bService = TRUE; TCHAR szTokens[] = _T("-/"); LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens); while (lpszToken != NULL) { if (lstrcmpi(lpszToken, _T("UnregServer"))==0) return _Module.UnregisterServer(); // Register as Local Server if (lstrcmpi(lpszToken, _T("RegServer"))==0) return _Module.RegisterServer(TRUE, FALSE); // Register as Service if (lstrcmpi(lpszToken, _T("Service"))==0) return _Module.RegisterServer(TRUE, TRUE); // Register as Service if (lstrcmpi(lpszToken, _T("Help"))==0 || lstrcmpi(lpszToken, _T("?"))==0) { char szBuffer[4096]; wsprintf (szBuffer, "%s Help", _Module.m_szServiceDisplayName); ::MessageBox (NULL, "/RegServer\t - registers the COM server\n" "/Service\t\t - registers the COM server & the service instance\n" "/UnregServer\t - unregisters the service instance & the COM server\n", szBuffer, MB_OK | MB_ICONINFORMATION); return TRUE; } lpszToken = FindOneOf(lpszToken, szTokens); } // Are we Service or Local Server CRegKey keyAppID; LONG lRes = keyAppID.Open(HKEY_CLASSES_ROOT, _T("AppID"), KEY_READ); if (lRes != ERROR_SUCCESS) return lRes; CRegKey key; lRes = key.Open(keyAppID, _T("{C9C50643-AE52-11D4-8986-00E018001CA1}"), KEY_READ); if (lRes != ERROR_SUCCESS) return lRes; TCHAR szValue[_MAX_PATH]; DWORD dwLen = _MAX_PATH; lRes = key.QueryValue(szValue, _T("LocalService"), &dwLen); _Module.m_bService = FALSE; if (lRes == ERROR_SUCCESS) _Module.m_bService = TRUE; _Module.Start(); // When we get here, the service has been stopped return _Module.m_status.dwWin32ExitCode; }
/*------------------------------------------------------------------------- * MTAKeepAliveThunk *------------------------------------------------------------------------- * Purpose: * static thunk function to reinterpret the thread parameter and call * through it as a 'this' pointer. */ DWORD WINAPI CServiceModule::MTAKeepAliveThunk(void* pvThis) { CServiceModule* pThis = reinterpret_cast<CServiceModule*>(pvThis); pThis->MTAKeepAliveThread(); return 0; }
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/) { lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT _Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_WIPPIENSERVICELib); _Module.m_bService = TRUE; TCHAR szTokens[] = _T("-/"); LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens); while (lpszToken != NULL) { if (_strnicmp(lpszToken, _T("UnregServer"), 11)==0) flagUnregServer = TRUE; // Register as Local Server if (_strnicmp(lpszToken, _T("RegServer"), 9)==0) flagRegServer = TRUE; // Register as Service if (_strnicmp(lpszToken, _T("Service"), 7)==0) flagService = TRUE; // Register as Service if (_strnicmp(lpszToken, _T("AutoStart"), 9)==0) flagAutoStart = SERVICE_AUTO_START; lpszToken = FindOneOf(lpszToken, szTokens); } if (flagUnregServer) return _Module.UnregisterServer(); if (flagRegServer) return _Module.RegisterServer(TRUE, FALSE); if (flagService) return _Module.RegisterServer(TRUE, TRUE); // Are we Service or Local Server CRegKey keyAppID; LONG lRes = keyAppID.Open(HKEY_CLASSES_ROOT, _T("AppID"), KEY_READ); if (lRes != ERROR_SUCCESS) return lRes; CRegKey key; lRes = key.Open(keyAppID, _T("{A3084FF8-63BF-40A1-8AE0-AD792B0078D9}"), KEY_READ); if (lRes != ERROR_SUCCESS) return lRes; TCHAR szValue[_MAX_PATH]; DWORD dwLen = _MAX_PATH; lRes = key.QueryValue(szValue, _T("LocalService"), &dwLen); _Module.m_bService = FALSE; if (lRes == ERROR_SUCCESS) _Module.m_bService = TRUE; WODXMPP::_XMPP_LibInit(_Module.GetModuleInstance()); WODVPN::_VPN_LibInit(_Module.GetModuleInstance()); if (LoadConfig()) { _Jabber = new CJabberLib(); _Ethernet = new CEthernet(); if (_Ethernet->InitAdapter()) { _Module.LogEvent("Connecting to the XMPP server." ); _Ethernet->GetMyIP(); _Module.Start(); } else _Module.LogEvent(_T("Wippien Service failed to open Network adapter.")); } else { char bf[8192]; sprintf(bf, "Could not read: %s.config", __argv[0]); _Module.LogEvent(bf); } WODVPN::_VPN_LibDeinit(); WODXMPP::_XMPP_LibDeInit(); // When we get here, the service has been stopped return _Module.m_status.dwWin32ExitCode; }
HRESULT CServiceModule::RegisterServer(BOOL bReRegister, BOOL bRegTypeLib, BOOL bService, int argc, char * argv[]) { // Enter the COM MTA TCCoInit init(COINIT_MULTITHREADED); if (init.Failed()) return init; // Initialize the ATL module HRESULT hr = _Module.Init(g.hInst); // init COM and/or NT service stuff ZSucceeded(hr); if (IsWin9x()) bService = false; // Windows 9x doesn't support services // Remove any previous service since it may point to // the incorrect file if (IsInServiceControlManager() && !bReRegister) RemoveService(); // Add service entries _Module.UpdateRegistryFromResource(IDR_AllSrv, true, _AGCModule.GetRegMapEntries()); #ifdef MONOLITHIC_DPLAY // Register custom dplay bits RegisterMonolithicDPlay(); #endif // Create the component category manager CComPtr<ICatRegister> spCatReg; hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_ALL, IID_ICatRegister, (void**)&spCatReg); ZSucceeded(hr); if (SUCCEEDED(hr)) { // Determine the LCID for US English const LANGID langid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US); const LCID lcid = MAKELCID(langid, SORT_DEFAULT); // Register the component category CATEGORYINFO catinfo; catinfo.catid = CATID_AllegianceAdmin; catinfo.lcid = lcid; wcscpy(catinfo.szDescription, L"Allegiance Admin Objects"); hr = spCatReg->RegisterCategories(1, &catinfo); ZSucceeded(hr); // Explicitly release the smart pointer spCatReg = NULL; } if (!bReRegister) { // Adjust the AppID for Local Server or Service CRegKey keyAppID; DWORD lRes = keyAppID.Open(HKEY_CLASSES_ROOT, _T("AppID"), KEY_WRITE); if (lRes != ERROR_SUCCESS) return lRes; CRegKey key; lRes = key.Open(keyAppID, _T("{E4E8767E-DFDB-11d2-8B46-00C04F681633}"), KEY_WRITE); if (lRes != ERROR_SUCCESS) return lRes; key.DeleteValue(_T("LocalService")); if (bService) { // mdvalley: I hate my ATL libraries sometimes (SetStringValue) key.SetValue(_T(__MODULE__), _T("LocalService")); key.SetValue(_T("-Service"), _T("ServiceParameters")); // Create service InstallService(argc, argv); } } // Add object entries hr = _Module.CComModule::RegisterServer(bRegTypeLib); // Terminate the ATL module _Module.Term(); return hr; }