| 126 | |
| 127 | |
| 128 | BOOL LoadKernelDriver() { |
| 129 | SC_HANDLE hSCManager = NULL; |
| 130 | SC_HANDLE hService = NULL; |
| 131 | LPCWSTR driverName = g_Config.driverName; |
| 132 | LPCWSTR driverPath = g_Config.driverPath; |
| 133 | BOOL ret = FALSE; |
| 134 | |
| 135 | // Open the Service Control Manager |
| 136 | hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
| 137 | if (!hSCManager) { |
| 138 | LOG_A(LOG_ERROR, "Kernel: OpenSCManager failed. Error: %lu", GetLastError()); |
| 139 | return FALSE; |
| 140 | } |
| 141 | |
| 142 | // Create the service (driver) |
| 143 | hService = CreateService( |
| 144 | hSCManager, // SCM handle |
| 145 | driverName, // Name of the service |
| 146 | driverName, // Display name |
| 147 | SERVICE_ALL_ACCESS, // Desired access |
| 148 | SERVICE_KERNEL_DRIVER, // Service type (kernel driver) |
| 149 | SERVICE_DEMAND_START, // Start type (on demand) |
| 150 | SERVICE_ERROR_NORMAL, // Error control type |
| 151 | driverPath, // Path to the driver executable |
| 152 | NULL, // No load ordering group |
| 153 | NULL, // No tag identifier |
| 154 | NULL, // No dependencies |
| 155 | NULL, // LocalSystem account |
| 156 | NULL // No password |
| 157 | ); |
| 158 | |
| 159 | if (!hService) { |
| 160 | if (GetLastError() == ERROR_SERVICE_EXISTS) { |
| 161 | LOG_A(LOG_INFO, "Kernel: Service already exists. Opening existing service..."); |
| 162 | hService = OpenService(hSCManager, driverName, SERVICE_ALL_ACCESS); |
| 163 | if (!hService) { |
| 164 | LOG_A(LOG_ERROR, "Kernel: OpenService failed. Error: %lu", GetLastError()); |
| 165 | ret = FALSE; |
| 166 | goto cleanup; |
| 167 | } |
| 168 | } |
| 169 | else { |
| 170 | LOG_A(LOG_ERROR, "Kernel: CreateService failed. Error: %lu", GetLastError()); |
| 171 | ret = FALSE; |
| 172 | goto cleanup; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Start the service (load the driver) |
| 177 | if (!StartService(hService, 0, NULL)) { |
| 178 | if (GetLastError() != ERROR_SERVICE_ALREADY_RUNNING) { |
| 179 | LOG_A(LOG_ERROR, "Kernel: StartService failed. Error: %lu", GetLastError()); |
| 180 | ret = FALSE; |
| 181 | goto cleanup; |
| 182 | } |
| 183 | else { |
| 184 | ret = TRUE; // Service already running should be success |
| 185 | LOG_A(LOG_INFO, "Kernel: Service already running."); |
no test coverage detected