我按照教程(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查询TestSvc)并停止(sc停止TestSvc)。 如果我在main函数的开头添加一个自定义函数,然后我调用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;
}
所以,基本上,如果我添加安装功能,服务安装正确,但我无法正确启动它。我得到的错误是服务没有正确响应启动命令。 我错过了什么?
答案 0 :(得分:1)
审核了您提供的文章参考,以及本文:Writing a ServiceMain Function。
根据Microsoft文章对您的代码进行了一些细微的更改,但无济于事。
然后,注意到这个(原始代码):
if( _strcmpi( argv[1], "install") == 0 )
{
SvcInstall();
return;
}
并意识到,如果没有.exe
的参数,就像您作为服务运行时一样,对argv[1]
的引用将超出范围,并且会使您的应用程序崩溃。
这个小小的变化:
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;
}
答案 1 :(得分:0)
&amp; ServiceMain是对函数的引用。如果是这样,那么它必须传递一些参数,并且handler_ex返回no_error而不是必须传递给g_ServiceStatusHandle的句柄。可能我错了吗?