BOOL CTeapotApp::InitInstance() { CWinApp::InitInstance(); // 如果装配文件 (manifest) 中指定使用 Windows 通用控件 ComCtl32.dll 6.0+ 版本, 则在 Windows XP 下需要调用 InitCommonControlsEx(), 否则窗口创建将失败 // 设置程序中能够使用的通用控件类, ICC_WIN95_CLASSES 表示所有 Win95 通用控件 INITCOMMONCONTROLSEX initCtrls; initCtrls.dwSize = sizeof(initCtrls); initCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&initCtrls); InitLocale(); // 计算主模块全路径, 设置工作目录 InitModulePath(MAX_PATH); InitWorkDir(GetModuleDir()); // NOTE: // 保存应用设置选项的注册表键, 通常命名为开发公司, 组织, 团队的名字 // 该函数设定 m_pszRegistryKey, 并影响 GetProfileInt 等函数 // 保存应用设置的注册表键: // HKCU\Software\<company name>\<application name>\<section name>\<value name> // 当不使用注册表而使用 ini 文件时, 请去掉 SetRegistryKey SetRegistryKey(_T(MODULE_NAME)); try { CMainFrame* pFrmWnd = new CMainFrame(); // 由共享资源 IDR_MAINFRAME 创建主框架窗口, IDR_MAINFRAME 可同时标识: 菜单(必需), 快捷键, 主图标, 标题栏字符串 pFrmWnd->LoadFrame(IDR_MAINFRAME); // NOTE: 在 Run 中显示窗口 pFrmWnd->ShowWindow(SW_HIDE); m_pMainWnd = pFrmWnd; } catch (std::exception& e) { ERR_TRACEA("std::exception: what: %s, type: %s", e.what(), typeid(e).name()); return FALSE; } catch (...) { ERR_TRACE("unknown exception"); return FALSE; } return TRUE; }
void CProProcess::InitProProcess(CString sProcess) { InitLogFile(); g_Log.WriteLine(_T("\nInitProProcess")); m_bIsProtect = false; m_hEvent = NULL; m_hMemMap = NULL; m_pMemPointer = NULL; m_thTimeThread = NULL; m_sProcess = sProcess; if (!m_hEvent) { TCHAR pAppEventName[256]={0}; lstrcpy(pAppEventName,FASTRUN_APP_EVENT_NAME); lstrcat(pAppEventName,sProcess); m_hEvent = ::CreateEvent (NULL,FALSE,FALSE,pAppEventName); } if (!m_hMemMap) { TCHAR pAppNotifyName[256]={0}; lstrcpy(pAppNotifyName,FASTRUN_NOTIFY_EVENT_NAME); lstrcat(pAppNotifyName,sProcess); m_hMemMap = ::CreateFileMapping (INVALID_HANDLE_VALUE,NULL, PAGE_READWRITE,0,sizeof(int),pAppNotifyName); } m_pMemPointer = ::MapViewOfFile (m_hMemMap,FILE_MAP_WRITE,0,0,0); *((int*)m_pMemPointer) = 0; MyRegister(TRUE); InitModulePath(); if (!m_thTimeThread) { m_thTimeThread= ::AfxBeginThread (TimeCheckingThreadFunc, NULL, THREAD_PRIORITY_BELOW_NORMAL); } //默认不是保护 就必须通过配置文件记录保护属性 //得到配置文件的属性 if (GetProfileSetting()) StartProtect(); else StopProtect(); }
VOID Core20Init(HINSTANCE hInstance) { InitModulePath(hInstance); MemInit(); }
int _tmain(int argc, _TCHAR* argv []) { if (argc < 3 || argc > 4) { puts("usage: System.ServiceProcess.ServiceController.TestNativeService.exe <ServiceName> <DisplayName> [create|delete]"); return 1; } gServiceName = argv[1]; gServiceDisplayName = argv[2]; if (argc == 3) { // When run with just a service name, just run as a service SERVICE_TABLE_ENTRY DispatchTable [] = { { gServiceName, (LPSERVICE_MAIN_FUNCTION) ServiceMain }, { NULL, NULL } }; // This call returns when the service has stopped. // The process should simply terminate when the call returns. if (!StartServiceCtrlDispatcher(DispatchTable)) { LogMessage(L"error: StartServiceCtrlDispatcher failed (%d)\n", GetLastError()); } } else if (argc == 4) { if (!InitModulePath()) { return -1; } GenerateDependentServiceNames(); std::wstring action = argv[3]; if (action == L"create") { if (!CreateTestServices()) { wprintf(L"error: Creating the test services failed\n"); DeleteTestServices(); return -1; } } else if (action == L"delete") { if (!DeleteTestServices()) { wprintf(L"error: Deleting the test services failed\n"); return -1; } } else { wprintf(L"error: Invalid action '%s'\n", action.c_str()); return -1; } } return 0; }
// // Purpose: // The service code // // Parameters: // dwArgc - Number of arguments in the lpszArgv array // lpszArgv - Array of strings. The first string is the name of // the service and subsequent strings are passed by the process // that called the StartService function to start the service. // // Return value: // None // VOID ServiceInit(DWORD dwArgc, LPTSTR* lpszArgv) { // Create an event. The control handler function, ServiceCtrlHandler, // signals this event when it receives the stop control code. ghServiceStopEvent = CreateEvent( NULL, // default security attributes TRUE, // manual reset event FALSE, // not signaled NULL); // no name if (ghServiceStopEvent == NULL) { ServiceReportStatus(SERVICE_STOPPED, NO_ERROR, 0); return; } InitModulePath(); CreateLogFile(); // Write the service arguments to the registry key: // HKEY_USERS\.DEFAULT\dotnetTests\ServiceController\<ServiceName>\ServiceArguments // to verify that they were correctly passed through. std::wstring keyPath = L".DEFAULT\\dotnetTests\\ServiceController\\"; keyPath += gServiceName; HKEY hKey; LONG result = RegCreateKeyEx( HKEY_USERS, keyPath.c_str(), 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); if (result != ERROR_SUCCESS) { LogMessage(L"warning: failed to open or create registry key 'HKEY_USERS\\%s' (%d)\n", keyPath.c_str(), result); } else { // Join the arguments array, separating each argument with a comma std::wstring argsString; DWORD i = 1; for (; i < dwArgc - 1; i++) { argsString += lpszArgv[i]; argsString += L','; } if (i < dwArgc) { argsString += lpszArgv[i]; } // Write the result to the value "ServiceArguments" LPCTSTR valueName = L"ServiceArguments"; result = RegSetValueEx( hKey, valueName, 0, REG_SZ, (const BYTE*) argsString.c_str(), (DWORD) ((argsString.length() + 1) * sizeof(wchar_t))); if (result != ERROR_SUCCESS) { LogMessage(L"warning: failed to set value '%s' = '%s' in registry key 'HKEY_USERS\\%s' (%d)\n", valueName, argsString.c_str(), keyPath.c_str(), result); } RegCloseKey(hKey); } // Report running status when initialization is complete. ServiceReportStatus(SERVICE_RUNNING, NO_ERROR, 0); while (1) { // Check whether to stop the service. // If the tests haven't finished within 90 seconds, just end the program anyways. DWORD error = WaitForSingleObject(ghServiceStopEvent, 90000); // We're stopping, delete the log file DWORD logError = DeleteLogFile(); // If WaitForSingleObject fails, use that code. // Otherwise use the result of DeleteLogFile. if (error == ERROR_SUCCESS) { error = logError; } ServiceReportStatus(SERVICE_STOPPED, error, 0); return; } }