当前位置:   article > 正文

server__tprintf(text("startservicectrldispatcher failed!"

_tprintf(text("startservicectrldispatcher failed!"));
  1. // server.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "Windows.h"
  5. #define SZAPPNAME "serverSample"
  6. #define SZSERVICENAME "serviceSample"
  7. bool bDebugServer=false;
  8. SERVICE_STATUS ssStatus;
  9. SERVICE_STATUS_HANDLE sshStatusHandle;
  10. DWORD dwErr=0;
  11. TCHAR szErr[256];
  12. void WINAPI Service_Main(DWORD dwArgc, LPTSTR *lpszArgv);
  13. void WINAPI Service_Ctrl(DWORD dwCtrlCode);
  14. void installService();
  15. void removeService();
  16. void debugService(int argc,char** argv);
  17. bool ReportStatusToSCMgr(DWORD dwCurrentState,DWORD dwWin32ExitCode,DWORD dwWaitHint);
  18. void AddToMessageLog(LPTSTR lpszMsg);
  19. void ServiceStart(DWORD dwArgc,LPTSTR* lpszArgv);
  20. int _tmain(int argc, _TCHAR* argv[])
  21. {
  22. SERVICE_TABLE_ENTRY dispatchTable[]=
  23. {
  24. {TEXT(SZSERVICENAME),(LPSERVICE_MAIN_FUNCTION)Service_Main},
  25. { NULL,NULL}
  26. };
  27. // if((argc>1)&&((*argv[1]=='-')||(argv[1]==L"/")))
  28. // {
  29. // if(_stricmp(L"install",argv[1]+1)==0)
  30. // {
  31. // installService();
  32. // }
  33. // else if(_stricmp(L"remove",argv[1]+1)==0)
  34. // {
  35. // removeService();
  36. // }
  37. // else if(_stricmp(L"debug",argv[1]+1)==0)
  38. // {
  39. // bDebugServer=true;
  40. // debugService(argc,argv);
  41. // }
  42. // else
  43. // { installService();
  44. printf("%s - install to install the service \n",SZAPPNAME);
  45. printf("%s - remove to remove the service \n",SZAPPNAME);
  46. printf("%s - debug to debug the service \n",SZAPPNAME);
  47. printf("\n StartServiceCtrlDispatcher being called.\n");
  48. printf("This may take several seconds.Please wait.\n");
  49. if(!StartServiceCtrlDispatcher(dispatchTable))
  50. AddToMessageLog(TEXT("StartServiceCtrlDispatcher failed."));
  51. else
  52. AddToMessageLog(TEXT("StartServiceCtrlDispatcher OK."));
  53. // }
  54. //exit(0);
  55. //}
  56. return 0;
  57. }
  58. void WINAPI Service_Main(DWORD dwArgc, LPTSTR *lpszArgv)
  59. {
  60. sshStatusHandle=RegisterServiceCtrlHandler(TEXT(SZSERVICENAME),Service_Ctrl);
  61. if(!sshStatusHandle)
  62. {
  63. goto cleanup;
  64. return;
  65. }
  66. ssStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
  67. ssStatus.dwServiceSpecificExitCode=0;
  68. if(!ReportStatusToSCMgr(
  69. SERVICE_START_PENDING,
  70. NO_ERROR,
  71. 3000))
  72. goto cleanup;
  73. ServiceStart(dwArgc,lpszArgv);
  74. return;
  75. cleanup:
  76. if(sshStatusHandle)
  77. (void)ReportStatusToSCMgr(SERVICE_STOPPED,dwErr,0);
  78. }
  79. void WINAPI Service_Ctrl(DWORD dwCtrlCode)
  80. {
  81. switch(dwCtrlCode)
  82. {
  83. //先更新服务状态为 SERVICDE_STOP_PENDING,再停止服务。
  84. case SERVICE_CONTROL_STOP:
  85. ReportStatusToSCMgr(SERVICE_STOP_PENDING,NO_ERROR,500);
  86. ServiceStop(); //由具体的服务程序实现
  87. return;
  88. //暂停服务
  89. case SERVICE_CONTROL_PAUSE:
  90. ReportStatusToSCMgr(SERVICE_STOP_PENDING,NO_ERROR,500);
  91. ServicePause(); //由具体的服务程序实现
  92. ssStatus.dwCurrentState=SERVICE_PAUSED;
  93. return;
  94. //继续服务
  95. case SERVICE_CONTROL_CONTINUE:
  96. ReportStatusToSCMgr(SERVICE_STOP_PENDING,NO_ERROR,500);
  97. ServiceContinue(); //由具体的服务程序实现
  98. ssStatus.dwCurrentState=SERVICE_RUNNING;
  99. return;
  100. //更新服务状态
  101. case SERVICE_CONTROL_INTERROGATE:
  102. break;
  103. //无效控制码
  104. default:
  105. break;
  106. }
  107. ReportStatusToSCMgr(ssStatus.dwCurrentState,NO_ERROR,0);
  108. }
  109. void installService()
  110. {
  111. SC_HANDLE schService;
  112. SC_HANDLE schSCManager;
  113. TCHAR szPath[512];
  114. //得到程序磁盘文件的路径
  115. if(GetModuleFileName(NULL,szPath,512)==0)
  116. {
  117. _tprintf(TEXT("Unable to install %s - %s \n"),
  118. TEXT(SZAPPNAME),
  119. GetLastError());//@1获取调用函数返回的最后错误码
  120. return;
  121. }
  122. //打开服务管理数据库
  123. schSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  124. if(schSCManager)
  125. {
  126. //登记服务程序
  127. schService=CreateService(
  128. schSCManager, //服务管理数据库句柄
  129. TEXT(SZSERVICENAME), //服务名
  130. TEXT(SZAPPNAME), //用于显示服务的标识
  131. SERVICE_ALL_ACCESS, //响应所有的访问请求
  132. SERVICE_WIN32_OWN_PROCESS, //服务类型
  133. SERVICE_DEMAND_START, //启动类型
  134. SERVICE_ERROR_NORMAL, //错误控制类型
  135. szPath, //服务程序磁盘文件的路径
  136. NULL, //服务不属于任何组
  137. NULL, //没有tag标识符
  138. NULL, //启动服务所依赖的服务或服务组,这里仅仅是一个空字符串
  139. NULL, //LocalSystem 帐号
  140. NULL);
  141. if(schService)
  142. {
  143. _tprintf(TEXT("%s installed. \n"),TEXT(SZAPPNAME));
  144. CloseServiceHandle(schService);
  145. }
  146. else
  147. {
  148. _tprintf(TEXT("CreateService failed - %s \n"),GetLastError());
  149. }
  150. CloseServiceHandle(schSCManager);
  151. }
  152. else
  153. _tprintf(TEXT("OpenSCManager failed - %s \n"),GetLastError());
  154. }
  155. //停止和删除已安装的服务程序
  156. void removeService()
  157. {
  158. SC_HANDLE schService;
  159. SC_HANDLE schSCManager;
  160. //打开服务管理数据库
  161. schSCManager=OpenSCManager(
  162. NULL, //本地计算机
  163. NULL, //默认的数据库
  164. SC_MANAGER_ALL_ACCESS //要求所有的访问权
  165. );
  166. if(schSCManager)
  167. {
  168. //获取服务程序句柄
  169. schService=OpenService(
  170. schSCManager, //服务管理数据库句柄
  171. TEXT(SZSERVICENAME), //服务名
  172. SERVICE_ALL_ACCESS //响应所有的访问请求
  173. );
  174. if(schService)
  175. {
  176. //试图停止服务
  177. if(ControlService(
  178. schService, //服务程序句柄
  179. SERVICE_CONTROL_STOP, //停止服务请求码
  180. &ssStatus //接收最后的服务状态信息
  181. ))
  182. {
  183. _tprintf(TEXT("Stopping %s."),TEXT(SZAPPNAME));
  184. Sleep(1000);
  185. //等待服务停止
  186. //
  187. while(QueryServiceStatus(schService,&ssStatus))
  188. {
  189. if(SERVICE_STOP_PENDING==ssStatus.dwCurrentState)
  190. {
  191. _tprintf(TEXT("."));
  192. Sleep(1000);
  193. }
  194. else
  195. break;
  196. }
  197. if(SERVICE_STOPPED==ssStatus.dwCurrentState)
  198. _tprintf(TEXT("\n %s stopped. \n"),TEXT(SZAPPNAME));
  199. else
  200. _tprintf(TEXT("\n %s failed to stopp. \n"),TEXT(SZAPPNAME));
  201. }
  202. //删除已安装的服务程序安装
  203. if(DeleteService(schService))
  204. _tprintf(TEXT("%s removed. \n"),TEXT(SZAPPNAME));
  205. else
  206. _tprintf(TEXT("DeleteService failed - %s. \n"), GetLastError());
  207. CloseServiceHandle(schService);
  208. }
  209. else
  210. _tprintf(TEXT("OpenService failed - %s \n"),GetLastError());
  211. CloseServiceHandle(schSCManager);
  212. }
  213. else
  214. _tprintf(TEXT("OpenSCManager failed - %s \n"),GetLastError());
  215. }


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/69283
推荐阅读
相关标签
  

闽ICP备14008679号