CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

StartServiceCtrlDispatcher()这个函数失败,错误代码1063.问题解决后马上结帖!

楼主Victorianism(风尚)2006-03-03 11:37:24 在 VC/MFC / 基础类 提问

MSDN上说:  
    Typically,   this   error   indicates   that   the   program   is   being   run   as   a   console   application   rather   than   as   a   service.    
  If   the   program   will   be   run   as   a   console   application   for   debugging   purposes,   structure   it   such   that   service-specific   code   is   not   called   when   this   error   is   returned.  
   
  但我不明白怎样去解决这个问题,烦请大家帮忙! 问题点数:50、回复次数:5Top

1 楼pomelowu(羽战士)回复于 2006-03-03 11:40:47 得分 0

你不能从VC直接启动你的服务程序,你需要先安装你的服务。否则的话,程序会作为控制台程序运行,并在StartServiceCtrlDispatcher返回FALSE之前会有一个等待失败的延迟。作控制台程序运行可用于调试。  
  ////////////////////////////////////////////////////  
  //   From   天极网   《Win32程序设计之服务》  
  Install:  
  //***************************************************************  
  //   From   the   book   "Win32   System   Services:   The   Heart   of   Windows   NT"  
  //   by   Marshall   Brain  
  //   Published   by   Prentice   Hall  
  //  
  //   This   code   installs   a   service.  
  //***************************************************************  
   
  //   install.cpp  
   
  #include   <windows.h>  
  #include   <iostream.h>  
   
  void   ErrorHandler(char   *s,   DWORD   err)  
  {  
          cout   <<   s   <<   endl;  
          cout   <<   "Error   number:   "   <<   err   <<   endl;  
          ExitProcess(err);  
  }  
   
  void   main(int   argc,   char   *argv[])  
  {  
          SC_HANDLE   newService,   scm;  
           
          if   (argc   !=   4)  
          {  
                  cout   <<   "Usage:\n";  
                  cout   <<   "   install   service_name   service_label   executable\n";  
                  cout   <<   "   service_name   is   the   name   used   internally   by   the   SCM\n";  
                  cout   <<   "   service_label   is   the   name   that   appears   in   the   Services   applet\n";  
                  cout   <<   "   (for   multiple   words,   put   them   in   double   quotes)\n";  
                  cout   <<   "   executable   is   the   full   path   to   the   EXE\n\n";  
                  return;  
          }  
           
          //   open   a   connection   to   the   SCM  
          scm   =   OpenSCManager(0,   0,   SC_MANAGER_CREATE_SERVICE);  
          if   (!scm)   ErrorHandler("In   OpenScManager",  
                  GetLastError());  
           
          //   Install   the   new   service  
          newService   =   CreateService(  
                  scm,   argv[1],   //   eg   "beep_srv"  
                  argv[2],   //   eg   "Beep   Service"  
                  SERVICE_ALL_ACCESS,   SERVICE_WIN32_OWN_PROCESS,  
                  SERVICE_DEMAND_START,   SERVICE_ERROR_NORMAL,  
                  argv[3],   //   eg   "c:\winnt\xxx.exe"  
                  0,   0,   0,   0,   0);  
          if   (!newService)   ErrorHandler("In   CreateService",  
                  GetLastError());  
          else   cout   <<   "Service   installed\n";  
           
          //   clean   up  
          CloseServiceHandle(newService);  
          CloseServiceHandle(scm);  
  }  
   
  //--------------------------------------------------------  
  Uninstall:  
  //***************************************************************  
  //   From   the   book   "Win32   System   Services:   The   Heart   of   Windows   NT"  
  //   by   Marshall   Brain  
  //   Published   by   Prentice   Hall  
  //  
  //   This   code   removes   a   service   from   the   Services   applet   in   the  
  //   Control   Panel.  
  //***************************************************************  
   
  //   remove.cpp  
   
  #include   <windows.h>  
  #include   <iostream.h>  
   
  void   ErrorHandler(char   *s,   DWORD   err)  
  {    
          cout   <<   s   <<   endl;  
          cout   <<   "Error   number:   "   <<   err   <<   endl;  
          ExitProcess(err);    
  }  
   
  void   main(int   argc,   char   *argv[])  
  {  
          SC_HANDLE   service,   scm;  
          BOOL   success;  
          SERVICE_STATUS   status;  
           
          if   (argc   !=   2)  
          {  
                  cout   <<   "Usage:\nremove   service_name\n";  
                  return;  
          }  
           
          //   Open   a   connection   to   the   SCM  
          scm   =   OpenSCManager(0,   0,   SC_MANAGER_CREATE_SERVICE);  
          if   (!scm)   ErrorHandler("In   OpenScManager",  
                  GetLastError());  
           
          //   Get   the   service's   handle  
          service   =   OpenService(scm,   argv[1],  
                  SERVICE_ALL_ACCESS   |   DELETE);  
          if   (!service)   ErrorHandler("In   OpenService",  
                  GetLastError());  
           
          //   Stop   the   service   if   necessary  
          success   =   QueryServiceStatus(service,   &status);  
          if   (!success)   ErrorHandler("In   QueryServiceStatus",  
                  GetLastError());  
          if   (status.dwCurrentState   !=   SERVICE_STOPPED)  
          {  
                  cout   <<   "Stopping   service...\n";  
                  success   =   ControlService(service,  
                          SERVICE_CONTROL_STOP,   &status);  
                  if   (!success)   ErrorHandler("In   ControlService",  
                          GetLastError());  
          }  
           
          //   Remove   the   service  
          success   =   DeleteService(service);  
          if   (success)   cout   <<   "Service   removed\n";  
          else   ErrorHandler("In   DeleteService",  
                  GetLastError());  
           
          //   Clean   up  
          CloseServiceHandle(service);  
          CloseServiceHandle(scm);  
  }Top

2 楼lzzqqq(Jonersen)回复于 2006-03-03 11:48:50 得分 0

安装服务的功能通常是包含在你的服务   .exe里面的,通过参数来实现.  
  比如  
  MyService.exe   -install             //安装服务,增加到SMC,服务程序   MyService.exe  
  MyService.exe   -uninstall         //泻载服务,从SMC里面删除掉.  
  Top

3 楼Victorianism(风尚)回复于 2006-03-03 11:53:35 得分 0

TO:pomelowu(羽战士)   (   )   信誉:193    
  可我已经安装了服务的啊!Top

4 楼Victorianism(风尚)回复于 2006-03-07 19:12:42 得分 0

怎样在VC中调试service呢?Top

5 楼mynamelj(风动,帆动,仁者心动)回复于 2006-05-03 16:38:16 得分 50

Attach你的service进程Top

相关问题

  • WaitforSingleObject函数调用失败————————?
  • 函数的小错误
  • 检查个函数错误
  • CreateThread()函数的183错误。
  • 构造函数失败的问题
  • SetFocus函数为什么调用失败?
  • 为什么WinInet函数从不失败?
  • 构造函数编译错误
  • 如何捕获strtofloat函数的错误!
  • 一个函数的错误提示

关键词

  • win32
  • service
  • 程序
  • install
  • 服务
  • include

得分解答快速导航

  • 帖主:Victorianism
  • mynamelj

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo