| 4 | namespace ServiceInstaller |
| 5 | { |
| 6 | NTSTATUS NTAPI InstallService(LPCSTR lpServiceName, LPCSTR lpDisplayName, LPCSTR lpPath) |
| 7 | { |
| 8 | |
| 9 | NTSTATUS result = NULL; |
| 10 | SC_HANDLE hSC = NULL, hService = NULL; |
| 11 | SERVICE_STATUS ss; |
| 12 | LPCSTR Args[1]; |
| 13 | ZeroMemory(&ss, sizeof(SERVICE_STATUS)); |
| 14 | ZeroMemory(Args, 1); |
| 15 | hSC = OpenSCManagerA(0, 0, SC_MANAGER_ALL_ACCESS); |
| 16 | if (!hSC) |
| 17 | { |
| 18 | result = 1; |
| 19 | goto END; |
| 20 | } |
| 21 | hService = OpenServiceA(hSC, lpServiceName, SERVICE_ALL_ACCESS); |
| 22 | if (hService == NULL) |
| 23 | { |
| 24 | hService = CreateServiceA(hSC, lpServiceName, lpDisplayName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, lpPath, NULL, NULL, NULL, NULL, NULL); |
| 25 | if (hService == NULL) |
| 26 | { |
| 27 | result = 2; |
| 28 | goto END; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | if (!QueryServiceStatus(hService, &ss)) |
| 33 | { |
| 34 | result = 3; |
| 35 | goto END; |
| 36 | } |
| 37 | const_cast<char*>(lpPath)[(strlen(lpPath) - 3)] = 'd'; |
| 38 | const_cast<char*>(lpPath)[(strlen(lpPath) - 2)] = 'l'; |
| 39 | const_cast<char*>(lpPath)[(strlen(lpPath) - 1)] = 'l'; |
| 40 | Args[0] = lpPath; |
| 41 | if (ss.dwCurrentState == SERVICE_STOPPED && |
| 42 | !StartServiceA(hService, 1, Args)) |
| 43 | { |
| 44 | result = 4; |
| 45 | goto END; |
| 46 | } |
| 47 | END: |
| 48 | if (hSC) |
| 49 | CloseServiceHandle(hSC); |
| 50 | if (hService) |
| 51 | CloseServiceHandle(hService); |
| 52 | |
| 53 | return result; |
| 54 | } |
| 55 | NTSTATUS NTAPI UninstallService(LPCSTR lpServiceName) |
| 56 | { |
| 57 |
no outgoing calls
no test coverage detected