BOOL CNTEventLogSource::SetStringArrayIntoRegistry(ATL::CRegKey& key, LPCTSTR lpszEntry, const CNTServiceStringArray& array, DWORD* pLastError) { //Validate our input parameters ATLASSERT(lpszEntry != NULL); //Work out the size of the buffer we will need #ifdef CNTSERVICE_MFC_EXTENSIONS int nSize = 0; INT_PTR nStrings = array.GetSize(); for (INT_PTR i=0; i<nStrings; i++) nSize += array.GetAt(i).GetLength() + 1; //1 extra for each NULL terminator #else CNTServiceStringArray::size_type nSize = 0; CNTServiceStringArray::size_type nStrings = array.size(); for (CNTServiceStringArray::size_type i=0; i<nStrings; i++) nSize += array[i].length() + 1; //1 extra for each NULL terminator #endif //Need one second NULL for the double NULL at the end nSize++; //Allocate some memory for the API ATL::CHeapPtr<TCHAR> lpBuffer; if (!lpBuffer.Allocate(nSize)) { SetLastError(ERROR_OUTOFMEMORY); if (pLastError) *pLastError = ERROR_OUTOFMEMORY; return FALSE; } //Now copy the strings into the buffer LPTSTR lpszString = lpBuffer.m_pData; #ifdef CNTSERVICE_MFC_EXTENSIONS int nCurOffset = 0; for (INT_PTR i=0; i<nStrings; i++) #else CNTServiceStringArray::size_type nCurOffset = 0; for (CNTServiceStringArray::size_type i=0; i<nStrings; i++) #endif { const CNTServiceString& sText = array[i]; #ifdef CNTSERVICE_MFC_EXTENSIONS int nCurrentStringLength = sText.GetLength(); _tcscpy_s(&lpszString[nCurOffset], nCurrentStringLength+1, sText); #else CNTServiceString::size_type nCurrentStringLength = sText.length(); _tcscpy_s(&lpszString[nCurOffset], nCurrentStringLength+1, sText.c_str()); #endif nCurOffset += (nCurrentStringLength + 1); } //Don't forgot to doubly NULL terminate lpszString[nCurOffset] = _T('\0'); //Finally write it into the registry LONG lResult = key.SetMultiStringValue(lpszEntry, lpBuffer); BOOL bSuccess = (lResult == ERROR_SUCCESS); if (!bSuccess && pLastError) *pLastError = lResult; return bSuccess; }
HRESULT CreateGreenShieldService(const wchar_t* szDllPath) { TSAUTO(); SC_HANDLE schSCManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL) { DWORD dwOpenError = ::GetLastError(); TSERROR4CXX("OpenSCManager failed. Error: " << dwOpenError); return HRESULT_FROM_WIN32(dwOpenError); } ScopeResourceHandle<SC_HANDLE, BOOL (WINAPI*)(SC_HANDLE)> autoCloseSCManagerHandle(schSCManager, ::CloseServiceHandle); const wchar_t* szImagePath = L"%SystemRoot%\\System32\\svchost.exe -k ADCleanService"; SC_HANDLE schService = ::CreateService( schSCManager, szServiceName, szServiceName, SERVICE_ALL_ACCESS, SERVICE_WIN32_SHARE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, szImagePath, NULL, NULL, NULL, NULL, NULL); if(schService == NULL) { DWORD dwCreateError = ::GetLastError(); TSERROR4CXX("CreateService failed. Error: " << dwCreateError); return HRESULT_FROM_WIN32(dwCreateError); } ScopeResourceHandle<SC_HANDLE, BOOL (WINAPI*)(SC_HANDLE)> autoCloseServiceHandle(schService, ::CloseServiceHandle); SERVICE_DESCRIPTION description = { L"广告清道夫实时过滤服务。" }; ChangeServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, &description); // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\ADCleanService ATL::CRegKey key; LONG lRegResult = key.Open(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\services\\ADCleanService", KEY_READ); if(lRegResult != ERROR_SUCCESS) { TSERROR4CXX("Filed to open reg key. Key: HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\ADCleanService. Error: " << lRegResult); return HRESULT_FROM_WIN32(lRegResult); } key.Close(); // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\ADCleanService\Parameters lRegResult = key.Create(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\services\\ADCleanService\\Parameters"); if(lRegResult != ERROR_SUCCESS) { TSERROR4CXX("Failed to create reg key. Key: HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\ADCleanService\\Parameters. Error: " << lRegResult); return HRESULT_FROM_WIN32(lRegResult); } lRegResult = key.SetStringValue(L"ServiceDll", szDllPath, REG_EXPAND_SZ); if(lRegResult != ERROR_SUCCESS) { TSERROR4CXX("Failed to set reg value. Key: HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\ADCleanService\\Parameters\\ServiceDll. Error: " << lRegResult); return HRESULT_FROM_WIN32(lRegResult); } key.Close(); // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost lRegResult = key.Open(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Svchost"); if(lRegResult != ERROR_SUCCESS) { TSERROR4CXX("Failed to open reg key. Key: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Svchost. Error: " << lRegResult); return HRESULT_FROM_WIN32(lRegResult); } lRegResult = key.SetMultiStringValue(L"ADCleanService", L"ADCleanService\0"); if(lRegResult != ERROR_SUCCESS) { TSERROR4CXX("Failed to set reg value. Key: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Svchost\\ADCleanService. Error: " << lRegResult); return HRESULT_FROM_WIN32(lRegResult); } // start service ::StartService(schService, 0, NULL); return S_OK; }