我在C++
中有一个应用程序,它在运行时尝试(此时没有其他相关进程被认为正在运行),以安装一些服务。
工作流程是,如果服务存在,则将其卸载,然后卸载并重新安装。
我曾经遵循过几个教程: Installing a service
不幸的是,安装或卸载似乎出现了问题。
在假设未安装该服务时,当我继续安装服务时,它们会被安装,然后我尝试将配置更改为延迟自动启动。
在这个阶段,虽然安装了服务,但是我无法启动它们,因为我收到的错误是无法找到服务。
当我尝试卸载服务时,卸载失败,出现错误1060,表示服务已标记为删除。
SC_HANDLE schSCManager;
SC_HANDLE schService;
schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (schSCManager == nullptr)
{
continue;
}
if (bInstall)
{
schService = CreateService(
schSCManager, // SCM database
szServiceName, // name of service
szDisplayName, // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szDirectory, // path to service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService == NULL)
{
TraceAdvice(L"CreateService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
continue;
}
else
{
if (!ChangeServiceConfig(
schService, // handle of service
SERVICE_NO_CHANGE, // service type: no change
SERVICE_CONFIG_DELAYED_AUTO_START_INFO, // service start type
SERVICE_NO_CHANGE, // error control: no change
NULL, // binary path: no change
NULL, // load order group: no change
NULL, // tag ID: no change
NULL, // dependencies: no change
NULL, // account name: no change
NULL, // password: no change
NULL)) // display name: no change
{
TraceAdvice(L"ChangeServiceConfig failed (%d)\n", GetLastError());
}
TraceAdvice(L"Service installed successfully\n");
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
/*TraceFatal(_T("Now installing : %s"), szServiceName);
_stprintf_s(szTmp, _T("SC create %s binpath= \"%s%s.exe\" displayName= \"%s\" start= delayed-auto"), szServiceName, szDirectory, szServiceName, szDisplayName);
TraceFatal(_T("Command is : %s"), szTmp);*/
}
else
{
_wsystem(_T("taskkill /F /IM mmc.exe")); // Need to kill any instance of MMC running
_wsystem(_T("taskkill /F /IM procexp.exe"));
schService = OpenService(
schSCManager, // SCM database
szServiceName, // name of service
DELETE); // need delete access
if (schService == NULL)
{
TraceAdvice(L"OpenService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
continue;
}
DWORD dwBytesNeeded;
SERVICE_STATUS_PROCESS ssp;
if (!QueryServiceStatusEx(
schService,
SC_STATUS_PROCESS_INFO,
(LPBYTE)&ssp,
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded))
{
printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
}
if (ssp.dwCurrentState == SERVICE_RUNNING)
{
_stprintf_s(szTmp, _T("taskkill /F /IM %s.exe"), szServiceName);
_wsystem(szTmp);
}
// Delete the service.
if (!DeleteService(schService))
{
TraceAdvice(L"DeleteService failed (%d)\n", GetLastError());
}
else TraceAdvice(L"Service deleted successfully\n");
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
}
我确定了所有要点,即
但它没有用。在重新启动系统之前,服务不会被卸载(删除链接到服务的注册表也不起作用)。
我已经检查了传递给这些函数的参数,它们看起来是正确的。
我还应该检查什么,以确保正确安装或成功卸载?