Service.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. // Service.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "SysLib.h"
  5. #include "ascprocess.h"
  6. #include "tcpprocess.h"
  7. #include "rtuprocess.h"
  8. #include "icpdasprocess.h"
  9. #include "SnmpProcess.h"
  10. #include "NoticeQueue.h"
  11. #include "MainPro.h"
  12. #include "CheckDog.h"
  13. #include "upsparadigmProcess.h"
  14. #include "stulzprocess.h"
  15. #include "Separate.h"
  16. #include "MostDevice.h"
  17. #include "stdio.h"
  18. #include "tchar.h"
  19. #include <windows.h>
  20. #include "winsvc.h"
  21. #include "ping.h"
  22. #include "SoftRegister.h"
  23. #include "resource1.h"
  24. //定义全局函数变量
  25. void Init();
  26. BOOL IsInstalled();
  27. BOOL Install();
  28. BOOL Uninstall();
  29. void WINAPI ServiceMain();
  30. void WINAPI ServiceStrl(DWORD dwOpcode);
  31. BOOL InitIOService();
  32. void UnInitIOService();
  33. SERVICE_STATUS_HANDLE g_hServiceStatus;
  34. SERVICE_STATUS g_status;
  35. DWORD dwThreadID;
  36. HINSTANCE g_hLangDLL;
  37. int CALLBACK Sms_Receive( int iType,float fData,CString sContent )
  38. {
  39. if( iType==0 )
  40. {
  41. int nRet = (int)fData;
  42. if( nRet==1 )
  43. {
  44. LOG4C((LOG_NOTICE, "收到恢复所有报警通知短信"));
  45. }
  46. else if( (int)fData==0 )
  47. {
  48. LOG4C((LOG_NOTICE, "收到关闭所有报警通知短信"));
  49. }
  50. g_bAlarmNoticeFlag = (int)fData;
  51. }
  52. else if( iType==1 )
  53. {
  54. if( !g_bGetBlanceReturn )
  55. {
  56. g_fBlance = fData;
  57. g_bGetBlanceReturn = true;
  58. CString sShow;
  59. if( int(fData)>=g_nBlanceLessToNotice )
  60. {
  61. sShow.Format( "当前帐户余额:%.2f 元\n%s",fData,sContent );
  62. LOG4C((LOG_NOTICE, sShow));
  63. }
  64. else
  65. {
  66. sShow.Format( g_strHintBlance,fData,g_nBlanceLessToNotice );
  67. LOG4C((LOG_NOTICE, sShow));
  68. if( fData>0.5 )
  69. SendSmsNoticeToUser( 0,sShow );
  70. }
  71. }
  72. }
  73. return 0;
  74. }
  75. //HANDLE m_hMutexInstance;
  76. int _tmain(int argc, _TCHAR* argv[])
  77. {
  78. // *****************给控制台程序设置图标******************************//
  79. HWND hwnd = GetForegroundWindow(); // 直接获得前景窗口的句柄
  80. SendMessage(hwnd , WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_MICON)));
  81. // *****************给控制台程序设置图标******************************//
  82. // 初始化服务参数;
  83. Init();
  84. // 获取当前程序主线程ID,用于程序自我结束时,需调用主线程ID;
  85. dwThreadID = ::GetCurrentThreadId();
  86. // 初始化SOCKET库;
  87. CSocketHandle::InitLibrary( MAKEWORD(2,2) );
  88. // 获取配置文件信息;
  89. GetSysData();
  90. // 加载多语言资源文件 ;
  91. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  92. if( stricmp(g_strLanguage, "CHS") == 0 )
  93. {
  94. wsprintf(strFile, "%s\\ChineseRes.dll", g_strDirectory);
  95. g_hLangDLL = ::LoadLibrary(strFile);
  96. }
  97. else if( stricmp(g_strLanguage, "CHT") == 0 )
  98. {
  99. wsprintf(strFile, "%s\\TaiwanRes.dll", g_strDirectory);
  100. g_hLangDLL = ::LoadLibrary(strFile);
  101. }
  102. else if( stricmp(g_strLanguage, "ENG") == 0 )
  103. {
  104. wsprintf(strFile, "%s\\EnglishRes.dll", g_strDirectory);
  105. g_hLangDLL = ::LoadLibrary(strFile);
  106. }
  107. // 设置当前程序要用的资源模块 ;
  108. AfxSetResourceHandle(g_hLangDLL);
  109. // 获取当前资源模块的字符串信息;(就是g_hLangDLL模块里的);
  110. GetResourceString();
  111. // 判断服务是否存在;
  112. if( IsInstalled())
  113. {
  114. // 卸载服务;
  115. if ( argc > 1)
  116. {
  117. if (stricmp((char *)argv[1], "/uninstall") == 0)
  118. Uninstall();
  119. // 卸载完了,必须退出;
  120. return 0;
  121. }
  122. else
  123. {
  124. SERVICE_TABLE_ENTRY st[] =
  125. {
  126. { g_szServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain },
  127. { NULL, NULL }
  128. };
  129. // 当服务存在时,通过双击执行文件也会到这一步;
  130. // 但是StartServiceCtrlDispatcher只能在服务管理器才能执行成功,避免了双击服务也能运行;
  131. if (!::StartServiceCtrlDispatcher(st))
  132. {
  133. LOG4C((LOG_NOTICE, _T("Register Service Main Function Error!")));
  134. return 0;
  135. }
  136. }
  137. }
  138. // 2.如果服务不存在,则安装;
  139. SERVICE_TABLE_ENTRY st[] =
  140. {
  141. { g_szServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain },
  142. { NULL, NULL }
  143. };
  144. Install();
  145. return 0;
  146. }
  147. BOOL InitIOService()
  148. {
  149. // 创建程序运行事件,控制整个程序的生命(运行与结束);
  150. MTVERIFY (g_hRunObject = ::CreateEvent(
  151. NULL, /* Security */
  152. TRUE, /* Manual event */
  153. 0, /* Clear on creation */
  154. 0));
  155. // 文件日志模块;
  156. CString strFileName;
  157. strFileName.Format("%s\\log4crc", g_strDirectory);
  158. LogEvent(strFileName);
  159. ///设置日志配置文件名
  160. LOG4C_PARAM_CFG_FILE_NAME(strFileName);
  161. ///设置日志级别
  162. LOG4C_PARAM_LOG_LEVEL("NOTICE");
  163. ///设置日志文件大小
  164. LOG4C_PARAM_LOG_FILE_SIZE(10240);
  165. ///设置生成日志文件个数,达到最大个数将自动覆盖最旧的日志
  166. LOG4C_PARAM_LOG_FILE_NUM(5);
  167. ///设置每次记录日志都重新读取日志配置文件
  168. LOG4C_PARAM_REREAD_LOG_CFG_FILE(1);
  169. ///带参数日志模块初始化,以上所有设置了的参数都将生效,没有设置的采用缺省值
  170. LOG4C_INIT_WITH_PARAM();
  171. if( ProgramEncrypt(NULL) == 0 ) return FALSE;
  172. // 创建设备管理对象(该类不纯,含有通信功能在内,需要修改);
  173. g_pDevicesManager = new CDevicesManager();
  174. // 加密方式;
  175. if( g_nEncryptionType==0 )
  176. {
  177. LOG4C((LOG_NOTICE, "狗加密"));
  178. int nRet=InitDog();
  179. if( nRet != 0 )
  180. {
  181. if( nRet!=-1 )
  182. g_sPassDueInfo = "狗的动态库找不到或者有误!";
  183. g_nUseTimeLimit = 1;
  184. //return FALSE;
  185. }
  186. }
  187. else if( g_nEncryptionType==476 )
  188. {
  189. LOG4C((LOG_NOTICE, "没加密"));
  190. }
  191. else
  192. {
  193. LOG4C((LOG_NOTICE, "软加密"));
  194. int nRet = InitSoftRegister();//0 :OK -1:DLL不存在 1:DLL不是新的
  195. //LOG4C((LOG_NOTICE, "软加密 nRet=%d",nRet));
  196. if( nRet==0 )
  197. {
  198. CString sMsg;
  199. //LOG4C((LOG_NOTICE, "pRegdllJudgeIsRegister in"));
  200. int nRet = pRegdllJudgeIsRegister( sMsg );
  201. //LOG4C((LOG_NOTICE, "pRegdllJudgeIsRegister nRet=%d",nRet));
  202. if( nRet!=0 && nRet!=8 )
  203. {
  204. LOG4C((LOG_NOTICE, "%s 软件不会采集数据!",sMsg ));//LOG4C((LOG_NOTICE, "%s 软件非法退出!",sMsg ));
  205. //试用已经过期( 总试用天数为%s天(%s)
  206. //int n1=-1,n2=-1;
  207. //n1 = sMsg.Find( "天(" );
  208. //n2 = sMsg.Find( ")",n1 );
  209. //g_nSlaveVersion = atoi( sMsg.Mid( n1+3,n2-n1-1 ) );
  210. g_sPassDueInfo = sMsg;
  211. g_nUseTimeLimit = 1;
  212. //return FALSE;
  213. }
  214. else
  215. {
  216. if( nRet==8 )
  217. g_bSoftRegisterFileIsOfficial = true;
  218. g_sPassDueInfo = sMsg;
  219. LOG4C((LOG_NOTICE, "%s",sMsg ));
  220. }
  221. }
  222. else if( nRet==-1 )
  223. {
  224. LOG4C((LOG_NOTICE, "动态库文件Register.dll不存在,软件非法退出!" ));
  225. return FALSE;
  226. }
  227. else //if( nRet==1 )
  228. {
  229. LOG4C((LOG_NOTICE, "动态库文件Register.dll不是新的,软件非法退出!" ));
  230. return FALSE;
  231. }
  232. }
  233. //LOG4C((LOG_NOTICE, "%s", g_strConnectString));
  234. //连接数据库
  235. LOG4C((LOG_NOTICE, "Enter OpenDataBase 【%s】",g_strConnectString));
  236. bool bFlag = false;
  237. while( !bFlag )
  238. {
  239. bFlag = CDBConnection::GetInstancePtr()->OpenDataBase(g_strConnectString);
  240. if( !bFlag )
  241. Sleep( 2000 );
  242. }
  243. //LOG4C((LOG_NOTICE, "Leave OpenDataBase"));
  244. //LOG4C((LOG_NOTICE, "Enter DBConnectionCheckThreadStart"));
  245. CDBConnection::GetInstancePtr()->DBConnectionCheckThreadStart();
  246. //LOG4C((LOG_NOTICE, "Leave DBConnectionCheckThreadStart"));
  247. //LOG4C((LOG_NOTICE, "Enter SetDBType"));
  248. CDBInterface::GetInstancePtr()->SetDBType(g_strDBType);
  249. //LOG4C((LOG_NOTICE, "Leave SetDBType"));
  250. //LOG4C((LOG_NOTICE, "Enter InitUserRoleInfo"));
  251. // 分配用户角色
  252. CDBInterface::GetInstancePtr()->InitUserRoleInfo();
  253. //LOG4C((LOG_NOTICE, "Leave InitUserRoleInfo"));
  254. LOG4C((LOG_NOTICE, "【ver:%s】",g_strVersion));
  255. //LOG4C((LOG_NOTICE, "Enter InitNotice"));
  256. if( !g_nSendVoice )
  257. {
  258. LOG4C((LOG_NOTICE, "没有启用语言通知"));
  259. }
  260. // 初始化语音库;
  261. int nRet = InitNotice();
  262. if( nRet==1 )
  263. {
  264. LOG4C((LOG_NOTICE, "启用了语音通知,但软件检查到语音卡不存在,请禁用语音通知或者安装语音卡,谢谢!"));
  265. }
  266. else if( nRet==2 )
  267. {
  268. LOG4C((LOG_NOTICE, "动态库不存在,请检查!"));
  269. }
  270. // 初始化Sms;
  271. if( g_nSendSms==1 )
  272. {
  273. CString strCommPort = CString(g_strSmsComPort);
  274. strCommPort = strCommPort.Mid( 3 );
  275. if( 0==InitSms( 0, atoi(strCommPort), g_nSmsRate, g_nSmsDataBit,
  276. g_nSmsStopBit, g_nSmsParity, g_nSmsInterval, g_nSmsLanguageTrans, g_nSmsMaxChar, g_nSmsMakeCall ) )
  277. {
  278. #if 1
  279. if( pSmsDllSetCallBack )
  280. {
  281. //LOG4C((LOG_NOTICE, "启用短信回调函数"));
  282. pSmsDllSetCallBack( Sms_Receive );
  283. }
  284. #endif
  285. }
  286. }
  287. else if( g_nSendSms==2 )
  288. {
  289. LOG4C((LOG_NOTICE, "启用支持短信平台服务"));
  290. }
  291. else
  292. {
  293. LOG4C((LOG_NOTICE, "没有启用短信通知"));
  294. }
  295. // 初始化Email;
  296. if( g_nSendEmail )
  297. {
  298. InitEmail(g_strEmailSMTPSrv, g_nEmailSMTPPort, g_nEmailIsNeed, g_strEmailUserAcc, g_strEmailUserPwd,
  299. g_strEmailFromAddr, g_nEmailTimeOut);
  300. }
  301. else
  302. {
  303. LOG4C((LOG_NOTICE, "没有启用email通知"));
  304. }
  305. #if 0
  306. // Tcp服务器
  307. g_pTcpServer = new CServerSocket();
  308. CString strPort;
  309. strPort.Format("%d", SOCKET_SERVER_PORT);
  310. g_pTcpServer->SvrStart(strPort);
  311. #else
  312. // Tcp服务器
  313. CString strPort1,strPort2,strPort3;
  314. // 被动
  315. g_pServerSocket[0] = new CServerSocket();
  316. strPort1.Format("%d", SOCKET_SERVER_PORT + 0);
  317. g_pServerSocket[0]->SvrStart(strPort1);
  318. // 主动
  319. g_pServerSocket[1] = new CServerSocket();
  320. strPort2.Format("%d", SOCKET_SERVER_PORT + 1);
  321. g_pServerSocket[1]->SvrStart(strPort2);
  322. ////远程DB
  323. //g_pServerSocket[6] = new CServerSocket();
  324. //strPort3.Format("%d", SOCKET_SERVER_PORT + 6);
  325. //g_pServerSocket[6]->SvrStart(strPort3);
  326. LOG4C((LOG_NOTICE, "Create Port %s,%s,%s success", strPort1,strPort2,strPort3));
  327. #endif
  328. // 初始化ping功能;
  329. InitPing();
  330. //读取iniList文件的内容
  331. CString sPath,sTemp;
  332. sPath.Format( "%s\\config", g_strDirectory);
  333. g_FunReadAllIniName( sPath,"*.*",0 );
  334. // 开始采集I/O设备
  335. StartMain();
  336. LOG4C((LOG_NOTICE, "system startup success!"));
  337. while(g_status.dwCurrentState != SERVICE_STOP_PENDING)
  338. {
  339. Sleep(1000);
  340. }
  341. LOG4C((LOG_NOTICE, _T("Service stopped")));
  342. UnInitIOService();
  343. return TRUE;
  344. }
  345. void UnInitIOService()
  346. {
  347. if (g_hRunObject != NULL)
  348. MTVERIFY( SetEvent( g_hRunObject ) );
  349. EndMain();
  350. #if 0
  351. if (g_pTcpServer != NULL)
  352. {
  353. g_pTcpServer->SocketStop();
  354. delete g_pTcpServer;
  355. g_pTcpServer = NULL;
  356. }
  357. #else
  358. for( int i = 0; i < MAX_SERVER_LISTEN; i++ )
  359. {
  360. if( NULL != g_pServerSocket[i] )
  361. {
  362. g_pServerSocket[i]->SocketStop();
  363. delete g_pServerSocket[i];
  364. g_pServerSocket[i] = NULL;
  365. }
  366. }
  367. #endif
  368. UnInitRtuDll();
  369. UnInitTcpDll();
  370. UnInitAscDll();
  371. UnInitIcpdasDll();
  372. UnInitSnmpDll();
  373. UnInitParadigmDll();
  374. UnInitStulzDll();
  375. UnInitSeparateDll();
  376. UnInitNotice();
  377. if( g_nSendEmail )
  378. {
  379. UnInitEmail();
  380. }
  381. if( g_nSendSms==1 )
  382. {
  383. UnInitSms();
  384. }
  385. //回收用户角色
  386. CDBInterface::GetInstancePtr()->UnInitUserRoleInfo();
  387. CDBConnection::GetInstancePtr()->DBConnectionCheckThreadStop();
  388. // 释放软件狗
  389. if( g_nEncryptionType==0 )
  390. UnInitDog();
  391. else if( g_nEncryptionType==476 )
  392. {
  393. }
  394. else
  395. UnInitSoftRegister();
  396. if( g_pDevicesManager )
  397. {
  398. delete g_pDevicesManager;
  399. g_pDevicesManager = NULL;
  400. }
  401. if( g_hRunObject != NULL )
  402. {
  403. MTVERIFY( CloseHandle( g_hRunObject ) );
  404. }
  405. if(g_hLangDLL)
  406. {
  407. AfxFreeLibrary(g_hLangDLL);
  408. }
  409. UnInitPing();
  410. ///关闭日志模块防止内存/资源泄漏
  411. LOG4C_FINI();
  412. }
  413. //*********************************************************
  414. //Functiopn: Init
  415. //Description: 初始化
  416. //Calls: main
  417. //Called By:
  418. //Table Accessed:
  419. //Table Updated:
  420. //Input:
  421. //Output:
  422. //Return:
  423. //Others:
  424. //History:
  425. // <author>niying <time>2006-8-10 <version> <desc>
  426. //*********************************************************
  427. void Init()
  428. {
  429. g_hServiceStatus = NULL;
  430. g_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  431. g_status.dwCurrentState = SERVICE_STOPPED;
  432. g_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  433. g_status.dwWin32ExitCode = 0;
  434. g_status.dwServiceSpecificExitCode = 0;
  435. g_status.dwCheckPoint = 0;
  436. g_status.dwWaitHint = 0;
  437. }
  438. //*********************************************************
  439. //Functiopn: ServiceMain
  440. //Description: 服务主函数,这在里进行控制对服务控制的注册
  441. //Calls:
  442. //Called By:
  443. //Table Accessed:
  444. //Table Updated:
  445. //Input:
  446. //Output:
  447. //Return:
  448. //Others:
  449. //History:
  450. // <author>niying <time>2006-8-10 <version> <desc>
  451. //*********************************************************
  452. void WINAPI ServiceMain()
  453. {
  454. // Register the control request handler
  455. g_status.dwCurrentState = SERVICE_START_PENDING;
  456. g_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  457. //注册服务控制
  458. g_hServiceStatus = RegisterServiceCtrlHandler(g_szServiceName, ServiceStrl);
  459. if (g_hServiceStatus == NULL)
  460. {
  461. return;
  462. }
  463. SetServiceStatus(g_hServiceStatus, &g_status);
  464. g_status.dwWin32ExitCode = S_OK;
  465. g_status.dwCheckPoint = 0;
  466. g_status.dwWaitHint = 0;
  467. g_status.dwCurrentState = SERVICE_RUNNING;
  468. SetServiceStatus(g_hServiceStatus, &g_status);
  469. //延时10S,开机自启动时,等待串口服务器和数据库都启动完再开始服务
  470. #if 0
  471. int i = 0;
  472. while (i < 11)
  473. {
  474. Sleep(1000);
  475. i++;
  476. }
  477. //
  478. #endif
  479. InitIOService();
  480. g_status.dwCurrentState = SERVICE_STOPPED;
  481. SetServiceStatus(g_hServiceStatus, &g_status);
  482. }
  483. //*********************************************************
  484. //Functiopn: ServiceStrl
  485. //Description: 服务控制主函数,这里实现对服务的控制,
  486. // 当在服务管理器上停止或其它操作时,将会运行此处代码
  487. //Calls:
  488. //Called By:
  489. //Table Accessed:
  490. //Table Updated:
  491. //Input: dwOpcode:控制服务的状态
  492. //Output:
  493. //Return:
  494. //Others:
  495. //History:
  496. // <author>niying <time>2006-8-10 <version> <desc>
  497. //*********************************************************
  498. void WINAPI ServiceStrl(DWORD dwOpcode)
  499. {
  500. switch (dwOpcode)
  501. {
  502. case SERVICE_CONTROL_STOP:
  503. //UnInitIOService();
  504. g_status.dwCurrentState = SERVICE_STOP_PENDING;
  505. SetServiceStatus(g_hServiceStatus, &g_status);
  506. PostThreadMessage(dwThreadID, WM_CLOSE, 0, 0);
  507. break;
  508. case SERVICE_CONTROL_PAUSE:
  509. break;
  510. case SERVICE_CONTROL_CONTINUE:
  511. break;
  512. case SERVICE_CONTROL_INTERROGATE:
  513. break;
  514. case SERVICE_CONTROL_SHUTDOWN:
  515. UnInitIOService();
  516. break;
  517. default:
  518. LOG4C((LOG_NOTICE, _T("Bad service request")));
  519. }
  520. }
  521. //*********************************************************
  522. //Functiopn: IsInstalled
  523. //Description: 判断服务是否已经被安装
  524. //Calls:
  525. //Called By:
  526. //Table Accessed:
  527. //Table Updated:
  528. //Input:
  529. //Output:
  530. //Return:
  531. //Others:
  532. //History:
  533. // <author>niying <time>2006-8-10 <version> <desc>
  534. //*********************************************************
  535. BOOL IsInstalled()
  536. {
  537. BOOL bResult = FALSE;
  538. //打开服务控制管理器
  539. SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  540. if (hSCM != NULL)
  541. {
  542. //打开服务
  543. SC_HANDLE hService = ::OpenService(hSCM, g_szServiceName, SERVICE_QUERY_CONFIG);
  544. if (hService != NULL)
  545. {
  546. bResult = TRUE;
  547. ::CloseServiceHandle(hService);
  548. }
  549. ::CloseServiceHandle(hSCM);
  550. }
  551. return bResult;
  552. }
  553. //*********************************************************
  554. //Functiopn: Install
  555. //Description: 安装服务函数
  556. //Calls:
  557. //Called By:
  558. //Table Accessed:
  559. //Table Updated:
  560. //Input:
  561. //Output:
  562. //Return:
  563. //Others:
  564. //History:
  565. // <author>niying <time>2006-8-10 <version> <desc>
  566. //*********************************************************
  567. BOOL Install()
  568. {
  569. if (IsInstalled())
  570. return TRUE;
  571. //打开服务控制管理器
  572. SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  573. if (hSCM == NULL)
  574. {
  575. //MessageBox(NULL, _T("Couldn't open service manager"), g_szServiceName, MB_OK);
  576. LOG4C((LOG_NOTICE,"安装服务: 未能打开服务管理器,安装失败"));
  577. int iEr = GetLastError();
  578. return FALSE;
  579. }
  580. // Get the executable file path
  581. TCHAR szFilePath[MAX_PATH];
  582. ::GetModuleFileName(NULL, szFilePath, MAX_PATH);
  583. //创建服务
  584. SC_HANDLE hService = ::CreateService(
  585. hSCM,
  586. g_szServiceName,
  587. g_szServiceName,
  588. SERVICE_ALL_ACCESS,
  589. SERVICE_WIN32_OWN_PROCESS,
  590. SERVICE_DEMAND_START, //SERVICE_AUTO_START,
  591. SERVICE_ERROR_NORMAL,
  592. szFilePath,
  593. NULL,
  594. NULL,
  595. _T(""),
  596. NULL,
  597. NULL);
  598. SERVICE_DESCRIPTION ServiceDesc;
  599. static TCHAR szDescription[MAX_PATH];
  600. _tcscpy(szDescription, g_strServiceName);
  601. ServiceDesc.lpDescription = szDescription;
  602. ::ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &ServiceDesc);
  603. if (hService == NULL)
  604. {
  605. ::CloseServiceHandle(hSCM);
  606. //MessageBox(NULL, _T("Couldn't create service"), g_szServiceName, MB_OK);
  607. LOG4C((LOG_NOTICE,"安装服务: 未能打开服务,安装失败"));
  608. return FALSE;
  609. }
  610. ::CloseServiceHandle(hService);
  611. ::CloseServiceHandle(hSCM);
  612. LOG4C((LOG_NOTICE,"安装服务: 安装服务成功"));
  613. return TRUE;
  614. }
  615. //*********************************************************
  616. //Functiopn: Uninstall
  617. //Description: 删除服务函数
  618. //Calls:
  619. //Called By:
  620. //Table Accessed:
  621. //Table Updated:
  622. //Input:
  623. //Output:
  624. //Return:
  625. //Others:
  626. //History:
  627. // <author>niying <time>2006-8-10 <version> <desc>
  628. //*********************************************************
  629. BOOL Uninstall()
  630. {
  631. if (!IsInstalled())
  632. return TRUE;
  633. SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  634. if (hSCM == NULL)
  635. {
  636. //MessageBox(NULL, _T("Couldn't open service manager"), g_szServiceName, MB_OK);
  637. LOG4C((LOG_NOTICE,"卸载服务: 未能打开服务管理器"));
  638. return FALSE;
  639. }
  640. SC_HANDLE hService = ::OpenService(hSCM, g_szServiceName, SERVICE_STOP | DELETE);
  641. if (hService == NULL)
  642. {
  643. ::CloseServiceHandle(hSCM);
  644. //MessageBox(NULL, _T("Couldn't open service"), g_szServiceName, MB_OK);
  645. LOG4C((LOG_NOTICE,"卸载服务: 未能打开服务"));
  646. return FALSE;
  647. }
  648. SERVICE_STATUS g_status;
  649. ::ControlService(hService, SERVICE_CONTROL_STOP, &g_status);
  650. //删除服务
  651. BOOL bDelete = ::DeleteService(hService);
  652. ::CloseServiceHandle(hService);
  653. ::CloseServiceHandle(hSCM);
  654. if (bDelete)
  655. return TRUE;
  656. LOG4C((LOG_NOTICE, _T("卸载服务:卸载服务失败")));
  657. return FALSE;
  658. }