Windows service work, remove old installer... not sure exactly what we're going to use.
This commit is contained in:
@@ -42,20 +42,21 @@
|
||||
// NOTE: If the function fails to install the service, it prints the error
|
||||
// in the standard output stream for users to diagnose the problem.
|
||||
//
|
||||
void InstallService(PSTR pszServiceName,
|
||||
std::string InstallService(PSTR pszServiceName,
|
||||
PSTR pszDisplayName,
|
||||
DWORD dwStartType,
|
||||
PSTR pszDependencies,
|
||||
PSTR pszAccount,
|
||||
PSTR pszPassword)
|
||||
{
|
||||
std::string ret;
|
||||
char szPath[MAX_PATH];
|
||||
SC_HANDLE schSCManager = NULL;
|
||||
SC_HANDLE schService = NULL;
|
||||
|
||||
if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)) == 0)
|
||||
{
|
||||
wprintf(L"GetModuleFileName failed w/err 0x%08lx\n", GetLastError());
|
||||
ret = "GetModuleFileName failed, unable to get path to self";
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
@@ -64,7 +65,7 @@ void InstallService(PSTR pszServiceName,
|
||||
SC_MANAGER_CREATE_SERVICE);
|
||||
if (schSCManager == NULL)
|
||||
{
|
||||
wprintf(L"OpenSCManager failed w/err 0x%08lx\n", GetLastError());
|
||||
ret = "OpenSCManager failed";
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
@@ -86,12 +87,10 @@ void InstallService(PSTR pszServiceName,
|
||||
);
|
||||
if (schService == NULL)
|
||||
{
|
||||
wprintf(L"CreateService failed w/err 0x%08lx\n", GetLastError());
|
||||
ret = "CreateService failed";
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
wprintf(L"%s is installed.\n", pszServiceName);
|
||||
|
||||
Cleanup:
|
||||
// Centralized cleanup for all allocated resources.
|
||||
if (schSCManager)
|
||||
@@ -104,6 +103,8 @@ Cleanup:
|
||||
CloseServiceHandle(schService);
|
||||
schService = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -119,8 +120,9 @@ Cleanup:
|
||||
// NOTE: If the function fails to uninstall the service, it prints the
|
||||
// error in the standard output stream for users to diagnose the problem.
|
||||
//
|
||||
void UninstallService(PSTR pszServiceName)
|
||||
std::string UninstallService(PSTR pszServiceName)
|
||||
{
|
||||
std::string ret;
|
||||
SC_HANDLE schSCManager = NULL;
|
||||
SC_HANDLE schService = NULL;
|
||||
SERVICE_STATUS ssSvcStatus = {};
|
||||
@@ -129,7 +131,7 @@ void UninstallService(PSTR pszServiceName)
|
||||
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
|
||||
if (schSCManager == NULL)
|
||||
{
|
||||
wprintf(L"OpenSCManager failed w/err 0x%08lx\n", GetLastError());
|
||||
ret = "OpenSCManager failed";
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
@@ -138,45 +140,32 @@ void UninstallService(PSTR pszServiceName)
|
||||
SERVICE_QUERY_STATUS | DELETE);
|
||||
if (schService == NULL)
|
||||
{
|
||||
wprintf(L"OpenService failed w/err 0x%08lx\n", GetLastError());
|
||||
ret = "OpenService failed (is service installed?)";
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
// Try to stop the service
|
||||
if (ControlService(schService, SERVICE_CONTROL_STOP, &ssSvcStatus))
|
||||
{
|
||||
wprintf(L"Stopping %s.", pszServiceName);
|
||||
Sleep(1000);
|
||||
Sleep(500);
|
||||
|
||||
while (QueryServiceStatus(schService, &ssSvcStatus))
|
||||
{
|
||||
if (ssSvcStatus.dwCurrentState == SERVICE_STOP_PENDING)
|
||||
{
|
||||
wprintf(L".");
|
||||
Sleep(1000);
|
||||
Sleep(500);
|
||||
}
|
||||
else break;
|
||||
}
|
||||
|
||||
if (ssSvcStatus.dwCurrentState == SERVICE_STOPPED)
|
||||
{
|
||||
wprintf(L"\n%s is stopped.\n", pszServiceName);
|
||||
}
|
||||
else
|
||||
{
|
||||
wprintf(L"\n%s failed to stop.\n", pszServiceName);
|
||||
}
|
||||
}
|
||||
|
||||
// Now remove the service by calling DeleteService.
|
||||
if (!DeleteService(schService))
|
||||
{
|
||||
wprintf(L"DeleteService failed w/err 0x%08lx\n", GetLastError());
|
||||
ret = "DeleteService failed (is service running?)";
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
wprintf(L"%s is removed.\n", pszServiceName);
|
||||
|
||||
Cleanup:
|
||||
// Centralized cleanup for all allocated resources.
|
||||
if (schSCManager)
|
||||
@@ -189,4 +178,6 @@ Cleanup:
|
||||
CloseServiceHandle(schService);
|
||||
schService = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user