为什么我写的服务程序,注册之后老是启动失败,返回1053

奔跑的_ 2014-05-25 12:24:23
代码如下,请求大侠们指教
#include "stdafx.h"
#include <Windows.h>
#include <atlstr.h>

#define SERVCE_NAME L"myservice1"

VOID WINAPI Handler(__in DWORD fdwControl)
{
switch (fdwControl)
{
case SERVICE_CONTROL_CONTINUE:
::MessageBoxW(NULL, L"我是继续", L"提示", MB_OK);
break;

case SERVICE_CONTROL_PAUSE:
::MessageBoxW(NULL, L"我是暂停", L"提示", MB_OK);
break;

case SERVICE_CONTROL_SHUTDOWN:
::MessageBoxW(NULL, L"我是关机", L"提示", MB_OK);
break;

case SERVICE_CONTROL_STOP:
::MessageBoxW(NULL, L"我是停止", L"提示", MB_OK);
break;

default:
{
CStringW strMsg;
strMsg.Format(L"我是 %d", fdwControl);
::MessageBoxW(NULL, strMsg.GetString(), L"提示", MB_OK);
}
break;
}
}

void WINAPI ServiceMain(__in DWORD dwArgc, __in LPTSTR* lpszArgv)
{
::MessageBoxW(NULL, L"进入入口函数", L"提示", MB_OK);

::RegisterServiceCtrlHandlerW(SERVCE_NAME, Handler);
}

int main()
{
SERVICE_TABLE_ENTRYW stew[] =
{
{SERVCE_NAME, ServiceMain},
{NULL, NULL}
};

::MessageBoxW(NULL, L"初始化入口函数", L"提示", MB_OK);

if (!::StartServiceCtrlDispatcherW(stew))
{
::MessageBoxW(NULL, L"入口函数初始化失败", L"提示", MB_OK);
}

return 0;
}
...全文
228 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
奔跑的_ 2014-05-29
  • 打赏
  • 举报
回复
引用 2 楼 fengqinqdca 的回复:
你就没有启动服务的代码...... ghStatus=RegisterServiceCtrlHandler(STR_SERVER_NAME,(LPHANDLER_FUNCTION)ControlHandler); if (ghStatus==NULL) { printf("Registering Control Handler failed.\n"); return; } //启动服务 gServiceStatus.dwCurrentState=SERVICE_RUNNING; SetServiceStatus(ghStatus,&gServiceStatus);
我有专门的程序去启动的,这个只是在注册了操作函数之后就启动了服务,我的意思是我从系统提供的服务管理程序去启动会失败,返回1053
华美乐章 2014-05-26
  • 打赏
  • 举报
回复
你就没有启动服务的代码...... ghStatus=RegisterServiceCtrlHandler(STR_SERVER_NAME,(LPHANDLER_FUNCTION)ControlHandler); if (ghStatus==NULL) { printf("Registering Control Handler failed.\n"); return; } //启动服务 gServiceStatus.dwCurrentState=SERVICE_RUNNING; SetServiceStatus(ghStatus,&gServiceStatus);
赵4老师 2014-05-26
  • 打赏
  • 举报
回复
用调试器(OD,WINDBG等)调试服务程序 To debug the initialization code of a service application, the debugger must be attached when the service is started. This is accomplished by creating a registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\ProgramName The ProgramName is the image file for the service application you are debugging. Do not specify a path. For example, the ProgramName might look like MyService.exe. Under this key create a string data value called Debugger. The value of this string should be set to the full path of the debugger that will be used. For example, c:\Debuggers\windbg.exe In addition to setting this registry key, the service application must be marked as "interactive". This allows your service to interact with the desktop, and allows the debugger window to appear on your desktop. This again requires modifying a registry key: you must bitwise-or the type entry for your service with 0x100 (this is the value for SERVICE_INTERACTIVE_PROCESS according to Winnt.h). The exact location and name of this registry entry varies. For example: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyServiceKey Finally, you need to adjust the service application timeout. Otherwise, the service application will kill the debugger within 20 seconds after starting. Adjusting the timeout involves setting an entry in the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control Under this key, create a DWORD data value called ServicesPipeTimeout. Set this entry to the amount of time in milliseconds that you want the service to wait before timing out. For example, 60,000 is one minute, while 86,400,000 is 24 hours. 设置ServicesPipeTimeout后需要重启系统才生效 Now, when the service is started, the debugger will also start. When the debugger starts, it will stop at the initial process breakpoint, before the service has begun running. This allows you to set breakpoints or otherwise configure your debugging session to let you monitor the startup of your service. Another option is to place calls to the DebugBreak function in your service from the point at which you would like to break into the debugger. (For more information, see DebugBreak in the Platform SDK documentation.) If your service is running with other services in a Service Host Process, you may need to isolate the service into its own Service Host Process.

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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