[服务]错误1053:服务没有及时响应启动或控制请求,代码列出,帮忙.

qqwweerraahh 2009-05-04 03:29:21

#include<afx.h>

void WINAPI ServiceMain(); //服务主函数
void WINAPI ServiceStrl(DWORD); //服务控制
void Init(); //初始化
BOOL IsInstalled(); //判断服务是否已安装
BOOL Install(); //安装服务
BOOL UnInstall(); //卸载服务
void Action(); //服务任务
char chServiceName[]="MyService"; //服务名称
char chServiceDisplayName[]="MyService"; //服务显示名称
char chServiceDescription[]="My service description."; //服务描述
SERVICE_STATUS_HANDLE hServiceStatus; //服务句柄
SERVICE_STATUS ServiceStatus; //服务状态
DWORD dwThreadId; //进程ID

//程序主函数
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{

//初始化
Init();

//获取进程ID
dwThreadId=::GetCurrentThreadId();

//判断命令行参数,决定动作
if(stricmp(lpCmdLine,"/install")==0)
if(!IsInstalled())
if(!Install())
return 1;
else if(stricmp(lpCmdLine,"/uninstall")==0)
if(IsInstalled())
if(!UnInstall())
return 1;
else
{
SERVICE_TABLE_ENTRY ServiceTableEntry[]=
{
{chServiceName,(LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL,NULL}
};
::StartServiceCtrlDispatcher(ServiceTableEntry);
}
return 0;
}

//初始化
void Init()
{
hServiceStatus=NULL;
ServiceStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
ServiceStatus.dwCurrentState=SERVICE_STOPPED;
ServiceStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP;
ServiceStatus.dwWin32ExitCode=0;
ServiceStatus.dwServiceSpecificExitCode=0;
ServiceStatus.dwCheckPoint=0;
ServiceStatus.dwWaitHint=0;
}

//服务主函数
void WINAPI ServiceMain()
{
ServiceStatus.dwCurrentState=SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP;

//注册服务控制函数
hServiceStatus=RegisterServiceCtrlHandler(chServiceName,ServiceStrl);
if(!hServiceStatus)
{
MessageBox(NULL,"注册控制服务失败!","",MB_OK);
return;
}

//设置服务状态
SetServiceStatus(hServiceStatus,&ServiceStatus);

ServiceStatus.dwWin32ExitCode=S_OK;
ServiceStatus.dwCheckPoint=0;
ServiceStatus.dwWaitHint=0;
ServiceStatus.dwCurrentState=SERVICE_RUNNING;
SetServiceStatus(hServiceStatus,&ServiceStatus);

//服务任务部分
Action();

//退出服务
ServiceStatus.dwCurrentState=SERVICE_STOPPED;
SetServiceStatus(hServiceStatus,&ServiceStatus);
}

//服务控制函数
void WINAPI ServiceStrl(DWORD dwMsg)
{
switch(dwMsg)
{
case SERVICE_CONTROL_STOP:
ServiceStatus.dwCurrentState=SERVICE_STOP_PENDING;
SetServiceStatus(hServiceStatus,&ServiceStatus);
PostThreadMessage(dwThreadId,WM_CLOSE,0,0);
break;
case SERVICE_CONTROL_PAUSE:
break;
case SERVICE_CONTROL_CONTINUE:
break;
case SERVICE_CONTROL_INTERROGATE:
break;
case SERVICE_CONTROL_SHUTDOWN:
break;
case SERVICE_CONTROL_PARAMCHANGE:
break;
default:
break;
}
}

//安装服务
BOOL Install()
{
//打开服务控制管理器
SC_HANDLE hSCM=::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(!hSCM)
return FALSE;

char chFilePathName[MAX_PATH];
CString strFileName;
char chSystemPath[MAX_PATH];
char* chNewFileName;

//获取自身程序文件名和系统目录
::GetModuleFileName(NULL,chFilePathName,MAX_PATH);
strFileName=chFilePathName;
while(strFileName.Find("\\",0)!=-1)
strFileName=strFileName.Right(strFileName.GetLength()-strFileName.Find("\\",0)-1);

//截取系统目录字符串,设置为system目录
::GetSystemDirectory(chSystemPath,MAX_PATH);
for(int n=0;(unsigned int)n<strlen(chSystemPath);n++)
{
if(chSystemPath[n]=='3')
chSystemPath[n]='\0';
}
chNewFileName=strcat(chSystemPath,"\\");
chNewFileName=strcat(chSystemPath,strFileName);

//将自身复制到系统system目录下
::CopyFile(chFilePathName,chNewFileName,FALSE);

//创建服务
SC_HANDLE hService=::CreateService(hSCM,chServiceName,chServiceDisplayName,
SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL,
chNewFileName,NULL,NULL,NULL,NULL,NULL);

//设置服务描述
SERVICE_DESCRIPTION ServiceDescription;
ServiceDescription.lpDescription=chServiceDescription;
::ChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION,&ServiceDescription);

//释放句柄
CloseServiceHandle(hSCM);

if(!hService)
return FALSE;

//释放句柄
CloseServiceHandle(hService);

MessageBox(NULL,"Install done!","",MB_OK);

return TRUE;
}

//判断服务是否已安装
BOOL IsInstalled()
{
//打开服务控制管理器
SC_HANDLE hSCM=::OpenSCManager(NULL,NULL,SERVICE_ALL_ACCESS);
if(!hSCM)
return FALSE;

//打开服务
SC_HANDLE hService=::OpenService(hSCM,chServiceName,SERVICE_QUERY_CONFIG);
if(!hService)
return FALSE;

//释放句柄
CloseServiceHandle(hSCM);
CloseServiceHandle(hService);

return TRUE;
}

//卸载服务
BOOL UnInstall()
{
//打开服务控制管理器
SC_HANDLE hSCM=::OpenSCManager(NULL,NULL,SERVICE_ALL_ACCESS);
if(!hSCM)
return FALSE;

//打开服务
SC_HANDLE hService=::OpenService(hSCM,chServiceName,SERVICE_STOP|DELETE);
if(!hService)
return FALSE;

//停止服务
SERVICE_STATUS ServiceStatus;
::ControlService(hService,SERVICE_CONTROL_STOP,&ServiceStatus);

//删除服务
if(::DeleteService(hService))
return FALSE;

//释放句柄
CloseServiceHandle(hSCM);
CloseServiceHandle(hService);

MessageBox(NULL,"UnInstall done!","",MB_OK);

return TRUE;
}

//服务任务
void Action()
{
MessageBox(NULL,"Service running!","NULL",MB_OK);
Sleep(10000);
}
...全文
19347 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
mino_0000 2012-10-27
  • 打赏
  • 举报
回复
解决的代码呢?
pofante 2011-08-12
  • 打赏
  • 举报
回复
要先运行服务程序还是先安装服务程序?必须要分开吗?
hamigua_12 2011-05-25
  • 打赏
  • 举报
回复
也遇到了这个问题,也觉得是StartServiceCtrlDispatcher的问题,因为在使用ATL创建的服务就没有问题,能够正常运行,但是自己写的时候,直接就是使用OpenSCManager和CreateService来创建函数,并使用StartService来启动,所以觉得应该是这里的问题
但是如果使用StartServiceCtrlDispatcher,就又牵涉到很多东西,不知道该怎么来弄

用没有更简单的方法,不适用StartServiceCtrlDispatcher就能让服务顺利跑起来呢
zyc_glboy 2011-05-09
  • 打赏
  • 举报
回复
可能是没有调用到StartServiceCtrlDispatcher,我的就是这个问题!
Jsurfer 2011-01-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zcnvnv 的回复:]

解决了,把服务程序和安装/卸载程序分开就行了.
[/Quote]

一定要分开才行吗?
haiscn 2010-09-19
  • 打赏
  • 举报
回复
各位哥哥姐姐,以上的信息没有看得很明白。
我是自己写的一个程序,然后加进了系统服务,可是怎么都启动不了,怎么办啊???
lbjayo 2010-08-30
  • 打赏
  • 举报
回复
我的也是这个问题,来顶一下
ray- 2010-05-06
  • 打赏
  • 举报
回复
BOOL InstallService()
{

char strDir[MAX_PATH ];
HANDLE schSCManager,schService;

memset(strDir,0,sizeof(TCHAR)*MAX_PATH);
GetModuleFileName( NULL,strDir, MAX_PATH );
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if (schSCManager == NULL)
return false;

LPCTSTR lpszBinaryPathName=strDir;

schService = CreateService((struct SC_HANDLE__ *)schSCManager,"Service2","MB Service6",
// service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password

if (schService == NULL)
return false;
/* SERVICE_FAILURE_ACTIONS sdBuf={0};

sdBuf.lpRebootMsg=NULL;
sdBuf.dwResetPeriod=3600*24;

SC_ACTION action[3];

action[0].Delay=60*1000;
action[0].Type=SC_ACTION_RESTART;

action[1].Delay=0;
action[1].Type=SC_ACTION_NONE;
action[2].Delay=0;
action[2].Type=SC_ACTION_NONE;

sdBuf.cActions=3;
sdBuf.lpsaActions=action;
sdBuf.lpCommand=NULL;

if( !ChangeServiceConfig2(
schService,
SERVICE_CONFIG_FAILURE_ACTIONS,
&sdBuf) )
{
printf("%s ChangeServiceConfig2 failed\n",argv[1]);
bSuccess = FALSE;
}
else
printf("%s ChangeServiceConfig2 succeeded\n",argv[1]);

*/
StartService(schService, 0, 0);
CloseServiceHandle((struct SC_HANDLE__ *)schService);

return true;
}
  • 打赏
  • 举报
回复
把解决后的代码贴一下撒
tengfei2002 2009-05-31
  • 打赏
  • 举报
回复
BOOL InstallService()
{

char strDir[MAX_PATH ];
HANDLE schSCManager,schService;

memset(strDir,0,sizeof(TCHAR)*MAX_PATH);
GetModuleFileName( NULL,strDir, MAX_PATH );
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if (schSCManager == NULL)
return false;

LPCTSTR lpszBinaryPathName=strDir;

schService = CreateService((struct SC_HANDLE__ *)schSCManager,"Service2","MB Service6",
// service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password

if (schService == NULL)
return false;
/* SERVICE_FAILURE_ACTIONS sdBuf={0};

sdBuf.lpRebootMsg=NULL;
sdBuf.dwResetPeriod=3600*24;

SC_ACTION action[3];

action[0].Delay=60*1000;
action[0].Type=SC_ACTION_RESTART;

action[1].Delay=0;
action[1].Type=SC_ACTION_NONE;
action[2].Delay=0;
action[2].Type=SC_ACTION_NONE;

sdBuf.cActions=3;
sdBuf.lpsaActions=action;
sdBuf.lpCommand=NULL;

if( !ChangeServiceConfig2(
schService,
SERVICE_CONFIG_FAILURE_ACTIONS,
&sdBuf) )
{
printf("%s ChangeServiceConfig2 failed\n",argv[1]);
bSuccess = FALSE;
}
else
printf("%s ChangeServiceConfig2 succeeded\n",argv[1]);

*/
StartService(schService, 0, 0);
CloseServiceHandle((struct SC_HANDLE__ *)schService);

return true;
}
这是程序安装时的代码
cnzdgs 2009-05-05
  • 打赏
  • 举报
回复
你在调用Action函数之前执行的代码就是设置服务状态的。
qqwweerraahh 2009-05-05
  • 打赏
  • 举报
回复
解决了,把服务程序和安装/卸载程序分开就行了.
qqwweerraahh 2009-05-04
  • 打赏
  • 举报
回复
谢谢各位,但是我现在要解决的问题是让服务能够正常地运行起来.
服务在启动时,它提示"没有及时相应启动或控制请求"
我觉得可能是服务启动后ServiceMain中需要及时设置服务的当前状态,但是我不知道要设置什么状态
希望大家能够帮助我解决这个问题,谢谢,继续
fangchao918628 2009-05-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jameshooo 的回复:]
1、服务程序中不能使用MessageBox来调试,尤其在Action()中,建议使用日志。
2、Action除了弹出消息框,仅仅执行了10秒,之后服务进程就退出了,这不符合服务精神,应该是一个长期循环,只接收停止指令才退出。
[/Quote]
顶,在后面加个空的无限循环
cnzdgs 2009-05-04
  • 打赏
  • 举报
回复
服务程序中使用MessageBox要加上MB_SERVICE_NOTIFICATION标志。
jameshooo 2009-05-04
  • 打赏
  • 举报
回复
1、服务程序中不能使用MessageBox来调试,尤其在Action()中,建议使用日志。
2、Action除了弹出消息框,仅仅执行了10秒,之后服务进程就退出了,这不符合服务精神,应该是一个长期循环,只接收停止指令才退出。
qqwweerraahh 2009-05-04
  • 打赏
  • 举报
回复

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧