| 207 | |
| 208 | |
| 209 | BOOL UnloadKernelDriver() { |
| 210 | SC_HANDLE hSCManager = NULL; |
| 211 | SC_HANDLE hService = NULL; |
| 212 | SERVICE_STATUS status; |
| 213 | LPCWSTR driverName = g_Config.driverName; |
| 214 | BOOL ret = FALSE; |
| 215 | |
| 216 | hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
| 217 | if (!hSCManager) { |
| 218 | LOG_A(LOG_ERROR, "Kernel: OpenSCManager failed. Error: %lu", GetLastError()); |
| 219 | return FALSE; |
| 220 | } |
| 221 | |
| 222 | hService = OpenService(hSCManager, driverName, SERVICE_STOP | DELETE | SERVICE_QUERY_STATUS); |
| 223 | if (!hService) { |
| 224 | LOG_A(LOG_ERROR, "Kernel: OpenService failed. Error: %lu", GetLastError()); |
| 225 | ret = FALSE; |
| 226 | goto cleanup; |
| 227 | } |
| 228 | |
| 229 | if (ControlService(hService, SERVICE_CONTROL_STOP, &status)) { |
| 230 | LOG_A(LOG_INFO, "Kernel: Service stopped successfully."); |
| 231 | ret = TRUE; |
| 232 | } |
| 233 | else if (GetLastError() == ERROR_SERVICE_NOT_ACTIVE) { |
| 234 | LOG_A(LOG_INFO, "Kernel: Service is not running."); |
| 235 | ret = TRUE; |
| 236 | } |
| 237 | else { |
| 238 | LOG_A(LOG_ERROR, "Kernel: ControlService failed. Error: %lu", GetLastError()); |
| 239 | ret = FALSE; |
| 240 | goto cleanup; |
| 241 | } |
| 242 | |
| 243 | if (!DeleteService(hService)) { |
| 244 | LOG_A(LOG_ERROR, "Kernel: DeleteService failed. Error: %lu", GetLastError()); |
| 245 | ret = FALSE; |
| 246 | goto cleanup; |
| 247 | } |
| 248 | else { |
| 249 | LOG_A(LOG_INFO, "Kernel: Service deleted successfully."); |
| 250 | } |
| 251 | |
| 252 | cleanup: |
| 253 | if (hService) CloseServiceHandle(hService); |
| 254 | if (hSCManager) CloseServiceHandle(hSCManager); |
| 255 | |
| 256 | return ret; |
| 257 | } |