Services::LoadDriver - register a service and load a driver given a driverName and driverPath using SCM return `true` on success */
| 705 | return `true` on success |
| 706 | */ |
| 707 | bool Services::LoadDriver(__in const std::wstring& serviceName, __in const std::wstring& driverPath) |
| 708 | { |
| 709 | SC_HANDLE hSCManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE); |
| 710 | |
| 711 | bool loadSuccess = true; |
| 712 | |
| 713 | if (!hSCManager) |
| 714 | { |
| 715 | Logger::logfw(Warning, L"Failed to open SCM: %d", GetLastError()); |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | SC_HANDLE hService = CreateService( //create driver service |
| 720 | hSCManager, |
| 721 | serviceName.c_str(), |
| 722 | serviceName.c_str(), |
| 723 | SERVICE_START | DELETE | SERVICE_STOP, |
| 724 | SERVICE_KERNEL_DRIVER, |
| 725 | SERVICE_DEMAND_START, |
| 726 | SERVICE_ERROR_IGNORE, |
| 727 | driverPath.c_str(), |
| 728 | nullptr, nullptr, nullptr, nullptr, nullptr); |
| 729 | |
| 730 | if (!hService) |
| 731 | { |
| 732 | if (GetLastError() == ERROR_SERVICE_EXISTS) |
| 733 | { |
| 734 | hService = OpenService(hSCManager, serviceName.c_str(), SERVICE_START); |
| 735 | } |
| 736 | else |
| 737 | { |
| 738 | Logger::logfw(Warning, L"Failed to create service: %d", GetLastError()); |
| 739 | CloseServiceHandle(hSCManager); |
| 740 | return false; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | if (!StartService(hService, 0, nullptr)) // Start the driver |
| 745 | { |
| 746 | if (GetLastError() != ERROR_SERVICE_ALREADY_RUNNING) |
| 747 | { |
| 748 | Logger::logfw(Warning, L"Failed to start service: %d", GetLastError()); |
| 749 | loadSuccess = false; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if(loadSuccess) |
| 754 | Logger::logfw(Info, L"Driver %s loaded successfully.", serviceName.c_str()); |
| 755 | |
| 756 | CloseServiceHandle(hService); |
| 757 | CloseServiceHandle(hSCManager); |
| 758 | return loadSuccess; |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | Services::UnloadDriver - unregister the driver service and unload a driver given a driverName and driverPath using SCM |
nothing calls this directly
no outgoing calls
no test coverage detected