| 73 | } |
| 74 | |
| 75 | bool CServiceHelper::LoadService(DWORD dwServiceType, LPDWORD pdwErrorCode) |
| 76 | { |
| 77 | auto bRet = false; |
| 78 | auto hSCManager = SC_HANDLE(nullptr); |
| 79 | auto hService = SC_HANDLE(nullptr); |
| 80 | |
| 81 | if (g_nmApp->DirFunctionsInstance()->IsFileExist(m_szServicePath) == false) |
| 82 | { |
| 83 | DEBUG_LOG(LL_ERR, "Target file: %s is not exist!", m_szServicePath.c_str()); |
| 84 | if (pdwErrorCode) *pdwErrorCode = g_winapiApiTable->GetLastError(); |
| 85 | goto _Complete; |
| 86 | } |
| 87 | |
| 88 | hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_CREATE_SERVICE); |
| 89 | if (!hSCManager) |
| 90 | { |
| 91 | DEBUG_LOG(LL_ERR, "OpenSCManager fail! Error: %u", g_winapiApiTable->GetLastError()); |
| 92 | if (pdwErrorCode) *pdwErrorCode = g_winapiApiTable->GetLastError(); |
| 93 | goto _Complete; |
| 94 | } |
| 95 | |
| 96 | hService = CreateServiceA(hSCManager, m_szServiceName.c_str(), m_szDisplayName.c_str(), SERVICE_ALL_ACCESS, dwServiceType, |
| 97 | SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, m_szServicePath.c_str(), dwServiceType == SERVICE_FILE_SYSTEM_DRIVER ? "FSFilter Activity Monitor" : NULL, |
| 98 | NULL, NULL, NULL, NULL |
| 99 | ); |
| 100 | if (!hService) |
| 101 | { |
| 102 | auto dwError = g_winapiApiTable->GetLastError(); |
| 103 | DEBUG_LOG(LL_ERR, "CreateServiceA fail! Error: %u", dwError); |
| 104 | if (pdwErrorCode) *pdwErrorCode = dwError; |
| 105 | goto _Complete; |
| 106 | } |
| 107 | |
| 108 | bRet = true; |
| 109 | |
| 110 | _Complete: |
| 111 | if (bRet == false && hService) |
| 112 | PrintDetailedLog(hService); |
| 113 | |
| 114 | if (hSCManager) |
| 115 | { |
| 116 | CloseServiceHandle(hSCManager); |
| 117 | hSCManager = nullptr; |
| 118 | } |
| 119 | if (hService) |
| 120 | { |
| 121 | CloseServiceHandle(hService); |
| 122 | hService = nullptr; |
| 123 | } |
| 124 | |
| 125 | return bRet; |
| 126 | } |
| 127 | |
| 128 | bool CServiceHelper::UnloadDriver(LPDWORD pdwErrorCode) |
| 129 | { |
no test coverage detected