gpt4 book ai didi

c - 服务放入安装功能后无法启动

转载 作者:行者123 更新时间:2023-11-30 14:49:37 25 4
gpt4 key购买 nike

我按照教程 (http://asawicki.info/news_1404_coding_windows_services_in_c.html) 用 C++ 创建了一个基本的 Windows 服务。这是我整理的代码:

#include <Windows.h>
#include <iostream>

using namespace std;

CHAR SvcName[] = "TestSvc";
CHAR SvcDesc[] = "This is a test service";
SERVICE_STATUS_HANDLE g_ServiceStatusHandle;
HANDLE g_StopEvent;
DWORD g_CurrentState = 0;
bool g_SystemShutdown = false;

void ReportStatus(DWORD state)
{
g_CurrentState = state;
SERVICE_STATUS serviceStatus = {
SERVICE_WIN32_OWN_PROCESS,
g_CurrentState,
state == SERVICE_START_PENDING ? 0 : SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN,
NO_ERROR,
0,
0,
0,
};
SetServiceStatus(g_ServiceStatusHandle, &serviceStatus);
}

DWORD WINAPI HandlerEx(DWORD control, DWORD eventType, void *eventData, void *context)
{
switch (control)
{
case SERVICE_CONTROL_SHUTDOWN:
g_SystemShutdown = true;

case SERVICE_CONTROL_STOP:
ReportStatus(SERVICE_STOP_PENDING);
SetEvent(g_StopEvent);
break;

default:
ReportStatus(g_CurrentState);
break;
}

return NO_ERROR;
}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
g_ServiceStatusHandle = RegisterServiceCtrlHandlerEx(SvcName, &HandlerEx, NULL);

ReportStatus(SERVICE_START_PENDING);
g_StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ReportStatus(SERVICE_RUNNING);

while (WaitForSingleObject(g_StopEvent, 3000) != WAIT_OBJECT_0)
{
do_some_work(); // Just a sample function that does nothing at all
}

ReportStatus(SERVICE_STOP_PENDING);
CloseHandle(g_StopEvent);
ReportStatus(SERVICE_STOPPED);
}

int main(int argc, char **argv)
{
SERVICE_TABLE_ENTRY serviceTable[] = {
{ SvcName, &ServiceMain },
{ NULL, NULL }
};

if (StartServiceCtrlDispatcher(serviceTable))
return 0;
else if (GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
return -1;
else
return -2;
}

如果我使用 sc create binpath = "C:...path...\TestSvc.exe",则效果很好。它可以正确启动,并且可以正确查询(sc query TestSvc)和停止(sc stop TestSvc)。如果我在主函数的开头添加一个自定义函数,然后调用 TestSvc.exe install,它会说它正确安装了该服务,但我无法启动。

VOID SvcInstall()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
TCHAR szPath[MAX_PATH];

if( !GetModuleFileName( NULL, szPath, MAX_PATH ) )
{
printf("Cannot install service (%d)\n", GetLastError());
return;
}

schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights

if (NULL == schSCManager)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return;
}

// Create the service

schService = CreateService(
schSCManager, // SCM database
SvcName, // name of service
SvcName, // 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
szPath, // 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)
{
printf("CreateService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
else printf("Service installed successfully\n");

CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}

int main(int argc, char **argv)
{
if( _strcmpi( argv[1], "install") == 0 )
{
SvcInstall();
return;
}

SERVICE_TABLE_ENTRY serviceTable[] = {
{ SvcName, &ServiceMain },
{ NULL, NULL }
};

if (StartServiceCtrlDispatcher(serviceTable))
return 0;
else if (GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
return -1;
else
return -2;
}

所以,基本上来说,如果我添加安装功能,服务会正确安装,但我无法正确启动它。我得到的错误是服务没有正确响应启动命令。我错过了什么?

最佳答案

查看了您提供的文章引用以及本文:Writing a ServiceMain Function .

根据 Microsoft 文章对代码进行了一些细微的更改,但没有效果。

然后,注意到这个(原始代码):

if( _strcmpi( argv[1], "install") == 0 )
{
SvcInstall();
return;
}

并意识到,如果.exe没有参数,就像当您作为服务运行时一样,对argv[1]的引用将是out-of-边界并且会有效地使您的应用程序崩溃。

这个小变化:

if (argc > 1 && _strcmpi(argv[1], "install") == 0)

应该可以解决您的问题。

我将完整发布您的原始代码,并进行了一些细微的修改,主要受我引用的 Microsoft 文章的影响,您可以根据需要采用或保留。

#include <Windows.h>
#include <iostream>

using namespace std;

CHAR SvcName[] = "TestSvc";
CHAR SvcDesc[] = "This is a test service";
SERVICE_STATUS_HANDLE g_ServiceStatusHandle;
HANDLE g_StopEvent;
DWORD g_CurrentState = 0;
bool g_SystemShutdown = false;

void ReportStatus(const DWORD state)
{
static DWORD dwCheckPoint = 1;

g_CurrentState = state;

SERVICE_STATUS serviceStatus = {
SERVICE_WIN32_OWN_PROCESS,
g_CurrentState,
state == SERVICE_START_PENDING ? 0 : SERVICE_ACCEPT_STOP,
NO_ERROR,
0,
state == SERVICE_RUNNING || state == SERVICE_STOPPED ? 0 : dwCheckPoint++,
state == SERVICE_START_PENDING ? 3000 : 0,
};
SetServiceStatus(g_ServiceStatusHandle, &serviceStatus);
}

DWORD WINAPI HandlerEx(DWORD control, DWORD eventType, void *eventData, void *context)
{
switch (control)
{
case SERVICE_CONTROL_SHUTDOWN:
g_SystemShutdown = true;

case SERVICE_CONTROL_STOP:
ReportStatus(SERVICE_STOP_PENDING);
SetEvent(g_StopEvent);
break;

default:
ReportStatus(g_CurrentState);
break;
}

return NO_ERROR;
}

void do_some_work()
{
}

VOID SvcInstall()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
TCHAR szPath[MAX_PATH];

if (!GetModuleFileName(NULL, szPath, MAX_PATH))
{
printf("Cannot install service (%lu)\n", GetLastError());
return;
}

schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights

if (NULL == schSCManager)
{
printf("OpenSCManager failed (%lu)\n", GetLastError());
return;
}

// Create the service

schService = CreateService(
schSCManager, // SCM database
SvcName, // name of service
SvcName, // 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
szPath, // 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)
{
printf("CreateService failed (%lu)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
else printf("Service installed successfully\n");

CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
g_ServiceStatusHandle = RegisterServiceCtrlHandlerEx(SvcName, &HandlerEx, NULL);

ReportStatus(SERVICE_START_PENDING);
g_StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ReportStatus(SERVICE_RUNNING);

while (WaitForSingleObject(g_StopEvent, 3000) != WAIT_OBJECT_0)
{
do_some_work(); // Just a sample function that does nothing at all
}

ReportStatus(SERVICE_STOP_PENDING);
CloseHandle(g_StopEvent);
ReportStatus(SERVICE_STOPPED);
}

int main(int argc, char **argv)
{
if (argc > 1 && _strcmpi(argv[1], "install") == 0)
{
SvcInstall();
return 0;
}

SERVICE_TABLE_ENTRY serviceTable[] = {
{ SvcName, &ServiceMain },
{ NULL, NULL }
};

if (StartServiceCtrlDispatcher(serviceTable))
return 0;
else if (GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
return -1;
else
return -2;
}

关于c - 服务放入安装功能后无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49447260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com