1,这个程序正确编译,生成.exe文件
2,在命令行 cs create成功, 在控制面板上可以看到此服务
3,手动启动这个服务,任务管理器可以看到服务程序
问题:
serviceMain不能执行,当然我自己的代码也就不能执行了;
代码如下
void MyFun()
{
for (int a =0; a<10; a++)
::MessageBoxA(NULL, "ggg", "ghgh" ,MB_OK);
}
SERVICE_STATUS ServiceStatus =
{
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
SERVICE_STOPPED,
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_PAUSE_CONTINUE,
0,0,0,0
};
TCHAR szSvName[]=TEXT("Myservice1");
SERVICE_STATUS_HANDLE hStatus;
SC_HANDLE SCMger;
void WINAPI ControlHandler(DWORD request);
void ServiceMain(int argc, char* argv);
void CreateMyService()
{
SCMger=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
CreateService(
SCMger,
szSvName,
szSvName,
SERVICE_START,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_IGNORE,
TEXT("c:\\myservice1.exe"),
NULL,
NULL,
NULL,
NULL,
NULL);
}
int main()
{
SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceName = szSvName;
ServiceTable[0].lpServiceProc=(LPSERVICE_MAIN_FUNCTION)ServiceMain;
ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc= NULL;
if(StartServiceCtrlDispatcher(ServiceTable)==0)CreateMyService();
}
//不能调试,执行
void ServiceMain(int argc, char* argv)
{
hStatus = RegisterServiceCtrlHandler(szSvName,ControlHandler);
ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN;
SetServiceStatus(hStatus,&ServiceStatus);
ServiceStatus.dwWin32ExitCode = NO_ERROR;
ServiceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(hStatus,&ServiceStatus);
Myfun();
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hStatus,&ServiceStatus);
}
//this fun can't be debug,can't run
void WINAPI ControlHandler(DWORD request)
{
switch(request)
{
case SERVICE_CONTROL_SHUTDOWN:
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hStatus,&ServiceStatus);
break;
}
}
------解决方案--------------------------------------------------------
自己顶吧
------解决方案--------------------------------------------------------
没有人回答啊, StartServiceCtrlDispatcher处发生1063错误
------解决方案--------------------------------------------------------
问题解决了,马上准备结贴子
------解决方案--------------------------------------------------------
问题怎么解决的呀?分享一下呗