在Windows上创建服务 [英] create service on windows
问题描述
我无法在我的电脑上开始我的服务。我的代码基于本文 http://www.gamedev.net/reference/articles/ article1899.asp
当我从int main(int argc,char * argv [])中调用installService时,它注册成功(我可以看到它在msconfig和services.msc)。但是它还没有开始。我通过services.msv手动启动服务,我得到错误错误2:系统找不到指定的文件。为什么是这样?我注册的服务没有更多,然后一分钟前,我的外部高清仍然(这是目前存储的。我会移动一个nondev版本到c:/当它准备好了)我做错了,是有另一个教程可以看看(我只发现通过google链接的一个)
#define srvNameMyTestService_01312009
void installService(char * path)
{
SC_HANDLE handle = :: OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
SC_HANDLE service = :: CreateService(
handle,
srvName,
MyTestService_01312009b,
GENERIC_READ | GENERIC_EXECUTE,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START ,
SERVICE_ERROR_IGNORE,
path,
NULL,
NULL,
NULL,
NULL,
NULL
);
}
void uninstallService()
{
SC_HANDLE handle = :: OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); //?
SC_HANDLE service = :: OpenService(handle,srvName,DELETE);
if(service!= NULL)
{
//删除服务!
:: DeleteService(service);
}
}
SERVICE_STATUS_HANDLE hStatus;
SERVICE_STATUS状态;
/ *
if(:: StartServiceCtrlDispatcher(dispatchTable)== 0)
{
//如果失败,可能是因为有人从
//命令行。打印一条消息告诉他们使用
}
* /
void WINAPI ServiceCtrlHandler(DWORD control)
{
switch(control)
{
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
//这里做关闭的东西
status.dwCurrentState = SERVICE_STOPPED;
status.dwWin32ExitCode = 0;
status.dwCheckPoint = 0;
status.dwWaitHint = 0;
break;
case SERVICE_CONTROL_INTERROGATE:
//只是将当前状态设置为任何值...
break;
}
:: SetServiceStatus(hStatus,& status);
}
void WINAPI ServiceDispatch(DWORD numArgs,char ** args)
{
//我们必须初始化服务特定的东西
memset(& status ,0,sizeof(SERVICE_STATUS));
status.dwServiceType = SERVICE_WIN32;
status.dwCurrentState = SERVICE_START_PENDING;
status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
hStatus = :: RegisterServiceCtrlHandler(srvName,& ServiceCtrlHandler);
//更多初始化的东西在这里
FILE * f = fopen(c:/testSrv.bin,wb);
:: SetServiceStatus(hStatus,& status);
}
SERVICE_TABLE_ENTRY dispatchTable [] =
{
{srvName,& ServiceDispatch},
{NULL,NULL}
};
也许你可以使用进程监视器找出有什么问题。
启动它,并查找与服务启动相关的结果 NAME NOT FOUND
。
I am having trouble starting my service on my pc. My code is based on this article http://www.gamedev.net/reference/articles/article1899.asp
When i call installService from my int main(int argc, char *argv[]), it is registered successfully (i can see it in msconfig and services.msc). However it has not started. I manually start the service via services.msv and i get the error "Error 2: system cannot find the file specified". Why is this? i registered the services no more then a min ago, my external HD is still on (where this is currently stored. i'll move a nondev version to c:/ when its ready) What am i doing wrong and is there another tutorial i can look at (i only found the one linked via google)
#define srvName "MyTestService_01312009"
void installService(char*path)
{
SC_HANDLE handle = ::OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
SC_HANDLE service = ::CreateService(
handle,
srvName,
"MyTestService_01312009b",
GENERIC_READ | GENERIC_EXECUTE,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_IGNORE,
path,
NULL,
NULL,
NULL,
NULL,
NULL
);
}
void uninstallService()
{
SC_HANDLE handle = ::OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );//?
SC_HANDLE service = ::OpenService( handle, srvName, DELETE );
if( service != NULL )
{
// remove the service!
::DeleteService( service );
}
}
SERVICE_STATUS_HANDLE hStatus;
SERVICE_STATUS status;
/*
if( ::StartServiceCtrlDispatcher( dispatchTable ) == 0 )
{
// if this fails, it's probably because someone started us from
// the command line. Print a message telling them the "usage"
}
*/
void WINAPI ServiceCtrlHandler( DWORD control )
{
switch( control )
{
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
// do shutdown stuff here
status.dwCurrentState = SERVICE_STOPPED;
status.dwWin32ExitCode = 0;
status.dwCheckPoint = 0;
status.dwWaitHint = 0;
break;
case SERVICE_CONTROL_INTERROGATE:
// just set the current state to whatever it is...
break;
}
::SetServiceStatus( hStatus, &status );
}
void WINAPI ServiceDispatch( DWORD numArgs, char **args )
{
// we have to initialize the service-specific stuff
memset( &status, 0, sizeof(SERVICE_STATUS) );
status.dwServiceType = SERVICE_WIN32;
status.dwCurrentState = SERVICE_START_PENDING;
status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
hStatus = ::RegisterServiceCtrlHandler( srvName, &ServiceCtrlHandler );
// more initialization stuff here
FILE *f = fopen("c:/testSrv.bin", "wb");
::SetServiceStatus( hStatus, &status );
}
SERVICE_TABLE_ENTRY dispatchTable[] =
{
{ srvName, &ServiceDispatch },
{ NULL, NULL }
};
Maybe you can use Process Monitor to find out what's wrong.
Start it, and look for NAME NOT FOUND
results that occur in connection with the service start.
这篇关于在Windows上创建服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!