{"id":1470,"date":"2018-06-27T19:01:55","date_gmt":"2018-06-27T11:01:55","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=1470"},"modified":"2018-06-27T19:01:55","modified_gmt":"2018-06-27T11:01:55","slug":"windows%e6%9c%8d%e5%8a%a1%e7%bc%96%e5%86%99%e5%8e%9f%e7%90%86%e5%8f%8a%e6%8e%a2%e8%ae%a8","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/1470","title":{"rendered":"Windows\u670d\u52a1\u7f16\u5199\u539f\u7406\u53ca\u63a2\u8ba8"},"content":{"rendered":"<div class=\"article-title-box\">\n<h1 class=\"title-article\">Windows\u670d\u52a1\u7f16\u5199\u539f\u7406\u53ca\u63a2\u8ba8<\/h1>\n<\/div>\n<div class=\"article-info-box\">\n<div class=\"article-bar-top d-flex\"><span class=\"time\">2012\u5e7405\u670818\u65e5 16:31:39<\/span><\/p>\n<div class=\"float-right\"><span class=\"read-count\">\u9605\u8bfb\u6570\uff1a2584<\/span><\/div>\n<\/div>\n<\/div>\n<article>\n<div id=\"article_content\" class=\"article_content clearfix csdn-tracking-statistics\" data-pid=\"blog\" data-mod=\"popu_307\" data-dsm=\"post\">\n<div class=\"htmledit_views\"><span id=\"font_word\" class=\"htd\">\u6709\u90a3\u4e48\u4e00\u7c7b\u5e94\u7528\u7a0b\u5e8f\uff0c\u662f\u80fd\u591f\u4e3a\u5404\u79cd\u7528\u6237\uff08\u5305\u62ec\u672c\u5730\u7528\u6237\u548c\u8fdc\u7a0b\u7528\u6237\uff09\u6240\u7528\u7684\uff0c\u62e5\u6709\u7528\u6237\u6388\u6743\u7ea7\u8fdb\u884c\u7ba1\u7406\u7684\u80fd\u529b\uff0c\u5e76\u4e14\u4e0d\u8bba\u7528\u6237\u662f\u5426\u7269\u7406\u7684\u4e0e\u6b63\u5728\u8fd0\u884c\u8be5\u5e94\u7528\u7a0b\u5e8f\u7684\u8ba1\u7b97\u673a\u76f8\u8fde\u90fd\u80fd\u6b63\u5e38\u6267\u884c\uff0c\u8fd9\u5c31\u662f\u6240\u8c13\u7684\u670d\u52a1\u4e86\u3002<\/span>\uff08\u4e00\uff09\u670d\u52a1\u7684\u57fa\u7840\u77e5\u8bc6<\/p>\n<p>Question 1. \u4ec0\u4e48\u662f\u670d\u52a1\uff1f\u5b83\u7684\u7279\u5f81\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u5728NT\/2000\u4e2d\uff0c\u670d\u52a1\u662f\u4e00\u7c7b\u53d7\u5230\u64cd\u4f5c\u7cfb\u7edf\u4f18\u5f85\u7684\u7a0b\u5e8f\u3002\u4e00\u4e2a\u670d\u52a1\u9996\u5148\u662f\u4e00\u4e2aWin32\u53ef\u6267\u884c\u7a0b\u5e8f\uff0c\u5982\u679c\u8981\u5199\u4e00\u4e2a\u529f\u80fd\u5b8c\u5907\u4e14\u5f3a\u5927\u7684\u670d\u52a1\uff0c\u9700\u8981\u719f\u6089\u52a8\u6001\u8fde\u63a5\u5e93(Dlls)\u3001\u7ed3\u6784\u5f02\u5e38\u5904\u7406\u3001\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u3001\u865a\u62df\u5185\u5b58\u3001\u8bbe\u5907I\/O\u3001\u7ebf\u7a0b\u53ca\u5176\u540c\u6b65\u3001Unicode\u4ee5\u53ca\u5176\u4ed6\u7684\u7531WinAPI\u51fd\u6570\u63d0\u4f9b\u7684\u5e94\u7528\u63a5\u53e3\u3002\u5f53\u7136\u672c\u6587\u8ba8\u8bba\u7684\u53ea\u662f\u5efa\u7acb\u4e00\u4e2a\u53ef\u4ee5\u5b89\u88c5\u3001\u8fd0\u884c\u3001\u542f\u52a8\u3001\u505c\u6b62\u7684\u6ca1\u6709\u4efb\u4f55\u5176\u4ed6\u529f\u80fd\u7684\u670d\u52a1\uff0c\u6240\u4ee5\u65e0\u9700\u4e0a\u8ff0\u77e5\u8bc6\u4ecd\u53ef\u4ee5\u7ee7\u7eed\u770b\u4e0b\u53bb\uff0c\u6211\u4f1a\u5728\u8fc7\u7a0b\u4e2d\u5c06\u7406\u89e3\u672c\u6587\u6240\u9700\u8981\u7684\u77e5\u8bc6\u9010\u4e00\u8bb2\u89e3\u3002<\/p>\n<p>\u7b2c\u4e8c\u8981\u77e5\u9053\u7684\u662f\u4e00\u4e2a\u670d\u52a1\u51b3\u4e0d\u9700\u8981\u7528\u6237\u754c\u9762\u3002\u5927\u591a\u6570\u7684\u670d\u52a1\u5c06\u8fd0\u884c\u5728\u90a3\u4e9b\u88ab\u9501\u5728\u67d0\u4e9b\u9ed1\u6697\u7684\uff0c\u51ac\u6696\u590f\u51c9\u7684\u5c0f\u5c4b\u5b50\u91cc\u7684\u5f3a\u5927\u7684\u670d\u52a1\u5668\u4e0a\u9762\uff0c\u5373\u4f7f\u6709\u7528\u6237\u754c\u9762\u4e00\u822c\u4e5f\u6ca1\u6709\u4eba\u53ef\u4ee5\u770b\u5230\u3002\u5982\u679c\u670d\u52a1\u63d0\u4f9b\u4efb\u4f55\u7528\u6237\u754c\u9762\u5982\u6d88\u606f\u6846\uff0c\u90a3\u4e48\u7528\u6237\u9519\u8fc7\u8fd9\u4e9b\u6d88\u606f\u7684\u53ef\u80fd\u6027\u5c31\u6781\u9ad8\u4e86\uff0c\u6240\u4ee5\u670d\u52a1\u7a0b\u5e8f\u901a\u5e38\u4ee5\u63a7\u5236\u53f0\u7a0b\u5e8f\u7684\u5f62\u5f0f\u88ab\u7f16\u5199\uff0c\u8fdb\u5165\u70b9\u51fd\u6570\u662fmain()\u800c\u4e0d\u662fWinMain()\u3002<\/p>\n<p>\u4e5f\u8bb8\u6709\u4eba\u6709\u7591\u95ee\uff1a\u6ca1\u6709\u7528\u6237\u754c\u9762\u7684\u8bdd\uff0c\u8981\u600e\u6837\u8bbe\u7f6e\u3001\u7ba1\u7406\u4e00\u4e2a\u670d\u52a1\uff1f\u600e\u6837\u5f00\u59cb\u3001\u505c\u6b62\u5b83\uff1f\u670d\u52a1\u5982\u4f55\u53d1\u51fa\u8b66\u544a\u6216\u9519\u8bef\u4fe1\u606f\u3001\u5982\u4f55\u62a5\u544a\u5173\u4e8e\u5b83\u7684\u6267\u884c\u60c5\u51b5\u7684\u7edf\u8ba1\u6570\u636e\uff1f\u8fd9\u4e9b\u95ee\u9898\u7684\u7b54\u6848\u5c31\u662f\u670d\u52a1\u80fd\u591f\u88ab\u8fdc\u7a0b\u7ba1\u7406\uff0cWindows NT\/2000\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u7ba1\u7406\u5de5\u5177\uff0c\u8fd9\u4e9b\u5de5\u5177\u5141\u8bb8\u901a\u8fc7\u7f51\u7edc\u4e0a\u7684\u5176\u5b83\u8ba1\u7b97\u673a\u5bf9\u67d0\u53f0\u673a\u5668\u4e0a\u9762\u7684\u670d\u52a1\u8fdb\u884c\u7ba1\u7406\u3002\u6bd4\u5982Windows 2000\u91cc\u9762\u7684\u201c\u63a7\u5236\u53f0\u201d\u7a0b\u5e8f(mmc.exe)\uff0c\u7528\u5b83\u6dfb\u52a0\u201c\u7ba1\u7406\u5355\u5143\u201d\u5c31\u53ef\u4ee5\u7ba1\u7406\u672c\u673a\u6216\u5176\u4ed6\u673a\u5668\u4e0a\u7684\u670d\u52a1\u3002<\/p>\n<p>Question 2. \u670d\u52a1\u7684\u5b89\u5168\u6027\u2026<\/p>\n<p>\u60f3\u8981\u5199\u4e00\u4e2a\u670d\u52a1\uff0c\u5c31\u5fc5\u987b\u719f\u6089Win NT\/2000\u7684\u5b89\u5168\u673a\u5236\uff0c\u5728\u4e0a\u8ff0\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u4e2d\uff0c\u6240\u6709\u5b89\u5168\u90fd\u662f\u57fa\u4e8e\u7528\u6237\u7684\u3002\u6362\u53e5\u8bdd\u8bf4\u2015\u2015\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u3001\u6587\u4ef6\u3001\u6ce8\u518c\u8868\u952e\u3001\u4fe1\u53f7\u3001\u4e8b\u4ef6\u7b49\u7b49\u7b49\u7b49\u90fd\u5c5e\u4e8e\u4e00\u4e2a\u7528\u6237\u3002\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u88ab\u4ea7\u751f\u7684\u65f6\u5019\uff0c\u5b83\u90fd\u662f\u6267\u884c\u5728\u4e00\u4e2a\u7528\u6237\u7684\u4e0a\u4e0b\u6587(context)\uff0c\u8fd9\u4e2a\u7528\u6237\u5e10\u53f7\u53ef\u80fd\u5728\u672c\u673a\uff0c\u4e5f\u53ef\u80fd\u5728\u7f51\u7edc\u4e2d\u7684\u5176\u4ed6\u673a\u5668\u4e0a\uff0c\u6216\u8005\u662f\u5728\u4e00\u4e2a\u7279\u6b8a\u7684\u8d26\u53f7\uff1aSystem Account\u2015\u2015\u5373\u7cfb\u7edf\u5e10\u53f7\u7684\u4e0a\u4e0b\u6587<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u6b63\u5728\u4e00\u4e2a\u7528\u6237\u5e10\u53f7\u4e0b\u6267\u884c\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8fdb\u7a0b\u5c31\u540c\u65f6\u62e5\u6709\u8fd9\u4e2a\u7528\u6237\u6240\u80fd\u62e5\u6709\u7684\u4e00\u5207\u8bbf\u95ee\u6743\u9650\uff0c\u4e0d\u8bba\u662f\u5728\u672c\u673a\u8fd8\u662f\u7f51\u7edc\u3002\u7cfb\u7edf\u5e10\u53f7\u5219\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u8d26\u53f7\uff0c\u5b83\u7528\u6765\u6807\u8bc6\u7cfb\u7edf\u672c\u8eab\uff0c\u800c\u4e14\u8fd0\u884c\u5728\u8fd9\u4e2a\u5e10\u53f7\u4e0b\u7684\u4efb\u4f55\u8fdb\u7a0b\u90fd\u62e5\u6709\u7cfb\u7edf\u4e0a\u7684\u6240\u6709\u8bbf\u95ee\u6743\u9650\uff0c\u4f46\u662f\u7cfb\u7edf\u5e10\u53f7\u4e0d\u80fd\u5728\u57df\u4e0a\u4f7f\u7528\uff0c\u65e0\u6cd5\u8bbf\u95ee\u7f51\u7edc\u8d44\u6e90\u2026<\/p>\n<p>\u670d\u52a1\u4e5f\u662fWin32\u53ef\u6267\u884c\u7a0b\u5e8f\uff0c\u5b83\u4e5f\u9700\u8981\u6267\u884c\u5728\u4e00\u4e2acontext\uff0c\u901a\u5e38\u670d\u52a1\u90fd\u662f\u5728\u7cfb\u7edf\u8d26\u53f7\u4e0b\u8fd0\u884c\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u6839\u636e\u60c5\u51b5\u9009\u62e9\u8ba9\u5b83\u8fd0\u884c\u5728\u4e00\u4e2a\u7528\u6237\u8d26\u53f7\u4e0b\uff0c\u4e5f\u5c31\u4f1a\u56e0\u6b64\u83b7\u5f97\u76f8\u5e94\u7684\u8bbf\u95ee\u8d44\u6e90\u7684\u6743\u9650\u3002<\/p>\n<p>Question 3. \u670d\u52a1\u7684\u4e09\u4e2a\u7ec4\u6210\u90e8\u5206<\/p>\n<p>\u4e00\u4e2a\u670d\u52a1\u7531\u4e09\u90e8\u5206\u7ec4\u6210\uff0c\u7b2c\u4e00\u90e8\u5206\u662fService Control Manager(SCM)\u3002\u6bcf\u4e2aWindows NT\/2000\u7cfb\u7edf\u90fd\u6709\u4e00\u4e2aSCM\uff0cSCM\u5b58\u5728\u4e8eService.exe\u4e2d\uff0c\u5728Windows\u542f\u52a8\u7684\u65f6\u5019\u4f1a\u81ea\u52a8\u8fd0\u884c\uff0c\u4f34\u968f\u7740\u64cd\u4f5c\u7cfb\u7edf\u7684\u542f\u52a8\u548c\u5173\u95ed\u800c\u4ea7\u751f\u548c\u7ec8\u6b62\u3002\u8fd9\u4e2a\u8fdb\u7a0b\u4ee5\u7cfb\u7edf\u7279\u6743\u8fd0\u884c\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e00\u4e2a\u7edf\u4e00\u7684\u3001\u5b89\u5168\u7684\u624b\u6bb5\u53bb\u63a7\u5236\u670d\u52a1\u3002\u5b83\u5176\u5b9e\u662f\u4e00\u4e2aRPC Server\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8fdc\u7a0b\u5b89\u88c5\u548c\u7ba1\u7406\u670d\u52a1\uff0c\u4e0d\u8fc7\u8fd9\u4e0d\u5728\u672c\u6587\u8ba8\u8bba\u7684\u8303\u56f4\u4e4b\u5185\u3002SCM\u5305\u542b\u4e00\u4e2a\u50a8\u5b58\u7740\u5df2\u5b89\u88c5\u7684\u670d\u52a1\u548c\u9a71\u52a8\u7a0b\u5e8f\u7684\u4fe1\u606f\u7684\u6570\u636e\u5e93\uff0c\u901a\u8fc7SCM\u53ef\u4ee5\u7edf\u4e00\u7684\u3001\u5b89\u5168\u7684\u7ba1\u7406\u8fd9\u4e9b\u4fe1\u606f\uff0c\u56e0\u6b64\u4e00\u4e2a\u670d\u52a1\u7a0b\u5e8f\u7684\u5b89\u88c5\u8fc7\u7a0b\u5c31\u662f\u5c06\u81ea\u8eab\u7684\u4fe1\u606f\u5199\u5165\u8fd9\u4e2a\u6570\u636e\u5e93\u3002<\/p>\n<p>\u7b2c\u4e8c\u90e8\u5206\u5c31\u662f\u670d\u52a1\u672c\u8eab\u3002\u4e00\u4e2a\u670d\u52a1\u62e5\u6709\u80fd\u4eceSCM\u6536\u5230\u4fe1\u53f7\u548c\u547d\u4ee4\u6240\u5fc5\u9700\u7684\u7684\u7279\u6b8a\u4ee3\u7801\uff0c\u5e76\u4e14\u80fd\u591f\u5728\u5904\u7406\u540e\u5c06\u5b83\u7684\u72b6\u6001\u56de\u4f20\u7ed9SCM\u3002<\/p>\n<p>\u7b2c\u4e09\u90e8\u5206\u4e5f\u5c31\u662f\u6700\u540e\u4e00\u90e8\u5206\uff0c\u662f\u4e00\u4e2aService Control Dispatcher(SCP)\u3002\u5b83\u662f\u4e00\u4e2a\u62e5\u6709\u7528\u6237\u754c\u9762\uff0c\u5141\u8bb8\u7528\u6237\u5f00\u59cb\u3001\u505c\u6b62\u3001\u6682\u505c\u3001\u7ee7\u7eed\uff0c\u5e76\u4e14\u63a7\u5236\u4e00\u4e2a\u6216\u591a\u4e2a\u5b89\u88c5\u5728\u8ba1\u7b97\u673a\u4e0a\u670d\u52a1\u7684Win32\u5e94\u7528\u7a0b\u5e8f\u3002SCP\u7684\u4f5c\u7528\u662f\u4e0eSCM\u901a\u8baf\uff0cWindows 2000\u7ba1\u7406\u5de5\u5177\u4e2d\u7684\u201c\u670d\u52a1\u201d\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u7684SCP\u3002<\/p>\n<p>\u5728\u8fd9\u4e09\u4e2a\u7ec4\u6210\u90e8\u5206\u4e2d\uff0c\u7528\u6237\u6700\u53ef\u80fd\u53bb\u5199\u670d\u52a1\u672c\u8eab\uff0c\u540c\u65f6\u4e5f\u53ef\u80fd\u4e0d\u5f97\u4e0d\u5199\u4e00\u4e2a\u4e0e\u5176\u4f34\u968f\u7684\u5ba2\u6237\u7aef\u7a0b\u5e8f\u4f5c\u4e3a\u4e00\u4e2aSCP\u53bb\u548cSCM\u901a\u8baf\uff0c\u672c\u6587\u53ea\u8ba8\u8bba\u53bb\u8bbe\u8ba1\u548c\u5b9e\u73b0\u4e00\u4e2a\u670d\u52a1\uff0c\u5173\u4e8e\u5982\u4f55\u53bb\u5b9e\u73b0\u4e00\u4e2aSCP\u5219\u5728\u4ee5\u540e\u7684\u5176\u5b83\u6587\u7ae0\u4e2d\u4ecb\u7ecd\u3002<\/p>\n<p>Question 4. \u600e\u6837\u5f00\u59cb\u8bbe\u8ba1\u670d\u52a1<\/p>\n<p>\u8fd8\u8bb0\u5f97\u524d\u9762\u6211\u63d0\u5230\u670d\u52a1\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u51fd\u6570\u4e00\u822c\u90fd\u662fmain()\u5417\uff1f\u4e00\u4e2a\u670d\u52a1\u62e5\u6709\u5f88\u91cd\u8981\u7684\u4e09\u4e2a\u51fd\u6570\uff0c\u7b2c\u4e00\u4e2a\u5c31\u662f\u5165\u53e3\u70b9\u51fd\u6570\uff0c\u5176\u5b9e\u7528WinMain()\u4f5c\u4e3a\u5165\u53e3\u70b9\u51fd\u6570\u4e5f\u4e0d\u662f\u4e0d\u53ef\u4ee5\uff0c\u867d\u7136\u8bf4\u670d\u52a1\u4e0d\u5e94\u8be5\u6709\u7528\u6237\u754c\u9762\uff0c\u4f46\u662f\u5176\u5b9e\u5b58\u5728\u5f88\u5c11\u7684\u51e0\u4e2a\u4f8b\u5916\uff0c\u8fd9\u5c31\u662f\u4e0b\u9762\u56fe\u4e2d\u7684\u9009\u9879\u5b58\u5728\u7684\u539f\u56e0\u3002<\/p>\n<p>\u7531\u4e8e\u8981\u548c\u7528\u6237\u684c\u9762\u8fdb\u884c\u4fe1\u606f\u4ea4\u4e92\uff0c\u670d\u52a1\u7a0b\u5e8f\u6709\u65f6\u4f1a\u4ee5WinMain()\u4f5c\u4e3a\u5165\u53e3\u70b9\u51fd\u6570\u3002<\/p>\n<p>\u5165\u53e3\u51fd\u6570\u8d1f\u8d23\u521d\u59cb\u5316\u6574\u4e2a\u8fdb\u7a0b\uff0c\u7531\u8fd9\u4e2a\u8fdb\u7a0b\u4e2d\u7684\u4e3b\u7ebf\u7a0b\u6765\u6267\u884c\u3002\u8fd9\u610f\u5473\u7740\u5b83\u5e94\u7528\u4e8e\u8fd9\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u670d\u52a1\u3002\u8981\u77e5\u9053\uff0c\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u80fd\u591f\u5305\u542b\u591a\u4e2a\u670d\u52a1\u4ee5\u4f7f\u5f97\u6267\u884c\u66f4\u52a0\u6709\u6548\u3002\u4e3b\u8fdb\u7a0b\u901a\u77e5SCM\u5728\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u542b\u6709\u51e0\u4e2a\u670d\u52a1\uff0c\u5e76\u4e14\u7ed9\u51fa\u6bcf\u4e00\u4e2a\u670d\u52a1\u7684ServiceMain\u56de\u8c03(Call Back)\u51fd\u6570\u7684\u5730\u5740\u3002\u4e00\u65e6\u5728\u53ef\u6267\u884c\u6587\u4ef6\u5185\u7684\u6240\u6709\u670d\u52a1\u90fd\u5df2\u7ecf\u505c\u6b62\u8fd0\u884c\uff0c\u4e3b\u7ebf\u7a0b\u5c31\u5728\u8fdb\u7a0b\u7ec8\u6b62\u524d\u5bf9\u6574\u4e2a\u8fdb\u7a0b\u8fdb\u884c\u6e05\u9664\u3002<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u5f88\u91cd\u8981\u7684\u51fd\u6570\u5c31\u662fServiceMain\uff0c\u6211\u770b\u8fc7\u4e00\u4e9b\u4f8b\u5b50\u7a0b\u5e8f\u91cc\u9762\u5bf9\u81ea\u5df1\u7684\u670d\u52a1\u7684\u8fdb\u5165\u70b9\u51fd\u6570\u90fd\u56fa\u5b9a\u547d\u540d\u4e3aServiceMain\uff0c\u5176\u5b9e\u5e76\u6ca1\u6709\u89c4\u5b9a\u8fc7\u4e00\u5b9a\u8981\u90a3\u6837\u547d\u540d\uff0c\u4efb\u4f55\u7684\u51fd\u6570\u53ea\u8981\u7b26\u5408\u4e0b\u5217\u7684\u5f62\u5f0f\u90fd\u53ef\u4ee5\u4f5c\u4e3a\u670d\u52a1\u7684\u8fdb\u5165\u70b9\u51fd\u6570\u3002<\/p>\n<p>VOID WINAPI ServiceMain(<br \/>\nDWORD dwArgc, \/\/ \u53c2\u6570\u4e2a\u6570<br \/>\nLPTSTR *lpszArgv \/\/ \u53c2\u6570\u4e32<br \/>\n);<\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u7531\u64cd\u4f5c\u7cfb\u7edf\u8c03\u7528\uff0c\u5e76\u6267\u884c\u80fd\u5b8c\u6210\u670d\u52a1\u7684\u4ee3\u7801\u3002\u4e00\u4e2a\u4e13\u7528\u7684\u7ebf\u7a0b\u6267\u884c\u6bcf\u4e00\u4e2a\u670d\u52a1\u7684ServiceMain\u51fd\u6570\uff0c\u6ce8\u610f\u662f\u670d\u52a1\u800c\u4e0d\u662f\u670d\u52a1\u7a0b\u5e8f\uff0c\u8fd9\u662f\u56e0\u4e3a\u6bcf\u4e2a\u670d\u52a1\u4e5f\u90fd\u62e5\u6709\u4e0e\u81ea\u5df1\u552f\u4e00\u5bf9\u5e94\u7684ServiceMain\u51fd\u6570\uff0c\u5173\u4e8e\u8fd9\u4e00\u70b9\u53ef\u4ee5\u7528\u201c\u7ba1\u7406\u5de5\u5177\u201d\u91cc\u7684\u201c\u670d\u52a1\u201d\u53bb\u5bdf\u770bWin2000\u91cc\u9762\u81ea\u5e26\u7684\u670d\u52a1\uff0c\u5c31\u4f1a\u53d1\u73b0\u5176\u5b9e\u5f88\u591a\u670d\u52a1\u90fd\u662f\u7531service.exe\u5355\u72ec\u63d0\u4f9b\u7684\u3002\u5f53\u4e3b\u7ebf\u7a0b\u8c03\u7528Win32\u51fd\u6570StartServiceCtrlDispatcher\u7684\u65f6\u5019\uff0cSCM\u4e3a\u8fd9\u4e2a\u8fdb\u7a0b\u4e2d\u7684\u6bcf\u4e00\u4e2a\u670d\u52a1\u4ea7\u751f\u4e00\u4e2a\u7ebf\u7a0b\u3002\u8fd9\u4e9b\u7ebf\u7a0b\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u548c\u5b83\u7684\u76f8\u5e94\u7684\u670d\u52a1\u7684ServiceMain\u51fd\u6570\u4e00\u8d77\u6267\u884c\uff0c\u8fd9\u5c31\u662f\u670d\u52a1\u603b\u662f\u591a\u7ebf\u7a0b\u7684\u539f\u56e0\u2015\u2015\u4e00\u4e2a\u4ec5\u6709\u4e00\u4e2a\u670d\u52a1\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5c06\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\uff0c\u5176\u5b83\u7684\u7ebf\u7a0b\u6267\u884c\u670d\u52a1\u672c\u8eab\u3002<\/p>\n<p>\u7b2c\u4e09\u4e2a\u4e5f\u5c31\u662f\u6700\u540e\u7684\u4e00\u4e2a\u91cd\u8981\u51fd\u6570\u662fCtrlHandler\uff0c\u5b83\u5fc5\u987b\u62e5\u6709\u4e0b\u9762\u7684\u539f\u578b\uff1a<\/p>\n<p>VOID WINAPI CtrlHandler(<br \/>\nDWORD fdwControl \/\/\u63a7\u5236\u547d\u4ee4<br \/>\n)<\/p>\n<p>\u50cfServiceMain\u4e00\u6837\uff0cCtrlHandler\u4e5f\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u7528\u6237\u5fc5\u987b\u4e3a\u5b83\u7684\u670d\u52a1\u7a0b\u5e8f\u4e2d\u6bcf\u4e00\u4e2a\u670d\u52a1\u5199\u4e00\u4e2a\u5355\u72ec\u7684CtrlHandler\u51fd\u6570\uff0c\u56e0\u6b64\u5982\u679c\u6709\u4e00\u4e2a\u7a0b\u5e8f\u542b\u6709\u4e24\u4e2a\u670d\u52a1\uff0c\u90a3\u4e48\u5b83\u81f3\u5c11\u8981\u62e5\u67095\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\uff1a\u4f5c\u4e3a\u5165\u53e3\u70b9\u7684main()\u6216WinMain()\uff0c\u7528\u4e8e\u7b2c\u4e00\u4e2a\u670d\u52a1\u7684ServiceMain\u51fd\u6570\u548cCtrlHandler\u51fd\u6570\uff0c\u4ee5\u53ca\u7528\u4e8e\u7b2c\u4e8c\u4e2a\u670d\u52a1\u7684ServiceMain\u51fd\u6570\u548cCtrlHandler\u51fd\u6570\u3002<\/p>\n<p>SCM\u8c03\u7528\u4e00\u4e2a\u670d\u52a1\u7684CtrlHandler\u51fd\u6570\u53bb\u6539\u53d8\u8fd9\u4e2a\u670d\u52a1\u7684\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u5f53\u67d0\u4e2a\u7ba1\u7406\u5458\u7528\u7ba1\u7406\u5de5\u5177\u91cc\u7684\u201c\u670d\u52a1\u201d\u5c1d\u8bd5\u505c\u6b62\u4f60\u7684\u670d\u52a1\u7684\u65f6\u5019\uff0c\u4f60\u7684\u670d\u52a1\u7684CtrlHandler\u51fd\u6570\u5c06\u6536\u5230\u4e00\u4e2aSERVICE_CONTROL_STOP\u901a\u77e5\u3002CtrlHandler\u51fd\u6570\u8d1f\u8d23\u6267\u884c\u505c\u6b62\u670d\u52a1\u6240\u9700\u7684\u4e00\u5207\u4ee3\u7801\u3002\u7531\u4e8e\u662f\u8fdb\u7a0b\u7684\u4e3b\u7ebf\u7a0b\u6267\u884c\u6240\u6709\u7684CtrlHandler\u51fd\u6570\uff0c\u56e0\u800c\u5fc5\u987b\u5c3d\u91cf\u4f18\u5316\u4f60\u7684CtrlHandler\u51fd\u6570\u7684\u4ee3\u7801\uff0c\u4f7f\u5b83\u8fd0\u884c\u8d77\u6765\u8db3\u591f\u5feb\uff0c\u4ee5\u4fbf\u76f8\u540c\u8fdb\u7a0b\u4e2d\u7684\u5176\u5b83\u670d\u52a1\u7684CtrlHandler\u51fd\u6570\u80fd\u5728\u9002\u5f53\u7684\u65f6\u95f4\u5185\u6536\u5230\u5c5e\u4e8e\u5b83\u4eec\u7684\u901a\u77e5\u3002\u800c\u4e14\u57fa\u4e8e\u4e0a\u8ff0\u539f\u56e0\uff0c\u4f60\u7684CtrlHandler\u51fd\u6570\u5fc5\u987b\u8981\u80fd\u591f\u5c06\u60f3\u8981\u4f20\u8fbe\u7684\u72b6\u6001\u9001\u5230\u670d\u52a1\u7ebf\u7a0b\uff0c\u8fd9\u4e2a\u4f20\u9012\u8fc7\u7a0b\u6ca1\u6709\u56fa\u5b9a\u7684\u65b9\u6cd5\uff0c\u5b8c\u5168\u53d6\u51b3\u4e8e\u4f60\u7684\u670d\u52a1\u7684\u7528\u9014\u3002<\/p>\n<p><span class=\"htd\">\u672c\u6587\u6d4b\u8bd5\u73af\u5883\u4e3aWin2000 Server + SP2<br \/>\nAthlon XP 1700 + 256MB DDR<br \/>\n\u5168\u90e8\u8d44\u6599\u53c2\u8003\u81eaMSDN OCT 2001<\/span><\/p>\n<p><span class=\"htd\">\uff08\u4e8c\uff09\u5bf9\u670d\u52a1\u7684\u6df1\u5165\u8ba8\u8bba\u4e4b\u4e0a<\/span><\/p>\n<p>\u4e0a\u4e00\u7ae0\u5176\u5b9e\u53ea\u662f\u6982\u62ec\u6027\u7684\u4ecb\u7ecd\uff0c\u4e0b\u9762\u5f00\u59cb\u624d\u662f\u771f\u6b63\u7684\u7ec6\u8282\u6240\u5728\u3002\u5728\u8fdb\u5165\u70b9\u51fd\u6570\u91cc\u9762\u8981\u5b8c\u6210ServiceMain\u7684\u521d\u59cb\u5316\uff0c\u51c6\u786e\u70b9\u8bf4\u662f\u521d\u59cb\u5316\u4e00\u4e2aSERVICE_TABLE_ENTRY\u7ed3\u6784\u6570\u7ec4\uff0c\u8fd9\u4e2a\u7ed3\u6784\u8bb0\u5f55\u4e86\u8fd9\u4e2a\u670d\u52a1\u7a0b\u5e8f\u91cc\u9762\u6240\u5305\u542b\u7684\u6240\u6709\u670d\u52a1\u7684\u540d\u79f0\u548c\u670d\u52a1\u7684\u8fdb\u5165\u70b9\u51fd\u6570\uff0c\u4e0b\u9762\u662f\u4e00\u4e2aSERVICE_TABLE_ENTRY\u7684\u4f8b\u5b50\uff1a<\/p>\n<p>SERVICE_TABLE_ENTRY service_table_entry[] =<br \/>\n{<br \/>\n{&#8220;MyFTPd&#8221; , FtpdMain},<br \/>\n{&#8220;MyHttpd&#8221;, Httpserv},<br \/>\n{NULL, NULL},<br \/>\n};<\/p>\n<p>\u7b2c\u4e00\u4e2a\u6210\u5458\u4ee3\u8868\u670d\u52a1\u7684\u540d\u5b57\uff0c\u7b2c\u4e8c\u4e2a\u6210\u5458\u662fServiceMain\u56de\u8c03\u51fd\u6570\u7684\u5730\u5740\uff0c\u4e0a\u9762\u7684\u670d\u52a1\u7a0b\u5e8f\u56e0\u4e3a\u62e5\u6709\u4e24\u4e2a\u670d\u52a1\uff0c\u6240\u4ee5\u6709\u4e09\u4e2aSERVICE_TABLE_ENTRY\u5143\u7d20\uff0c\u524d\u4e24\u4e2a\u7528\u4e8e\u670d\u52a1\uff0c\u6700\u540e\u7684NULL\u6307\u660e\u6570\u7ec4\u7684\u7ed3\u675f\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u8fd9\u4e2a\u6570\u7ec4\u7684\u5730\u5740\u88ab\u4f20\u9012\u5230StartServiceCtrlDispatcher\u51fd\u6570\uff1a<\/p>\n<p>BOOL StartServiceCtrlDispatcher(<br \/>\nLPSERVICE_TABLE_ENTRY lpServiceStartTable<br \/>\n)<\/p>\n<p>\u8fd9\u4e2aWin32\u51fd\u6570\u8868\u660e\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8fdb\u7a0b\u600e\u6837\u901a\u77e5SCM\u5305\u542b\u5728\u8fd9\u4e2a\u8fdb\u7a0b\u4e2d\u7684\u670d\u52a1\u3002\u5c31\u50cf\u4e0a\u4e00\u7ae0\u4e2d\u8bb2\u7684\u90a3\u6837\uff0cStartServiceCtrlDispatcher\u4e3a\u6bcf\u4e00\u4e2a\u4f20\u9012\u5230\u5b83\u7684\u6570\u7ec4\u4e2d\u7684\u975e\u7a7a\u5143\u7d20\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\uff0c\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u5f00\u59cb\u6267\u884c\u7531\u6570\u7ec4\u5143\u7d20\u4e2d\u7684lpServiceStartTable\u6307\u660e\u7684ServiceMain\u51fd\u6570\u3002<\/p>\n<p>SCM\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\u7a0b\u5e8f\u4e4b\u540e\uff0c\u5b83\u4f1a\u7b49\u5f85\u8be5\u7a0b\u5e8f\u7684\u4e3b\u7ebf\u7a0b\u53bb\u8c03StartServiceCtrlDispatcher\u3002\u5982\u679c\u90a3\u4e2a\u51fd\u6570\u5728\u4e24\u5206\u949f\u5185\u6ca1\u6709\u88ab\u8c03\u7528\uff0cSCM\u5c06\u4f1a\u8ba4\u4e3a\u8fd9\u4e2a\u670d\u52a1\u6709\u95ee\u9898\uff0c\u5e76\u8c03\u7528TerminateProcess\u53bb\u6740\u6b7b\u8fd9\u4e2a\u8fdb\u7a0b\u3002\u8fd9\u5c31\u8981\u6c42\u4f60\u7684\u4e3b\u7ebf\u7a0b\u8981\u5c3d\u53ef\u80fd\u5feb\u7684\u8c03\u7528StartServiceCtrlDispatcher\u3002<\/p>\n<p>StartServiceCtrlDispatcher\u51fd\u6570\u5219\u5e76\u4e0d\u7acb\u5373\u8fd4\u56de\uff0c\u76f8\u53cd\u5b83\u4f1a\u9a7b\u7559\u5728\u4e00\u4e2a\u5faa\u73af\u5185\u3002\u5f53\u5728\u8be5\u5faa\u73af\u5185\u65f6\uff0cStartServiceCtrlDispatcher\u60ac\u6302\u8d77\u81ea\u5df1\uff0c\u7b49\u5f85\u4e0b\u9762\u4e24\u4e2a\u4e8b\u4ef6\u4e2d\u7684\u4e00\u4e2a\u53d1\u751f\u3002\u7b2c\u4e00\uff0c\u5982\u679cSCM\u8981\u53bb\u9001\u4e00\u4e2a\u63a7\u5236\u901a\u77e5\u7ed9\u8fd0\u884c\u5728\u8fd9\u4e2a\u8fdb\u7a0b\u5185\u4e00\u4e2a\u670d\u52a1\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u7ebf\u7a0b\u5c31\u4f1a\u6fc0\u6d3b\u3002\u5f53\u63a7\u5236\u901a\u77e5\u5230\u8fbe\u540e\uff0c\u7ebf\u7a0b\u6fc0\u6d3b\u5e76\u8c03\u7528\u76f8\u5e94\u670d\u52a1\u7684CtrlHandler\u51fd\u6570\u3002CtrlHandler\u51fd\u6570\u5904\u7406\u8fd9\u4e2a\u670d\u52a1\u63a7\u5236\u901a\u77e5\uff0c\u5e76\u8fd4\u56de\u5230StartServiceCtrlDispatcher\u3002StartServiceCtrlDispatcher\u5faa\u73af\u56de\u53bb\u540e\u518d\u4e00\u6b21\u60ac\u6302\u81ea\u5df1\u3002<\/p>\n<p>\u7b2c\u4e8c\uff0c\u5982\u679c\u670d\u52a1\u7ebf\u7a0b\u4e2d\u7684\u4e00\u4e2a\u670d\u52a1\u4e2d\u6b62\uff0c\u8fd9\u4e2a\u7ebf\u7a0b\u4e5f\u5c06\u6fc0\u6d3b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8be5\u8fdb\u7a0b\u5c06\u8fd0\u884c\u5728\u5b83\u91cc\u9762\u7684\u670d\u52a1\u6570\u51cf\u4e00\u3002\u5982\u679c\u670d\u52a1\u6570\u4e3a\u96f6\uff0cStartServiceCtrlDispatcher\u5c31\u4f1a\u8fd4\u56de\u5230\u5165\u53e3\u70b9\u51fd\u6570\uff0c\u4ee5\u4fbf\u80fd\u591f\u6267\u884c\u4efb\u4f55\u4e0e\u8fdb\u7a0b\u6709\u5173\u7684\u6e05\u9664\u5de5\u4f5c\u5e76\u7ed3\u675f\u8fdb\u7a0b\u3002\u5982\u679c\u8fd8\u6709\u670d\u52a1\u5728\u8fd0\u884c\uff0c\u54ea\u6015\u53ea\u662f\u4e00\u4e2a\u670d\u52a1\uff0cStartServiceCtrlDispatcher\u4e5f\u4f1a\u7ee7\u7eed\u5faa\u73af\u4e0b\u53bb\uff0c\u7ee7\u7eed\u7b49\u5f85\u5176\u5b83\u7684\u63a7\u5236\u901a\u77e5\u6216\u8005\u5269\u4e0b\u7684\u670d\u52a1\u7ebf\u7a0b\u4e2d\u6b62\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u5185\u5bb9\u662f\u5173\u4e8e\u5165\u53e3\u70b9\u51fd\u6570\u7684\uff0c\u4e0b\u9762\u7684\u5185\u5bb9\u5219\u662f\u5173\u4e8eServiceMain\u51fd\u6570\u7684\u3002\u8fd8\u8bb0\u5f97\u4ee5\u524d\u8bb2\u8fc7\u7684ServiceMain\u51fd\u6570\u7684\u7684\u539f\u578b\u5417\uff1f\u4f46\u5b9e\u9645\u4e0a\u4e00\u4e2aServiceMain\u51fd\u6570\u901a\u5e38\u5ffd\u7565\u4f20\u9012\u7ed9\u5b83\u7684\u4e24\u4e2a\u53c2\u6570\uff0c\u56e0\u4e3a\u670d\u52a1\u4e00\u822c\u4e0d\u600e\u4e48\u4f20\u9012\u53c2\u6570\u3002\u8bbe\u7f6e\u4e00\u4e2a\u670d\u52a1\u6700\u597d\u7684\u65b9\u6cd5\u5c31\u662f\u8bbe\u7f6e\u6ce8\u518c\u8868\uff0c\u4e00\u822c\u670d\u52a1\u5728<br \/>\nHKEY_LOCAL_MACHINESYSTEMCurrentControlSetServiceServiceNameParameters<br \/>\n\u5b50\u952e\u4e0b\u5b58\u653e\u81ea\u5df1\u7684\u8bbe\u7f6e\uff0c\u8fd9\u91cc\u7684ServiceName\u662f\u670d\u52a1\u7684\u540d\u5b57\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ef\u80fd\u8981\u5199\u4e00\u4e2a\u5ba2\u6237\u5e94\u7528\u7a0b\u5e8f\u53bb\u8fdb\u884c\u670d\u52a1\u7684\u80cc\u666f\u8bbe\u7f6e\uff0c\u8fd9\u4e2a\u5ba2\u6237\u5e94\u7528\u7a0b\u5e8f\u5c06\u8fd9\u4e9b\u4fe1\u606f\u5b58\u5728\u6ce8\u518c\u8868\u4e2d\uff0c\u4ee5\u4fbf\u670d\u52a1\u8bfb\u53d6\u3002\u5f53\u4e00\u4e2a\u5916\u90e8\u5e94\u7528\u7a0b\u5e8f\u5df2\u7ecf\u6539\u53d8\u4e86\u67d0\u4e2a\u6b63\u5728\u8fd0\u884c\u4e2d\u7684\u670d\u52a1\u7684\u8bbe\u7f6e\u6570\u636e\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u670d\u52a1\u80fd\u591f\u7528RegNotifyChangeKeyValue\u51fd\u6570\u53bb\u63a5\u53d7\u4e00\u4e2a\u901a\u77e5\uff0c\u8fd9\u6837\u5c31\u5141\u8bb8\u670d\u52a1\u5feb\u901f\u7684\u91cd\u65b0\u8bbe\u7f6e\u81ea\u5df1\u3002<\/p>\n<p>\u524d\u9762\u8bb2\u5230StartServiceCtrlDispatcher\u4e3a\u6bcf\u4e00\u4e2a\u4f20\u9012\u5230\u5b83\u7684\u6570\u7ec4\u4e2d\u7684\u975e\u7a7a\u5143\u7d20\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u3002\u63a5\u4e0b\u6765\uff0c\u4e00\u4e2aServiceMain\u8981\u505a\u4e9b\u4ec0\u4e48\u5462\uff1fMSDN\u91cc\u9762\u7684\u539f\u6587\u662f\u8fd9\u6837\u8bf4\u7684\uff1aThe ServiceMain function should immediately call the RegisterServiceCtrlHandler function to specify a Handler function to handle control requests. Next, it should call the SetServiceStatus function to send status information to the service control manager. \u4e3a\u4ec0\u4e48\u5462\uff1f\u56e0\u4e3a\u53d1\u51fa\u542f\u52a8\u670d\u52a1\u8bf7\u6c42\u4e4b\u540e\uff0c\u5982\u679c\u5728\u4e00\u5b9a\u65f6\u95f4\u4e4b\u5185\u65e0\u6cd5\u5b8c\u6210\u670d\u52a1\u7684\u521d\u59cb\u5316\uff0cSCM\u4f1a\u8ba4\u4e3a\u670d\u52a1\u7684\u542f\u52a8\u5df2\u7ecf\u5931\u8d25\u4e86\uff0c\u8fd9\u4e2a\u65f6\u95f4\u7684\u957f\u5ea6\u5728Win NT 4.0\u4e2d\u662f80\u79d2\uff0cWin2000\u4e2d\u4e0d\u8be6&#8230;<\/p>\n<p>\u57fa\u4e8e\u4e0a\u9762\u7684\u7406\u7531\uff0cServiceMain\u8981\u8fc5\u901f\u5b8c\u6210\u81ea\u8eab\u5de5\u4f5c\uff0c\u9996\u5148\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u4e24\u9879\u5de5\u4f5c\uff0c\u7b2c\u4e00\u9879\u662f\u8c03\u7528RegisterServiceCtrlHandler\u51fd\u6570\u53bb\u901a\u77e5SCM\u5b83\u7684CtrlHandler\u56de\u8c03\u51fd\u6570\u7684\u5730\u5740\uff1a<\/p>\n<p>SERVICE_STATUS_HANDLE RegisterServiceCtrlHandler(<br \/>\nLPCTSTR lpServiceName, \/\/\u670d\u52a1\u7684\u540d\u5b57<br \/>\nLPHANDLER_FUNCTION lpHandlerProc \/\/CtrlHandler\u51fd\u6570\u5730\u5740<br \/>\n)<\/p>\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\u6307\u660e\u4f60\u6b63\u5728\u5efa\u7acb\u7684CtrlHandler\u662f\u4e3a\u54ea\u4e00\u4e2a\u670d\u52a1\u6240\u7528\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662fCtrlHandler\u51fd\u6570\u7684\u5730\u5740\u3002lpServiceName\u5fc5\u987b\u548c\u5728SERVICE_TABLE_ENTRY\u91cc\u9762\u88ab\u521d\u59cb\u5316\u7684\u670d\u52a1\u7684\u540d\u5b57\u76f8\u5339\u914d\u3002RegisterServiceCtrlHandler\u8fd4\u56de\u4e00\u4e2aSERVICE_STATUS_HANDLE\uff0c\u8fd9\u662f\u4e00\u4e2a32\u4f4d\u7684\u53e5\u67c4\u3002SCM\u7528\u5b83\u6765\u552f\u4e00\u786e\u5b9a\u8fd9\u4e2a\u670d\u52a1\u3002\u5f53\u8fd9\u4e2a\u670d\u52a1\u9700\u8981\u628a\u5b83\u5f53\u65f6\u7684\u72b6\u6001\u62a5\u544a\u7ed9SCM\u7684\u65f6\u5019\uff0c\u5c31\u5fc5\u987b\u628a\u8fd9\u4e2a\u53e5\u67c4\u4f20\u7ed9\u9700\u8981\u5b83\u7684Win32\u51fd\u6570\u3002\u6ce8\u610f\uff1a\u8fd9\u4e2a\u53e5\u67c4\u548c\u5176\u4ed6\u5927\u591a\u6570\u7684\u53e5\u67c4\u4e0d\u540c\uff0c\u4f60\u65e0\u9700\u5173\u95ed\u5b83\u3002<\/p>\n<p>SCM\u8981\u6c42ServiceMain\u51fd\u6570\u7684\u7ebf\u7a0b\u5728\u4e00\u79d2\u949f\u5185\u8c03\u7528RegisterServiceCtrlHandler\u51fd\u6570\uff0c\u5426\u5219SCM\u4f1a\u8ba4\u4e3a\u670d\u52a1\u5df2\u7ecf\u5931\u8d25\u3002\u4f46\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cSCM\u4e0d\u4f1a\u7ec8\u6b62\u670d\u52a1\uff0c\u4e0d\u8fc7\u5728NT 4\u4e2d\u5c06\u65e0\u6cd5\u542f\u52a8\u8fd9\u4e2a\u670d\u52a1\uff0c\u540c\u65f6\u4f1a\u8fd4\u56de\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u8fd9\u4e00\u70b9\u5728Windows 2000\u4e2d\u5f97\u5230\u4e86\u4fee\u6b63\u3002<\/p>\n<p>\u5728RegisterServiceCtrlHandler\u51fd\u6570\u8fd4\u56de\u540e\uff0cServiceMain\u7ebf\u7a0b\u8981\u7acb\u5373\u544a\u8bc9SCM\u670d\u52a1\u6b63\u5728\u7ee7\u7eed\u521d\u59cb\u5316\u3002\u5177\u4f53\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u8c03\u7528SetServiceStatus\u51fd\u6570\u4f20\u9012SERVICE_STATUS\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>BOOL SetServiceStatus(<br \/>\nSERVICE_STATUS_HANDLE hService, \/\/\u670d\u52a1\u7684\u53e5\u67c4<br \/>\nSERVICE_STATUS lpServiceStatus \/\/SERVICE_STATUS\u7ed3\u6784\u7684\u5730\u5740<br \/>\n)<\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u8981\u6c42\u4f20\u9012\u7ed9\u5b83\u6307\u660e\u670d\u52a1\u7684\u53e5\u67c4(\u521a\u521a\u901a\u8fc7\u8c03\u7528RegisterServiceCtrlHandler\u5f97\u5230)\uff0c\u548c\u4e00\u4e2a\u521d\u59cb\u5316\u7684SERVICE_STATUS\u7ed3\u6784\u7684\u5730\u5740\uff1a<\/p>\n<p>typedef struct _SERVICE_STATUS<br \/>\n{<br \/>\nDWORD dwServiceType;<br \/>\nDWORD dwCurrentState;<br \/>\nDWORD dwControlsAccepted;<br \/>\nDWORD dwWin32ExitCode;<br \/>\nDWORD dwServiceSpecificExitCode;<br \/>\nDWORD dwCheckPoint;<br \/>\nDWORD dwWaitHint;<br \/>\n} SERVICE_STATUS, *LPSERVICE_STATUS;<\/p>\n<p>SERVICE_STATUS\u7ed3\u6784\u542b\u6709\u4e03\u4e2a\u6210\u5458\uff0c\u5b83\u4eec\u53cd\u6620\u670d\u52a1\u7684\u73b0\u884c\u72b6\u6001\u3002\u6240\u6709\u8fd9\u4e9b\u6210\u5458\u5fc5\u987b\u5728\u8fd9\u4e2a\u7ed3\u6784\u88ab\u4f20\u9012\u5230SetServiceStatus\u4e4b\u524d\u6b63\u786e\u7684\u8bbe\u7f6e\u3002<\/p>\n<p>\u6210\u5458dwServiceType\u6307\u660e\u670d\u52a1\u53ef\u6267\u884c\u6587\u4ef6\u7684\u7c7b\u578b\u3002\u5982\u679c\u4f60\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u53ea\u6709\u4e00\u4e2a\u5355\u72ec\u7684\u670d\u52a1\uff0c\u5c31\u628a\u8fd9\u4e2a\u6210\u5458\u8bbe\u7f6e\u6210SERVICE_WIN32_OWN_PROCESS\uff1b\u5982\u679c\u62e5\u6709\u591a\u4e2a\u670d\u52a1\u7684\u8bdd\uff0c\u5c31\u8bbe\u7f6e\u6210SERVICE_WIN32_SHARE_PROCESS\u3002\u9664\u4e86\u8fd9\u4e24\u4e2a\u6807\u5fd7\u4e4b\u5916\uff0c\u5982\u679c\u4f60\u7684\u670d\u52a1\u9700\u8981\u548c\u684c\u9762\u53d1\u751f\u4ea4\u4e92(\u5f53\u7136\u4e0d\u63a8\u8350\u8fd9\u6837\u505a)\uff0c\u5c31\u8981\u7528\u201cOR\u201d\u8fd0\u7b97\u7b26\u9644\u52a0\u4e0aSERVICE_INTERACTIVE_PROCESS\u3002\u8fd9\u4e2a\u6210\u5458\u7684\u503c\u5728\u4f60\u7684\u670d\u52a1\u7684\u751f\u5b58\u671f\u5185\u7edd\u5bf9\u4e0d\u5e94\u8be5\u6539\u53d8\u3002<\/p>\n<p>\u6210\u5458dwCurrentState\u662f\u8fd9\u4e2a\u7ed3\u6784\u4e2d\u6700\u91cd\u8981\u7684\u6210\u5458\uff0c\u5b83\u5c06\u544a\u8bc9SCM\u4f60\u7684\u670d\u52a1\u7684\u73b0\u884c\u72b6\u6001\u3002\u4e3a\u4e86\u62a5\u544a\u670d\u52a1\u4ecd\u5728\u521d\u59cb\u5316\uff0c\u5e94\u8be5\u628a\u8fd9\u4e2a\u6210\u5458\u8bbe\u7f6e\u6210SERVICE_START_PENDING\u3002\u5728\u4ee5\u540e\u5177\u4f53\u8bb2\u8ff0CtrlHandler\u51fd\u6570\u7684\u65f6\u5019\u5177\u4f53\u89e3\u91ca\u5176\u5b83\u53ef\u80fd\u7684\u503c\u3002<\/p>\n<p>\u6210\u5458dwControlsAccepted\u6307\u660e\u670d\u52a1\u613f\u610f\u63a5\u53d7\u4ec0\u4e48\u6837\u7684\u63a7\u5236\u901a\u77e5\u3002\u5982\u679c\u4f60\u5141\u8bb8\u4e00\u4e2aSCP\u53bb\u6682\u505c\/\u7ee7\u7eed\u670d\u52a1\uff0c\u5c31\u628a\u5b83\u8bbe\u6210SERVICE_ACCEPT_PAUSE_CONTINUE\u3002\u5f88\u591a\u670d\u52a1\u4e0d\u652f\u6301\u6682\u505c\u6216\u7ee7\u7eed\uff0c\u5c31\u5fc5\u987b\u81ea\u5df1\u51b3\u5b9a\u5728\u670d\u52a1\u4e2d\u5b83\u662f\u5426\u53ef\u7528\u3002\u5982\u679c\u4f60\u5141\u8bb8\u4e00\u4e2aSCP\u53bb\u505c\u6b62\u670d\u52a1\uff0c\u5c31\u8981\u8bbe\u7f6e\u5b83\u4e3aSERVICE_ACCEPT_STOP\u3002\u5982\u679c\u670d\u52a1\u8981\u5728\u64cd\u4f5c\u7cfb\u7edf\u5173\u95ed\u7684\u65f6\u5019\u5f97\u5230\u901a\u77e5\uff0c\u8bbe\u7f6e\u5b83\u4e3aSERVICE_ACCEPT_SHUTDOWN\u53ef\u4ee5\u6536\u5230\u9884\u671f\u7684\u7ed3\u679c\u3002\u8fd9\u4e9b\u6807\u5fd7\u53ef\u4ee5\u7528\u201cOR\u201d\u8fd0\u7b97\u7b26\u7ec4\u5408\u3002<\/p>\n<p>\u6210\u5458dwWin32ExitCode\u548cdwServiceSpecificExitCode\u662f\u5141\u8bb8\u670d\u52a1\u62a5\u544a\u9519\u8bef\u7684\u5173\u952e\uff0c\u5982\u679c\u5e0c\u671b\u670d\u52a1\u53bb\u62a5\u544a\u4e00\u4e2aWin32\u9519\u8bef\u4ee3\u7801(\u9884\u5b9a\u4e49\u5728WinError.h\u4e2d)\uff0c\u5b83\u5c31\u8bbe\u7f6edwWin32ExitCode\u4e3a\u9700\u8981\u7684\u4ee3\u7801\u3002\u4e00\u4e2a\u670d\u52a1\u4e5f\u53ef\u4ee5\u62a5\u544a\u5b83\u672c\u8eab\u7279\u6709\u7684\u3001\u6ca1\u6709\u6620\u5c04\u5230\u4e00\u4e2a\u9884\u5b9a\u4e49\u7684Win32\u9519\u8bef\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u3002\u4e3a\u4e86\u8fd9\u4e00\u70b9\uff0c\u8981\u628adwWin32ExitCode\u8bbe\u7f6e\u4e3aERROR_SERVICE_SPECIFIC_ERROR\uff0c\u7136\u540e\u8fd8\u8981\u8bbe\u7f6e\u6210\u5458dwServiceSpecificExitCode\u4e3a\u670d\u52a1\u7279\u6709\u7684\u9519\u8bef\u4ee3\u7801\u3002\u5f53\u670d\u52a1\u8fd0\u884c\u6b63\u5e38\uff0c\u6ca1\u6709\u9519\u8bef\u53ef\u4ee5\u62a5\u544a\u7684\u65f6\u5019\uff0c\u5c31\u8bbe\u7f6e\u6210\u5458dwWin32ExitCode\u4e3aNO_ERROR\u3002<\/p>\n<p>\u6700\u540e\u7684\u4e24\u4e2a\u6210\u5458dwCheckPoint\u548cdwWaitHint\u662f\u4e00\u4e2a\u670d\u52a1\u7528\u6765\u62a5\u544a\u5b83\u5f53\u524d\u7684\u4e8b\u4ef6\u8fdb\u5c55\u60c5\u51b5\u7684\u3002\u5f53\u6210\u5458dwCurrentState\u88ab\u8bbe\u7f6e\u6210SERVICE_START_PENDING\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u628adwCheckPoint\u8bbe\u62100\uff0cdwWaitHint\u8bbe\u6210\u4e00\u4e2a\u7ecf\u8fc7\u591a\u6b21\u5c1d\u8bd5\u540e\u786e\u5b9a\u6bd4\u8f83\u5408\u9002\u7684\u6570\uff0c\u8fd9\u6837\u670d\u52a1\u624d\u80fd\u9ad8\u6548\u8fd0\u884c\u3002\u4e00\u65e6\u670d\u52a1\u88ab\u5b8c\u5168\u521d\u59cb\u5316\uff0c\u5c31\u5e94\u8be5\u91cd\u65b0\u521d\u59cb\u5316SERVICE_STATUS\u7ed3\u6784\u7684\u6210\u5458\uff0c\u66f4\u6539dwCurrentState\u4e3aSERVICE_RUNNING\uff0c\u7136\u540e\u628adwCheckPoint\u548cdwWaitHint\u90fd\u6539\u4e3a0\u3002<\/p>\n<p>dwCheckPoint\u6210\u5458\u7684\u5b58\u5728\u5bf9\u7528\u6237\u662f\u6709\u76ca\u7684\uff0c\u5b83\u5141\u8bb8\u4e00\u4e2a\u670d\u52a1\u62a5\u544a\u5b83\u5904\u4e8e\u8fdb\u7a0b\u7684\u54ea\u4e00\u6b65\u3002\u6bcf\u4e00\u6b21\u8c03\u7528SetServiceStatus\u65f6\uff0c\u53ef\u4ee5\u589e\u52a0\u5b83\u5230\u4e00\u4e2a\u80fd\u6307\u660e\u670d\u52a1\u5df2\u7ecf\u6267\u884c\u5230\u54ea\u4e00\u6b65\u7684\u6570\u5b57\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u51b3\u5b9a\u591a\u957f\u65f6\u95f4\u62a5\u544a\u4e00\u6b21\u670d\u52a1\u7684\u8fdb\u5c55\u60c5\u51b5\u3002\u5982\u679c\u51b3\u5b9a\u8981\u62a5\u544a\u670d\u52a1\u7684\u521d\u59cb\u5316\u8fdb\u7a0b\u7684\u6bcf\u4e00\u6b65\uff0c\u5c31\u5e94\u8be5\u8bbe\u7f6edwWaitHint\u4e3a\u4f60\u8ba4\u4e3a\u5230\u8fbe\u4e0b\u4e00\u6b65\u6240\u9700\u7684\u6beb\u79d2\u6570\uff0c\u800c\u4e0d\u662f\u670d\u52a1\u5b8c\u6210\u5b83\u7684\u8fdb\u7a0b\u6240\u9700\u7684\u6beb\u79d2\u6570\u3002<\/p>\n<p>\u5728\u670d\u52a1\u7684\u6240\u6709\u521d\u59cb\u5316\u90fd\u5b8c\u6210\u4e4b\u540e\uff0c\u670d\u52a1\u8c03\u7528SetServiceStatus\u6307\u660eSERVICE_RUNNING\uff0c\u5728\u90a3\u4e00\u523b\u670d\u52a1\u5df2\u7ecf\u5f00\u59cb\u8fd0\u884c\u3002\u901a\u5e38\u4e00\u4e2a\u670d\u52a1\u662f\u628a\u81ea\u5df1\u653e\u5728\u4e00\u4e2a\u5faa\u73af\u4e4b\u4e2d\u6765\u8fd0\u884c\u7684\u3002\u5728\u5faa\u73af\u7684\u5185\u90e8\u8fd9\u4e2a\u670d\u52a1\u8fdb\u7a0b\u60ac\u6302\u81ea\u5df1\uff0c\u7b49\u5f85\u6307\u660e\u5b83\u4e0b\u4e00\u6b65\u662f\u5e94\u8be5\u6682\u505c\u3001\u7ee7\u7eed\u6216\u505c\u6b62\u4e4b\u7c7b\u7684\u7f51\u7edc\u8bf7\u6c42\u6216\u901a\u77e5\u3002\u5f53\u4e00\u4e2a\u8bf7\u6c42\u5230\u8fbe\u7684\u65f6\u5019\uff0c\u670d\u52a1\u7ebf\u7a0b\u6fc0\u6d3b\u5e76\u5904\u7406\u8fd9\u4e2a\u8bf7\u6c42\uff0c\u7136\u540e\u518d\u5faa\u73af\u56de\u53bb\u7b49\u5f85\u4e0b\u4e00\u4e2a\u8bf7\u6c42\/\u901a\u77e5\u3002<\/p>\n<p><span class=\"htd\">\u3000\u3000\u5982\u679c\u4e00\u4e2a\u670d\u52a1\u7531\u4e8e\u4e00\u4e2a\u901a\u77e5\u800c\u6fc0\u6d3b\uff0c\u5b83\u4f1a\u5148\u5904\u7406\u8fd9\u4e2a\u901a\u77e5\uff0c\u9664\u975e\u8fd9\u4e2a\u670d\u52a1\u5f97\u5230\u7684\u662f\u505c\u6b62\u6216\u5173\u95ed\u7684\u901a\u77e5\u3002\u5982\u679c\u771f\u7684\u662f\u505c\u6b62\u6216\u5173\u95ed\u7684\u901a\u77e5\uff0c\u670d\u52a1\u7ebf\u7a0b\u5c06\u9000\u51fa\u5faa\u73af\uff0c\u6267\u884c\u5fc5\u8981\u7684\u6e05\u9664\u64cd\u4f5c\uff0c\u7136\u540e\u4ece\u8fd9\u4e2a\u7ebf\u7a0b\u8fd4\u56de\u3002\u5f53ServiceMain\u7ebf\u7a0b\u8fd4\u56de\u5e76\u4e2d\u6b62\u65f6\uff0c\u5f15\u8d77\u5728StartServiceCtrlDispatcher\u5185\u7761\u7720\u7684\u7ebf\u7a0b\u6fc0\u6d3b\uff0c\u5e76\u50cf\u5728\u524d\u9762\u89e3\u91ca\u8fc7\u7684\u90a3\u6837\uff0c\u51cf\u5c11\u5b83\u8fd0\u884c\u7684\u670d\u52a1\u7684\u8ba1\u6570\u3002<\/span><\/p>\n<p>\uff08\u4e09\uff09\u5bf9\u670d\u52a1\u7684\u6df1\u5165\u8ba8\u8bba\u4e4b\u4e0b<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u8fd8\u5269\u4e0b\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u5728\u7ec6\u8282\u4e0a\u8ba8\u8bba\uff0c\u90a3\u5c31\u662f\u670d\u52a1\u7684CtrlHandler\u51fd\u6570\u3002<\/p>\n<p>\u5f53\u8c03\u7528RegisterServiceCtrlHandler\u51fd\u6570\u65f6\uff0cSCM\u5f97\u5230\u5e76\u4fdd\u5b58\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u7684\u5730\u5740\u3002\u4e00\u4e2aSCP\u8c03\u4e00\u4e2a\u544a\u8bc9SCM\u5982\u4f55\u53bb\u63a7\u5236\u670d\u52a1\u7684Win32\u51fd\u6570\uff0c\u73b0\u5728\u5df2\u7ecf\u670910\u4e2a\u9884\u5b9a\u4e49\u7684\u63a7\u5236\u8bf7\u6c42\uff1a<\/p>\n<table border=\"2\" cellspacing=\"0\" cellpadding=\"1\" align=\"center\">\n<tbody>\n<tr valign=\"top\" bgcolor=\"#cccccc\">\n<th align=\"left\">\n<div align=\"center\">Control code<\/div>\n<\/th>\n<th align=\"left\">\n<div align=\"center\">Meaning<\/div>\n<\/th>\n<\/tr>\n<tr valign=\"top\">\n<td>SERVICE_CONTROL_STOP<\/td>\n<td>Requests the service to stop. The<em>hService<\/em>\u00a0handle must have SERVICE_STOP access.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SERVICE_CONTROL_PAUSE<\/td>\n<td>Requests the service to pause. The<em>hService<\/em>\u00a0handle must have SERVICE_PAUSE_CONTINUE access.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SERVICE_CONTROL_CONTINUE<\/td>\n<td>Requests the paused service to resume. The\u00a0<em>hService<\/em>\u00a0handle must have SERVICE_PAUSE_CONTINUE access.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SERVICE_CONTROL_INTERROGATE<\/td>\n<td>Requests the service to update immediately its current status information to the service control manager. The\u00a0<em>hService<\/em>\u00a0handle must have SERVICE_INTERROGATE access.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SERVICE_CONTROL_SHUTDOWN<\/td>\n<td>Requests the service to perform cleanup tasks, because the system is shutting down. For more information, see Remarks.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SERVICE_CONTROL_PARAMCHANGE<\/td>\n<td><strong>Windows 2000:<\/strong>\u00a0Requests the service to reread its startup parameters. The\u00a0<em>hService<\/em>\u00a0handle must have SERVICE_PAUSE_CONTINUE access.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SERVICE_CONTROL_NETBINDCHANGE<\/td>\n<td><strong>Windows 2000:<\/strong>\u00a0Requests the service to update its network binding. The\u00a0<em>hService<\/em>\u00a0handle must have SERVICE_PAUSE_CONTINUE access.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SERVICE_CONTROL_NETBINDREMOVE<\/td>\n<td><strong>Windows 2000:<\/strong>\u00a0Notifies a network service that a component for binding has been removed. The service should reread its binding information and unbind from the removed component.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SERVICE_CONTROL_NETBINDENABLE<\/td>\n<td><strong>Windows 2000:<\/strong>\u00a0Notifies a network service that a disabled binding has been enabled. The service should reread its binding information and add the new binding.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SERVICE_CONTROL_NETBINDDISABLE<\/td>\n<td><strong>Windows 2000:<\/strong>\u00a0Notifies a network service that one of its bindings has been disabled. The service should reread its binding information and remove the binding.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0a\u8868\u4e2d\u6807\u6709<strong>Windows 2000<\/strong>\u5b57\u6837\u7684\u5c31\u662f2000\u4e2d\u65b0\u6dfb\u52a0\u7684\u63a7\u5236\u4ee3\u7801\u3002\u9664\u4e86\u8fd9\u4e9b\u4ee3\u7801\u4e4b\u5916\uff0c\u670d\u52a1\u4e5f\u53ef\u4ee5\u63a5\u53d7\u7528\u6237\u5b9a\u4e49\u7684\uff0c\u8303\u56f4\u5728128-255\u4e4b\u95f4\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u5f53CtrlHandler\u51fd\u6570\u6536\u5230\u4e00\u4e2aSERVICE_CONTROL_STOP\u3001SERVICE_CONTROL_PAUSE\u3001SERVICE_CONTROL_CONTINUE\u63a7\u5236\u4ee3\u7801\u7684\u65f6\u5019\uff0cSetServiceStatus\u5fc5\u987b\u88ab\u8c03\u7528\u53bb\u786e\u8ba4\u8fd9\u4e2a\u4ee3\u7801\uff0c\u5e76\u6307\u5b9a\u4f60\u8ba4\u4e3a\u670d\u52a1\u5904\u7406\u8fd9\u4e2a\u72b6\u6001\u53d8\u5316\u6240\u9700\u8981\u7684\u65f6\u95f4\u3002<\/p>\n<p>\u4f8b\u5982\uff1a\u4f60\u7684\u670d\u52a1\u6536\u5230\u4e86\u505c\u6b62\u8bf7\u6c42\uff0c\u9996\u5148\u8981\u628aSERVICE_STATUS\u7ed3\u6784\u7684dwCurrentState\u6210\u5458\u8bbe\u7f6e\u6210SERVICE_STOP_PENDING\uff0c\u8fd9\u6837\u53ef\u4ee5\u4f7fSCM\u786e\u5b9a\u4f60\u5df2\u7ecf\u6536\u5230\u4e86\u63a7\u5236\u4ee3\u7801\u3002\u5f53\u4e00\u4e2a\u670d\u52a1\u7684\u6682\u505c\u6216\u505c\u6b62\u64cd\u4f5c\u6b63\u5728\u6267\u884c\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u6307\u5b9a\u4f60\u8ba4\u4e3a\u8fd9\u79cd\u64cd\u4f5c\u6240\u9700\u8981\u7684\u65f6\u95f4\uff1a\u8fd9\u662f\u56e0\u4e3a\u4e00\u4e2a\u670d\u52a1\u4e5f\u8bb8\u4e0d\u80fd\u7acb\u5373\u6539\u53d8\u5b83\u7684\u72b6\u6001\uff0c\u5b83\u53ef\u80fd\u5fc5\u987b\u7b49\u5f85\u4e00\u4e2a\u7f51\u7edc\u8bf7\u6c42\u88ab\u5b8c\u6210\u6216\u8005\u6570\u636e\u88ab\u5237\u65b0\u5230\u4e00\u4e2a\u9a71\u52a8\u5668\u4e0a\u3002\u6307\u5b9a\u65f6\u95f4\u7684\u65b9\u6cd5\u5c31\u50cf\u6211\u4e0a\u4e00\u7ae0\u8bf4\u7684\u90a3\u6837\uff0c\u7528\u6210\u5458dwCheckPoint\u548cdwWaitHint\u6765\u6307\u660e\u5b83\u5b8c\u6210\u72b6\u6001\u6539\u53d8\u6240\u9700\u8981\u7684\u65f6\u95f4\u3002\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u7528\u589e\u52a0dwCheckPoint\u6210\u5458\u7684\u503c\u548c\u8bbe\u7f6edwWaitHint\u6210\u5458\u7684\u503c\u53bb\u6307\u660e\u4f60\u671f\u5f85\u7684\u670d\u52a1\u5230\u8fbe\u4e0b\u4e00\u6b65\u7684\u65f6\u95f4\u7684\u65b9\u5f0f\u5468\u671f\u6027\u7684\u62a5\u544a\u8fdb\u5c55\u60c5\u51b5\u3002<\/p>\n<p>\u5f53\u6574\u4e2a\u542f\u52a8\u7684\u8fc7\u7a0b\u5b8c\u6210\u4e4b\u540e\uff0c\u8981\u518d\u4e00\u6b21\u8c03\u7528SetServiceStatus\u3002\u8fd9\u65f6\u5c31\u8981\u628aSERVICE_STATUS\u7ed3\u6784\u7684dwCurrentState\u6210\u5458\u8bbe\u7f6e\u6210SERVICE_STOPPED\uff0c\u5f53\u62a5\u544a\u72b6\u6001\u4ee3\u7801\u7684\u540c\u65f6\uff0c\u4e00\u5b9a\u8981\u628a\u6210\u5458dwCheckPoint\u548cdwWaitHint\u8bbe\u7f6e\u4e3a0\uff0c\u56e0\u4e3a\u670d\u52a1\u5df2\u7ecf\u5b8c\u6210\u4e86\u5b83\u7684\u72b6\u6001\u53d8\u5316\u3002\u6682\u505c\u6216\u7ee7\u7eed\u670d\u52a1\u7684\u65f6\u5019\u65b9\u6cd5\u4e5f\u4e00\u6837\u3002<\/p>\n<p>\u5f53CtrlHandler\u51fd\u6570\u6536\u5230\u4e00\u4e2aSERVICE_CONTROL_INTERROGATE\u63a7\u5236\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u670d\u52a1\u5c06\u7b80\u5355\u7684\u5c06dwCurrentState\u6210\u5458\u8bbe\u7f6e\u6210\u670d\u52a1\u5f53\u524d\u7684\u72b6\u6001\uff0c\u540c\u65f6\uff0c\u628a\u6210\u5458dwCheckPoint\u548cdwWaitHint\u8bbe\u7f6e\u4e3a0\uff0c\u7136\u540e\u518d\u8c03\u7528SetServiceStatus\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u5728\u64cd\u4f5c\u7cfb\u7edf\u5173\u95ed\u7684\u65f6\u5019\uff0cCtrlHandler\u51fd\u6570\u6536\u5230\u4e00\u4e2aSERVICE_CONTROL_SHUTDOWN\u63a7\u5236\u4ee3\u7801\u3002\u670d\u52a1\u6839\u672c\u65e0\u987b\u56de\u5e94\u8fd9\u4e2a\u4ee3\u7801\uff0c\u56e0\u4e3a\u7cfb\u7edf\u5373\u5c06\u5173\u95ed\u3002\u5b83\u5c06\u6267\u884c\u4fdd\u5b58\u6570\u636e\u6240\u9700\u8981\u7684\u6700\u5c0f\u884c\u52a8\u96c6\uff0c\u8fd9\u662f\u4e3a\u4e86\u786e\u5b9a\u673a\u5668\u80fd\u53ca\u65f6\u5173\u95ed\u3002\u7f3a\u7701\u65f6\u7cfb\u7edf\u53ea\u7ed9\u5f88\u5c11\u7684\u65f6\u95f4\u53bb\u5173\u95ed\u6240\u6709\u7684\u670d\u52a1\uff0cMSDN\u91cc\u9762\u8bf4\u5927\u6982\u662f20\u79d2\u7684\u65f6\u95f4\uff0c\u4e0d\u8fc7\u90a3\u53ef\u80fd\u662f<strong>Windows NT 4<\/strong>\u7684\u8bbe\u7f6e\uff0c\u5728\u6211\u7684<strong>Windows 2000 Server<\/strong>\u91cc\u8fd9\u4e2a\u65f6\u95f4\u662f10\u79d2\uff0c\u4f60\u53ef\u4ee5\u624b\u52a8\u7684\u4fee\u6539\u8fd9\u4e2a\u6570\u503c\uff0c\u5b83\u88ab\u8bb0\u5f55\u5728<strong>HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControl<\/strong>\u5b50\u952e\u91cc\u9762\u7684<strong>WaitToKillServiceTimeout<\/strong>\uff0c\u5355\u4f4d\u662f\u6beb\u79d2\u3002<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"http:\/\/www.frontfree.net\/articles\/pages\/0000000527\/serv05.gif\" alt=\"\" \/><\/p>\n<p>\u3000\u3000\u5f53CtrlHandler\u51fd\u6570\u6536\u5230\u4efb\u4f55\u7528\u6237\u5b9a\u4e49\u7684\u4ee3\u7801\u65f6\uff0c\u5b83\u5e94\u8be5\u6267\u884c\u671f\u671b\u7684\u7528\u6237\u81ea\u5b9a\u4e49\u884c\u52a8\u3002\u9664\u975e\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u884c\u52a8\u8981\u5f3a\u5236\u670d\u52a1\u53bb\u6682\u505c\u3001\u7ee7\u7eed\u6216\u505c\u6b62\uff0c\u5426\u5219\u4e0d\u8c03SetServiceStatus\u51fd\u6570\u3002\u5982\u679c\u7528\u6237\u5b9a\u4e49\u7684\u884c\u52a8\u5f3a\u8feb\u670d\u52a1\u7684\u72b6\u6001\u53d1\u751f\u53d8\u5316\uff0cSetServiceStatus\u5c06\u88ab\u8c03\u7528\u53bb\u8bbe\u7f6edwCurrentState\u3001dwCheckPoint\u548cdwWaitHint\uff0c\u5177\u4f53\u63a7\u5236\u4ee3\u7801\u548c\u524d\u9762\u8bf4\u7684\u4e00\u6837\u3002<\/p>\n<p>\u5982\u679c\u4f60\u7684CtrlHandler\u51fd\u6570\u9700\u8981\u5f88\u957f\u7684\u65f6\u95f4\u6267\u884c\u64cd\u4f5c\u7684\u8bdd\uff0c\u5343\u4e07\u8981\u6ce8\u610f\uff1a\u5047\u5982CtrlHandler\u51fd\u6570\u572830\u79d2\u5185\u6ca1\u6709\u8fd4\u56de\u7684\u8bdd\uff0cSCM\u5c06\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\uff0c\u8fd9\u4e0d\u662f\u6211\u4eec\u6240\u671f\u671b\u7684\u3002\u6240\u4ee5\u5982\u679c\u51fa\u73b0\u4e0a\u8ff0\u60c5\u51b5\uff0c\u6700\u597d\u7684\u529e\u6cd5\u662f\u518d\u5efa\u7acb\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u8ba9\u5b83\u53bb\u7ee7\u7eed\u6267\u884c\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u4f7f\u5f97CtrlHandler\u51fd\u6570\u80fd\u591f\u8fc5\u901f\u7684\u8fd4\u56de\u3002\u4f8b\u5982\uff0c\u5f53\u6536\u5230\u4e00\u4e2aSERVICE_CONTROL_STOP\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u5c31\u50cf\u4e0a\u9762\u8bf4\u7684\u4e00\u6837\uff0c\u670d\u52a1\u53ef\u80fd\u6b63\u5728\u7b49\u5f85\u4e00\u4e2a\u7f51\u7edc\u8bf7\u6c42\u88ab\u5b8c\u6210\u6216\u8005\u6570\u636e\u88ab\u5237\u65b0\u5230\u4e00\u4e2a\u9a71\u52a8\u5668\u4e0a\uff0c\u800c\u8fd9\u4e9b\u64cd\u4f5c\u6240\u9700\u8981\u7684\u65f6\u95f4\u662f\u4f60\u4e0d\u80fd\u4f30\u8ba1\u7684\uff0c\u90a3\u4e48\u5c31\u8981\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u7b49\u5f85\u64cd\u4f5c\u5b8c\u6210\u540e\u6267\u884c\u505c\u6b62\u547d\u4ee4\uff0cCtrlHandler\u51fd\u6570\u5728\u8fd4\u56de\u4e4b\u524d\u4ecd\u7136\u8981\u62a5\u544aSERVICE_STOP_PENDING\u72b6\u6001\uff0c\u5f53\u65b0\u7684\u7ebf\u7a0b\u6267\u884c\u5b8c\u64cd\u4f5c\u4e4b\u540e\uff0c\u518d\u7531\u5b83\u5c06\u670d\u52a1\u7684\u72b6\u6001\u8bbe\u7f6e\u6210SERVICE_STOPPED\u3002\u5982\u679c\u5f53\u524d\u64cd\u4f5c\u7684\u65f6\u95f4\u53ef\u4ee5\u4f30\u8ba1\u7684\u5230\u5c31\u4e0d\u8981\u8fd9\u6837\u505a\uff0c\u4ecd\u7136\u4f7f\u7528\u524d\u9762\u4ea4\u5f85\u7684\u65b9\u6cd5\u5904\u7406\u3002<\/p>\n<p>CtrlHandler\u51fd\u6570\u6211\u5c31\u5148\u8bb2\u8fd9\u4e9b\uff0c\u4e0b\u9762\u8bf4\u8bf4\u670d\u52a1\u600e\u4e48\u5b89\u88c5\u3002\u4e00\u4e2a\u670d\u52a1\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528CreateService\u51fd\u6570\u5c06\u670d\u52a1\u7684\u4fe1\u606f\u6dfb\u52a0\u5230SCM\u7684\u6570\u636e\u5e93\u3002<\/p>\n<p>SC_HANDLE CreateService( SC_HANDLE hSCManager, \/\/ handle to SCM database LPCTSTR lpServiceName, \/\/ name of service to start LPCTSTR lpDisplayName, \/\/ display name DWORD dwDesiredAccess, \/\/ type of access to service DWORD dwServiceType, \/\/ type of service DWORD dwStartType, \/\/ when to start service DWORD dwErrorControl, \/\/ severity of service failure LPCTSTR lpBinaryPathName, \/\/ name of binary file LPCTSTR lpLoadOrderGroup, \/\/ name of load ordering group LPDWORD lpdwTagId, \/\/ tag identifier LPCTSTR lpDependencies, \/\/ array of dependency names LPCTSTR lpServiceStartName, \/\/ account name LPCTSTR lpPassword \/\/ account password );<\/p>\n<p>hSCManager\u662f\u4e00\u4e2a\u6807\u793aSCM\u6570\u636e\u5e93\u7684\u53e5\u67c4\uff0c\u53ef\u4ee5\u7b80\u5355\u7684\u901a\u8fc7\u8c03\u7528OpenSCManager\u5f97\u5230\u3002<\/p>\n<p>SC_HANDLE OpenSCManager( LPCTSTR lpMachineName, \/\/ computer name LPCTSTR lpDatabaseName, \/\/ SCM database name DWORD dwDesiredAccess \/\/ access type );<\/p>\n<p>lpMachineName\u662f\u76ee\u6807\u673a\u5668\u7684\u540d\u5b57\uff0c\u8fd8\u8bb0\u5f97\u6211\u5728\u7b2c\u4e00\u7ae0\u91cc\u8bf4\u8fc7\u53ef\u4ee5\u5728\u5176\u5b83\u7684\u673a\u5668\u4e0a\u9762\u5b89\u88c5\u670d\u52a1\u5417\uff1f\u8fd9\u5c31\u662f\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002\u5bf9\u65b9\u673a\u5668\u540d\u5b57\u5fc5\u987b\u4ee5\u201c\\\u201d\u5f00\u59cb\u3002\u5982\u679c\u4f20\u9012NULL\u6216\u8005\u4e00\u4e2a\u7a7a\u7684\u5b57\u7b26\u4e32\u7684\u8bdd\u5c31\u9ed8\u8ba4\u662f\u672c\u673a\u3002<\/p>\n<p>lpDatabaseName\u662f\u76ee\u6807\u673a\u5668\u4e0a\u9762SCM\u6570\u636e\u5e93\u7684\u540d\u5b57\uff0c\u4f46MSDN\u91cc\u9762\u8bf4\u8fd9\u4e2a\u53c2\u6570\u8981\u9ed8\u8ba4\u7684\u8bbe\u7f6e\u6210SERVICES_ACTIVE_DATABASE\uff0c\u5982\u679c\u4f20\u9012NULL\uff0c\u5c31\u9ed8\u8ba4\u7684\u6253\u5f00SERVICES_ACTIVE_DATABASE\u3002\u6240\u4ee5\u6211\u8fd8\u6ca1\u6709\u771f\u7684\u641e\u660e\u767d\u8fd9\u4e2a\u53c2\u6570\u7684\u5b58\u5728\u610f\u4e49\uff0c\u603b\u4e4b\u4f7f\u7528\u7684\u65f6\u5019\u4f20\u9012NULL\u5c31\u884c\u4e86\u3002<\/p>\n<p>dwDesiredAccess\u662fSCM\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u5177\u4f53\u503c\u89c1\u4e0b\u8868\uff1a<\/p>\n<table border=\"2\" cellspacing=\"0\" cellpadding=\"1\" align=\"center\">\n<tbody>\n<tr valign=\"top\" bgcolor=\"#cccccc\">\n<th align=\"left\">\n<div align=\"center\">Object access<\/div>\n<\/th>\n<th align=\"left\">\n<div align=\"center\">Description<\/div>\n<\/th>\n<\/tr>\n<tr valign=\"top\">\n<td>SC_MANAGER_ALL_ACCESS<\/td>\n<td>Includes STANDARD_RIGHTS_REQUIRED, in addition to all of the access types listed in this table.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SC_MANAGER_CONNECT<\/td>\n<td>Enables connecting to the service control manager.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SC_MANAGER_CREATE_SERVICE<\/td>\n<td>Enables calling of the<strong>CreateService<\/strong>\u00a0function to create a service object and add it to the database.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SC_MANAGER_ENUMERATE_SERVICE<\/td>\n<td>Enables calling of the<strong>EnumServicesStatus<\/strong>\u00a0function to list the services that are in the database.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SC_MANAGER_LOCK<\/td>\n<td>Enables calling of the<strong>LockServiceDatabase<\/strong>\u00a0function to acquire a lock on the database.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td>SC_MANAGER_QUERY_LOCK_STATUS<\/td>\n<td>Enables calling of the<strong>QueryServiceLockStatus<\/strong>\u00a0function to retrieve the lock status information for the database.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u60f3\u8981\u83b7\u5f97\u8bbf\u95ee\u6743\u9650\u7684\u8bdd\uff0c\u4f3c\u4e4e\u6ca1\u90a3\u4e48\u590d\u6742\u3002MSDN\u91cc\u9762\u8bf4\u6240\u6709\u8fdb\u7a0b\u90fd\u88ab\u5141\u8bb8\u83b7\u5f97\u5bf9\u6240\u6709SCM\u6570\u636e\u5e93\u7684SC_MANAGER_CONNECT, SC_MANAGER_ENUMERATE_SERVICE, and SC_MANAGER_QUERY_LOCK_STATUS\u6743\u9650\uff0c\u8fd9\u4e9b\u6743\u9650\u4f7f\u5f97\u4f60\u53ef\u4ee5\u8fde\u63a5SCM\u6570\u636e\u5e93\uff0c\u679a\u4e3e\u76ee\u6807\u673a\u5668\u4e0a\u5b89\u88c5\u7684\u670d\u52a1\u548c\u67e5\u8be2\u76ee\u6807\u6570\u636e\u5e93\u662f\u5426\u5df2\u88ab\u9501\u4f4f\u3002\u4f46\u5982\u679c\u8981\u521b\u5efa\u670d\u52a1\uff0c\u9996\u5148\u4f60\u9700\u8981\u62e5\u6709\u76ee\u6807\u673a\u5668\u7684\u7ba1\u7406\u5458\u6743\u9650\uff0c\u4e00\u822c\u7684\u4f20\u9012SC_MANAGER_ALL_ACCESS\u5c31\u53ef\u4ee5\u4e86\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u7684\u53e5\u67c4\u53ef\u4ee5\u88abCloseServiceHandle\u51fd\u6570\u5173\u95ed\u3002<\/p>\n<p>lpServiceName\u662f\u670d\u52a1\u7684\u540d\u5b57\uff0clpDisplayName\u662f\u670d\u52a1\u5728\u201c\u670d\u52a1\u201d\u7ba1\u7406\u5de5\u5177\u91cc\u663e\u793a\u7684\u540d\u5b57\u3002<\/p>\n<p>dwDesiredAccess\u4e5f\u662f\u8bbf\u95ee\u7684\u6743\u9650\uff0c\u6709\u4e00\u4e2a\u6bd4\u4e0a\u9762\u7684\u8fd8\u957f\u7684\u591a\u7684\u4e00\u4e2a\u8868\uff0c\u5404\u4f4d\u81ea\u5df1\u67e5MSDN\u5427\u3002\u6211\u4eec\u8981\u5b89\u88c5\u670d\u52a1\uff0c\u4ecd\u7136\u7b80\u5355\u7684\u4f20\u9012SC_MANAGER_ALL_ACCESS\u3002<\/p>\n<p>dwServiceType\u662f\u6307\u4f60\u7684\u670d\u52a1\u662f\u5426\u548c\u5176\u5b83\u7684\u8fdb\u7a0b\u76f8\u5173\u8054\uff0c\u4e00\u822c\u662fSERVICE_WIN32_OWN_PROCESS\uff0c\u8868\u793a\u4e0d\u548c\u4efb\u4f55\u8fdb\u7a0b\u76f8\u5173\u8054\u3002\u5982\u679c\u4f60\u786e\u8ba4\u4f60\u7684\u670d\u52a1\u9700\u8981\u548c\u67d0\u4e9b\u8fdb\u7a0b\u76f8\u5173\u8054\uff0c\u5c31\u8bbe\u7f6e\u6210SERVICE_WIN32_SHARE_PROCESS\u3002\u5f53\u4f60\u7684\u670d\u52a1\u8981\u548c\u684c\u9762\u76f8\u5173\u8054\u7684\u65f6\u5019\uff0c\u9700\u8981\u8bbe\u7f6e\u6210SERVICE_INTERACTIVE_PROCESS\u3002<\/p>\n<p>dwStartType\u662f\u670d\u52a1\u7684\u542f\u52a8\u65b9\u5f0f\u3002\u670d\u52a1\u6709\u4e09\u79cd\u542f\u52a8\u65b9\u5f0f\uff0c\u5206\u522b\u662f\u201c\u81ea\u52a8(SERVICE_AUTO_START)\u201d\u201c\u624b\u52a8(SERVICE_DEMAND_START)\u201d\u548c\u201c\u7981\u7528(SERVICE_DISABLED)\u201d\u3002\u5728MSDN\u91cc\u8fd8\u6709\u53e6\u5916\u7684\u4e24\u79cd\u65b9\u5f0f\uff0c\u4e0d\u8fc7\u662f\u4e13\u4e3a\u9a71\u52a8\u7a0b\u5e8f\u8bbe\u7f6e\u7684\u3002<\/p>\n<p>dwErrorControl\u51b3\u5b9a\u670d\u52a1\u5982\u679c\u5728\u7cfb\u7edf\u542f\u52a8\u7684\u65f6\u5019\u542f\u52a8\u5931\u8d25\u7684\u8bdd\u8981\u600e\u4e48\u529e\u3002<\/p>\n<table border=\"2\" cellspacing=\"0\" cellpadding=\"1\" align=\"center\">\n<tbody>\n<tr bgcolor=\"#cccccc\">\n<th align=\"left\" valign=\"top\">\n<div align=\"center\">\u503c<\/div>\n<\/th>\n<th align=\"left\" valign=\"top\">\n<div align=\"center\">\u610f\u4e49<\/div>\n<\/th>\n<\/tr>\n<tr>\n<td valign=\"top\">SERVICE_ERROR_IGNORE<\/td>\n<td>\u542f\u52a8\u7a0b\u5e8f\u8bb0\u5f55\u9519\u8bef\u53d1\u751f\uff0c\u4f46\u7ee7\u7eed\u542f\u52a8\u3002<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">SERVICE_ERROR_NORMAL<\/td>\n<td>\u542f\u52a8\u7a0b\u5e8f\u8bb0\u5f55\u9519\u8bef\u53d1\u751f\uff0c\u5e76\u5f39\u51fa\u4e00\u4e2a\u6d88\u606f\u6846\uff0c\u4f46\u4ecd\u7ee7\u7eed\u542f\u52a8<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">SERVICE_ERROR_SEVERE<\/td>\n<td>\u542f\u52a8\u7a0b\u5e8f\u8bb0\u5f55\u9519\u8bef\u53d1\u751f\uff0c\u5982\u679c\u662f\u4ee5last-known-good configuration\u542f\u52a8\u7684\u8bdd\uff0c\u542f\u52a8\u4f1a\u7ee7\u7eed\u3002\u5426\u5219\u4f1a\u4ee5last-known-good configuration\u91cd\u65b0\u542f\u52a8\u8ba1\u7b97\u673a\u3002<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">SERVICE_ERROR_CRITICAL<\/td>\n<td>\u542f\u52a8\u7a0b\u5e8f\u8bb0\u5f55\u9519\u8bef\u53d1\u751f\uff0c\u5982\u679c\u53ef\u80fd\u7684\u8bdd\u3002\u5982\u679c\u662f\u4ee5last-known-good configuration\u542f\u52a8\u7684\u8bdd\uff0c\u542f\u52a8\u4f1a\u5931\u8d25\u3002\u5426\u5219\u4f1a\u4ee5last-known-good configuration\u91cd\u65b0\u542f\u52a8\u8ba1\u7b97\u673a\u3002\u597d\u4e25\u91cd\u7684\u9519\u8bef\u554a\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>lpBinaryPathName\u662f\u670d\u52a1\u7a0b\u5e8f\u7684\u8def\u5f84\u3002MSDN\u91cc\u9762\u7279\u522b\u63d0\u5230\u5982\u679c\u670d\u52a1\u8def\u5f84\u91cc\u9762\u6709\u7a7a\u683c\u7684\u8bdd\u4e00\u5b9a\u8981\u5c06\u8def\u5f84\u7528\u5f15\u53f7\u5f15\u8d77\u6765\u3002\u4f8b\u5982&#8221;d:\\my share\\myservice.exe&#8221;\u5c31\u4e00\u5b9a\u8981\u6307\u5b9a\u4e3a&#8221;&#8221;d:\\my share\\myservice.exe&#8221;&#8221;\u3002<\/p>\n<p>lpLoadOrderGroup\u7684\u610f\u4e49\u5728\u4e8e\uff0c\u5982\u679c\u6709\u4e00\u7ec4\u670d\u52a1\u8981\u6309\u7167\u4e00\u5b9a\u7684\u987a\u5e8f\u542f\u52a8\u7684\u8bdd\uff0c\u8fd9\u4e2a\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u4e00\u4e2a\u7ec4\u540d\u7528\u4e8e\u6807\u5fd7\u8fd9\u4e2a\u542f\u52a8\u987a\u5e8f\u7ec4\uff0c\u4e0d\u8fc7\u6211\u8fd8\u6ca1\u6709\u7528\u8fc7\u8fd9\u4e2a\u53c2\u6570\u3002\u4f60\u7684\u670d\u52a1\u5982\u679c\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u542f\u52a8\u987a\u5e8f\u7ec4\uff0c\u53ea\u8981\u4f20\u9012NULL\u6216\u8005\u4e00\u4e2a\u7a7a\u7684\u5b57\u7b26\u4e32\u5c31\u884c\u4e86\u3002<\/p>\n<p>lpdwTagId\u662f\u5e94\u7528\u4e86\u4e0a\u9762\u7684\u53c2\u6570\u4e4b\u540e\u8981\u6307\u5b9a\u7684\u503c\uff0c\u4e13\u7528\u4e8e\u9a71\u52a8\u7a0b\u5e8f\uff0c\u4e0e\u672c\u6587\u5185\u5bb9\u65e0\u5173\u3002\u4f20\u9012NULL\u3002<\/p>\n<p>lpDependencies\u6807\u793a\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u7528\u4e8e\u6307\u660e\u4e00\u4e32\u670d\u52a1\u7684\u540d\u5b57\u6216\u8005\u4e00\u4e2a\u542f\u52a8\u987a\u5e8f\u7ec4\u3002\u5f53\u4e0e\u4e00\u4e2a\u542f\u52a8\u987a\u5e8f\u7ec4\u5efa\u7acb\u5173\u8054\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u53c2\u6570\u7684\u542b\u4e49\u5c31\u662f\u53ea\u6709\u4f60\u6307\u5b9a\u7684\u542f\u52a8\u987a\u5e8f\u7ec4\u91cc\u6709\u81f3\u5c11\u4e00\u4e2a\u7ecf\u8fc7\u5bf9\u6574\u4e2a\u7ec4\u91cc\u6240\u6709\u7684\u6210\u5458\u5df2\u7ecf\u5168\u90e8\u5c1d\u8bd5\u8fc7\u542f\u52a8\u540e\uff0c\u6709\u81f3\u5c11\u4e00\u4e2a\u6210\u5458\u6210\u529f\u542f\u52a8\uff0c\u4f60\u7684\u670d\u52a1\u624d\u80fd\u542f\u52a8\u3002\u4e0d\u9700\u8981\u5efa\u7acb\u4f9d\u5b58\u5173\u7cfb\u7684\u8bdd\uff0c\u4ecd\u662f\u4f20\u9012NULL\u6216\u8005\u4e00\u4e2a\u7a7a\u7684\u5b57\u7b26\u4e32\u3002\u4f46\u5982\u679c\u4f60\u8981\u6307\u5b9a\u542f\u52a8\u987a\u5e8f\u7ec4\u7684\u8bdd\uff0c\u5fc5\u987b\u4e3a\u7ec4\u540d\u52a0\u4e0aSC_GROUP_IDENTIFIER\u524d\u7f00\uff0c\u56e0\u4e3a\u7ec4\u540d\u548c\u670d\u52a1\u540d\u662f\u5171\u4eab\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u7684\u3002<\/p>\n<p>lpServiceStartName\u662f\u670d\u52a1\u7684\u542f\u52a8\u8d26\u53f7\uff0c\u5982\u679c\u4f60\u8bbe\u7f6e\u4f60\u7684\u670d\u52a1\u7684\u5173\u8054\u7c7b\u578b\u662fSERVICE_WIN32_OWN_PROCESS\u7684\u8bdd\uff0c\u4f60\u9700\u8981\u4ee5DomainNameUserName\u7684\u683c\u5f0f\u6307\u5b9a\u7528\u6237\u540d\uff0c\u5982\u679c\u8fd9\u4e2a\u8d26\u6237\u5728\u4f60\u672c\u673a\u7684\u8bdd\uff0c\u7528.UserName\u5c31\u53ef\u4ee5\u6307\u5b9a\u3002\u5982\u679c\u4f20\u9012NULL\u7684\u8bdd\uff0c\u4f1a\u4ee5\u672c\u5730\u7684\u7cfb\u7edf\u8d26\u6237\u767b\u9646\u3002\u5982\u679c\u662fWin NT 4.0\u6216\u66f4\u65e9\u7684\u7248\u672c\u7684\u8bdd\uff0c\u5982\u679c\u4f60\u6307\u5b9a\u4e86SERVICE_WIN32_SHARE_PROCESS\uff0c\u5c31\u5fc5\u987b\u4f20\u9012.System\u6307\u5b9a\u670d\u52a1\u4f7f\u7528\u672c\u5730\u7684\u7cfb\u7edf\u8d26\u6237\u3002\u6700\u540e\uff0c\u5982\u679c\u4f60\u6307\u5b9a\u4e86SERVICE_INTERACTIVE_PROCESS\uff0c\u4f60\u5fc5\u987b\u4f7f\u670d\u52a1\u8fd0\u884c\u5728\u672c\u673a\u7cfb\u7edf\u8d26\u6237\u3002<\/p>\n<p>\u770b\u540d\u5b57\u5c31\u77e5\u9053\u4e86\uff0clpPassword\u662f\u8d26\u6237\u7684\u5bc6\u7801\u3002\u5982\u679c\u6307\u5b9a\u7cfb\u7edf\u8d26\u6237\u7684\u8bdd\uff0c\u4f20\u9012NULL\u3002\u5982\u679c\u8d26\u6237\u6ca1\u6709\u5bc6\u7801\u7684\u8bdd\uff0c\u4f20\u9012\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<p><span class=\"htd\">\u3000\u3000\u603b\u4e4b\u670d\u52a1\u7684\u57fa\u672c\u539f\u7406\u5c31\u662f\u8fd9\u6837\u5b50\u4e86\uff0c\u5230\u4e86\u8fd9\u91cc\u8fd9\u7bc7\u6587\u7ae0\u4f3c\u4e4e\u53ef\u4ee5\u544a\u4e00\u6bb5\u843d\u4e86\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd8\u6709\u5f88\u591a\u5185\u5bb9\u5fc5\u987b\u8981\u8ba8\u8bba\uff0c\u6240\u4ee5\u6211\u8fd8\u4e0d\u80fd\u8349\u8349\u6536\u7b14\uff0c\u656c\u8bf7\u5173\u6ce8\u4e0b\u4e00\u7ae0\u3002<\/span><\/p>\n<p><span class=\"htd\">\uff08\u56db\uff09\u4e00\u4e9b\u95ee\u9898\u7684\u8ba8\u8bba<\/span>\u3000\u3000\u524d\u9762\u51e0\u7ae0\u7684\u5185\u5bb9\u90fd\u662f\u670d\u52a1\u7684\u4e00\u4e9b\u901a\u7528\u7684\u7f16\u5199\u539f\u7406\uff0c\u4f46\u91cc\u9762\u9690\u542b\u7740\u4e00\u4e9b\u95ee\u9898\uff0c\u7f16\u5199\u7b80\u5355\u7684\u670d\u52a1\u65f6\u770b\u4e0d\u51fa\u6765\uff0c\u4f46\u9047\u5230\u590d\u6742\u7684\u5e94\u7528\u5c31\u4f1a\u51fa\u73b0\u4e00\u4e9b\u95ee\u9898\uff0c\u6240\u4ee5\u672c\u7ae0\u5c31\u662f\u7528\u6765\u5206\u6790\u3001\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\uff0c\u9002\u7528\u4e8e\u9ad8\u7ea7\u5e94\u7528\u7684\u5f00\u53d1\u4eba\u5458\u3002\u6211\u8fd9\u4e00\u7ae0\u7684\u5185\u5bb9\u90fd\u662f\u7ecf\u8fc7\u5b9e\u9a8c\u5f97\u5230\u7684\uff0c\u5f88\u6709\u5b9e\u9645\u610f\u4e49\u3002<\/p>\n<p>\u6211\u5728\u7b2c\u4e00\u7ae0\u91cc\u9762\u5c31\u8bf4\u8fc7\uff0c\u662f\u7531\u4e00\u4e2a\u670d\u52a1\u7684\u4e3b\u7ebf\u7a0b\u6267\u884cCtrlHandler\u51fd\u6570\uff0c\u5b83\u5c06\u6536\u5230\u5404\u79cd\u63a7\u5236\u547d\u4ee4\uff0c\u4f46\u662f\u771f\u6b63\u5904\u7406\u547d\u4ee4\uff0c\u6267\u884c\u64cd\u4f5c\u7684\u662fServiceMain\u7684\u7ebf\u7a0b\u3002\u73b0\u5728\uff0c\u5f53\u4e00\u4e2aSERVICE_CONTROL_STOP\u5230\u8fbe\u4e4b\u540e\uff0c\u4f60\u4f5c\u4e3a\u4e00\u4e2a\u5f00\u53d1\u8005\uff0c\u8981\u600e\u6837\u505c\u6b62\u8fd9\u4e2a\u670d\u52a1\uff1f\u5728\u6211\u770b\u8fc7\u7684\u4e00\u4e9b\u6e90\u4ee3\u7801\u91cc\uff0c\u5927\u90e8\u5206\u53ea\u662f\u7b80\u5355\u7684\u8c03\u7528TerminateThread\u51fd\u6570\u53bb\u5f3a\u884c\u6740\u6389\u670d\u52a1\u8fdb\u7a0b\u3002\u4f46\u5e94\u8be5\u7a0d\u7a0d\u6709\u70b9\u7ebf\u7a0b\u7f16\u7a0b\u7684\u5e38\u8bc6\u5c31\u5e94\u8be5\u77e5\u9053TerminateThread\u51fd\u6570\u662f\u53ef\u7528\u7684\u8c03\u7528\u4e2d\u6700\u4e3a\u7cdf\u7cd5\u7684\u4e00\u4e2a\uff0c\u670d\u52a1\u7ebf\u7a0b\u5c06\u5f97\u4e0d\u5230\u4efb\u4f55\u673a\u4f1a\u53bb\u505a\u5e94\u8be5\u7684\u6e05\u7406\u5de5\u4f5c\uff0c\u8bf8\u5982\u6e05\u9664\u5185\u5b58\u3001\u91ca\u653e\u6838\u5fc3\u5bf9\u8c61\uff0cDlls\u4e5f\u5f97\u4e0d\u5230\u4efb\u4f55\u7ebf\u7a0b\u5df2\u7ecf\u88ab\u6bc1\u7684\u901a\u77e5\u3002<\/p>\n<p>\u6240\u4ee5\u505c\u6b62\u670d\u52a1\u7684\u9002\u5f53\u65b9\u6cd5\u662f\u4ee5\u67d0\u79cd\u65b9\u5f0f\u6fc0\u6d3b\u670d\u52a1\u7ebf\u7a0b\uff0c\u8ba9\u5b83\u505c\u6b62\u7ee7\u7eed\u63d0\u4f9b\u670d\u52a1\u529f\u80fd\uff0c\u7136\u540e\u6267\u884c\u5b8c\u5f53\u524d\u64cd\u4f5c\u548c\u6e05\u9664\u5de5\u4f5c\u540e\u8fd4\u56de\u3002\u8fd9\u5c31\u8868\u793a\u4f60\u5fc5\u987b\u5728CtrlHandler\u7ebf\u7a0b\u548cServiceMain\u7ebf\u7a0b\u4e4b\u95f4\u6267\u884c\u9002\u5f53\u7684\u7ebf\u7a0b\u901a\u4fe1\u3002\u73b0\u5728\u5df2\u77e5\u7684\u6700\u597d\u7684\u5185\u90e8\u7ebf\u7a0b\u901a\u4fe1\u673a\u5236\u662fI\/O Completion Port\uff08I\/O \u5b8c\u6210\u7aef\u53e3)\uff0c\u5047\u5982\u4f60\u7f16\u5199\u7684\u662f\u4e00\u4e2a\u5927\u578b\u7684\u670d\u52a1\uff0c\u9700\u8981\u540c\u65f6\u5904\u7406\u4e3a\u6570\u4f17\u591a\u7684\u8bf7\u6c42\uff0c\u5e76\u4e14\u8fd0\u884c\u5728\u591a\u5904\u7406\u5668\u7cfb\u7edf\u4e0a\u9762\uff0c\u8fd9\u4e2a\u6a21\u578b\u5c31\u53ef\u4ee5\u63d0\u4f9b\u6700\u4f73\u7684\u7cfb\u7edf\u6027\u80fd\u3002\u4f46\u4e5f\u6b63\u56e0\u4e3a\u5b83\u7684\u590d\u6742\u6027\u8f83\u9ad8\uff0c\u5728\u5c0f\u89c4\u6a21\u7684\u5e94\u7528\u4e0a\u9762\u4e0d\u503c\u5f97\u82b1\u8d39\u5f88\u591a\u7684\u65f6\u95f4\u548c\u7cbe\u529b\uff0c\u8fd9\u65f6\u4f5c\u4e3a\u5f00\u53d1\u8005\u53ef\u4ee5\u9002\u5f53\u7684\u9009\u53d6\u5176\u5b83\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u8bf8\u5982\u5f02\u6b65\u8fc7\u7a0b\u8c03\u7528\u961f\u5217\u3001\u5957\u63a5\u5b57\u548c\u7a97\u53e3\u6d88\u606f\uff0c\u4ee5\u9002\u5e94\u5b9e\u9645\u60c5\u51b5\u3002<\/p>\n<p>\u5f00\u53d1\u670d\u52a1\u65f6\u7684\u53e6\u5916\u4e00\u4e2a\u91cd\u8981\u95ee\u9898\u5c31\u662f\u8c03\u7528SetServiceStatus\u51fd\u6570\u65f6\u7684\u6240\u6709\u72b6\u6001\u62a5\u544a\u95ee\u9898\u3002\u5f88\u591a\u7684\u670d\u52a1\u5f00\u53d1\u8005\u4e3a\u4e86\u5728\u4ec0\u4e48\u65f6\u5019\u8c03\u7528SetServiceStatus\u7684\u95ee\u9898\u800c\u5e38\u5e38\u4ea7\u751f\u4e89\u8bba\uff0c\u4e00\u822c\u63a8\u8350\u7684\u65b9\u6cd5\u5c31\u662f\uff1a\u5148\u8c03\u7528SetServiceStatus\u51fd\u6570\uff0c\u62a5\u544aSERVICE_STOP_PENDING\u72b6\u6001\uff0c\u7136\u540e\u5c06\u63a7\u5236\u4ee3\u7801\u4f20\u7ed9\u670d\u52a1\u7ebf\u7a0b\u6216\u8005\u518d\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\uff0c\u8ba9\u5b83\u53bb\u7ee7\u7eed\u6267\u884c\u64cd\u4f5c\uff0c\u5f53\u8be5\u7ebf\u7a0b\u5373\u5c06\u6267\u884c\u5b8c\u64cd\u4f5c\u4e4b\u524d\uff0c\u518d\u7531\u5b83\u5c06\u670d\u52a1\u7684\u72b6\u6001\u8bbe\u7f6e\u6210SERVICE_STOPPED\uff0c\u7136\u540e\u670d\u52a1\u6b63\u597d\u505c\u6b62\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u4e3b\u610f\u4ece\u4e24\u4e2a\u65b9\u9762\u6765\u8bb2\u8fd8\u662f\u5f88\u4e0d\u9519\u7684\u3002\u9996\u5148\u670d\u52a1\u53ef\u4ee5\u7acb\u5373\u786e\u8ba4\u6536\u5230\u4e86\u63a7\u5236\u4ee3\u7801\uff0c\u5e76\u5c06\u5728\u5b83\u8ba4\u4e3a\u9002\u5f53\u7684\u65f6\u5019\u8fdb\u884c\u5904\u7406\uff1b\u7136\u540e\u5c31\u662f\u56e0\u4e3a\u524d\u9762\u8bf4\u8fc7\u7684\uff0c\u6267\u884cCtrlHandler\u51fd\u6570\u7684\u662f\u4e3b\u7ebf\u7a0b\uff0c\u5982\u679c\u6309\u7167\u8fd9\u79cd\u5de5\u4f5c\u65b9\u6cd5\uff0cCtrlHandler\u51fd\u6570\u53ef\u4ee5\u8fc5\u901f\u7684\u8fd4\u56de\uff0c\u4e0d\u4f1a\u5f71\u54cd\u5230\u5176\u5b83\u670d\u52a1\u53ef\u80fd\u6536\u5230\u7684\u63a7\u5236\u8bf7\u6c42\uff0c\u5bf9\u542b\u6709\u591a\u4e2a\u670d\u52a1\u7684\u7a0b\u5e8f\u6765\u8bf4\uff0c\u54cd\u5e94\u5404\u4e2a\u670d\u52a1\u7684\u63a7\u5236\u4ee3\u7801\u7684\u901f\u5ea6\u4f1a\u5927\u5927\u7684\u63d0\u9ad8\u3002\u53ef\u662f\uff0c\u968f\u4e4b\u800c\u6765\u7684\u662f\u95ee\u9898\u2015\u2015 race condition \u5373\u201c\u7ade\u4e89\u6761\u4ef6\u201d\u7684\u4ea7\u751f\u3002<\/p>\n<p>\u6446\u5728\u4e0b\u9762\u7684\u5c31\u662f\u4e00\u4e2a\u7ade\u4e89\u6761\u4ef6\u7684\u4f8b\u5b50\uff0c\u6211\u82b1\u4e86\u4e00\u70b9\u65f6\u95f4\u6765\u4fee\u6539\u6211\u7684\u57fa\u672c\u670d\u52a1\u7684\u4ee3\u7801\uff0c\u610f\u56fe\u6545\u610f\u5f15\u53d1\u201c\u7ade\u4e89\u6761\u4ef6\u201d\u7684\u53d1\u751f\u3002\u6211\u6dfb\u52a0\u4e86\u4e00\u4e2a\u7ebf\u7a0b\uff0cCtrlHandler\u51fd\u6570\u7684\u7ebf\u7a0b\u5728\u6536\u5230\u8bf7\u6c42\u540e\u7acb\u523b\u4f5c\u51fa\u53cd\u5e94\uff0c\u5c06\u5f53\u524d\u7684\u670d\u52a1\u72b6\u6001\u8bbe\u7f6e\u6210\u201c\u8bf7\u6c42\u6b63\u5728\u88ab\u5904\u7406\u201d\u5373&#8230;_PENDING\uff0c\u7136\u540e\u7531\u6211\u6dfb\u52a0\u7684\u7ebf\u7a0b\u5728\u7761\u7720\u4e865\u79d2\u4e4b\u540e\u518d\u5c06\u670d\u52a1\u72b6\u6001\u8bbe\u7f6e\u6210\u201c\u8bf7\u6c42\u5df2\u5b8c\u6210\u201d\u72b6\u6001\u2015\u2015\u4ee5\u6a21\u62df\u670d\u52a1\u6b63\u5728\u5904\u7406\u4e00\u4e9b\u4e0d\u53ef\u4e2d\u6b62\u7684\u4e8b\u4ef6\uff0c\u53ea\u6709\u5904\u7406\u5b8c\u6210\u540e\u624d\u4f1a\u66f4\u6539\u670d\u52a1\u7684\u72b6\u6001\u3002\u4e00\u5207\u5c31\u7eea\u4e4b\u540e\uff0c\u6211\u5c1d\u8bd5\u5728\u77ed\u65f6\u95f4\u5185\u8fde\u7eed\u53d1\u9001\u4e24\u4e2a\u201c\u6682\u505c\u201d\u8bf7\u6c42\uff0c\u5982\u679c\u201c\u7ade\u4e89\u6761\u4ef6\u201d\u4e0d\u5b58\u5728\u7684\u8bdd\u5e94\u8be5\u53ea\u6709\u5148\u53d1\u9001\u7684\u90a3\u4e2a\u8bf7\u6c42\u80fd\u591f\u5230\u8fbeSCM\uff0c\u800c\u53e6\u4e00\u4e2a\u5219\u5e94\u8be5\u8fd4\u56de\u8bf7\u6c42\u53d1\u9001\u5931\u8d25\u7684\u4fe1\u606f\uff0c\u5929\u4e0b\u592a\u5e73\u3002<\/p>\n<p>\u4e8b\u5b9e\u4e0a\u5f88\u4e0d\u5e78\u7684\uff0c\u6211\u6210\u529f\u4e86\u3002\u5f53\u6211\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u201c\u547d\u4ee4\u63d0\u793a\u7b26\u201d\u7a97\u53e3\u5206\u522b\u540c\u6837\u7684\u8f93\u5165\u4e0b\u9762\u7684\u547d\u4ee4\uff1a<\/p>\n<p>net pause kservice<\/p>\n<p>\u4e4b\u540e\u5728\u201c\u4e8b\u4ef6\u67e5\u770b\u5668\u201d\u91cc\u9762\uff0c\u6211\u627e\u5230\u4e86\u6211\u7684\u670d\u52a1\u5728\u201c\u5e94\u7528\u7a0b\u5e8f\u65e5\u5fd7\u201d\u91cc\u6dfb\u52a0\u7684\u4e8b\u4ef6\u8bb0\u5f55\uff0c\u7ed3\u679c\u662f\u6211\u5f97\u5230\u4e86\u8fd9\u6837\u7684\u4e8b\u4ef6\u5217\u8868\uff1a<\/p>\n<p>SERVICE_PAUSE_PENDING<br \/>\nSERVICE_PAUSE_PENDING<br \/>\nSERVICE_PAUSED<br \/>\nSERVICE_PAUSED<\/p>\n<p>\u770b\u4e0a\u53bb\u5f88\u5947\u602a\u662f\u4e0d\u662f\uff1f\u56e0\u4e3a\u670d\u52a1\u5904\u4e8e\u6b63\u5728\u6682\u505c\u72b6\u6001\u7684\u65f6\u5019\uff0c\u5b83\u4e0d\u5e94\u8be5\u88ab\u518d\u6b21\u6682\u505c\u7684\u3002\u4f46\u4e8b\u5b9e\u6446\u5728\u773c\u524d\uff0c\u5f88\u591a\u670d\u52a1\u90fd\u66fe\u660e\u786e\u7684\u62a5\u544a\u8fc7\u4e0a\u9762\u7684\u987a\u5e8f\u72b6\u6001\u3002\u6211\u66fe\u7ecf\u8ba4\u4e3a\u8fd9\u65f6SCM\u5e94\u8be5\u8bf4\u4e9b\u4ec0\u4e48\u6216\u505a\u4e9b\u4ec0\u4e48\uff0c\u4ee5\u963b\u6b62\u201c\u7ade\u4e89\u72b6\u6001\u201d\u7684\u51fa\u73b0\uff0c\u4f46\u5b9e\u9a8c\u7ed3\u679c\u544a\u8bc9\u6211SCM\u4f3c\u4e4e\u5bf9\u6b64\u65e0\u80fd\u4e3a\u529b\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u63a7\u5236\u72b6\u6001\u4ee3\u7801\u5728\u4ec0\u4e48\u65f6\u5019\u88ab\u53d1\u9001\u3002\u5f53\u7528\u6237\u4f7f\u7528\u201c\u7ba1\u7406\u5de5\u5177\u201d\u91cc\u9762\u7684\u201c\u670d\u52a1\u201d\u5de5\u5177\u6765\u7ba1\u7406\u670d\u52a1\u7684\u72b6\u6001\u7684\u65f6\u5019\uff0c\u5728\u4e00\u4e2a\u201c\u6682\u505c\u201d\u8bf7\u6c42\u5df2\u7ecf\u53d1\u51fa\u4e4b\u540e\u4e0d\u80fd\u518d\u6b21\u7528\u8fd9\u4e2a\u5de5\u5177\u5411\u5b83\u53d1\u51fa\u201c\u6682\u505c\u201d\u8bf7\u6c42\uff0c\u5982\u679c\u6b63\u5728\u6682\u505c\u670d\u52a1\uff0c\u4f1a\u6709\u4e00\u4e2a\u5bf9\u8bdd\u6846\u51fa\u73b0\uff0c\u963b\u6b62\u4f60\u6309\u4e0b\u5b83\u540e\u9762\u7684\u201c\u670d\u52a1\u201d\u5de5\u5177\u7684\u5de5\u5177\u680f\u4e0a\u7684\u4efb\u4f55\u6309\u94ae\uff0c\u5982\u679c\u5df2\u7ecf\u6682\u505c\uff0c\u201c\u6682\u505c\u201c\u6309\u94ae\u5c06\u53d8\u6210\u7070\u8272\u3002\u4f46\u662f\u8fd9\u65f6\u7528\u547d\u4ee4\u884c\u5de5\u5177 net.exe \u5c31\u53ef\u4ee5\u5f88\u987a\u5229\u5730\u5c06\u6682\u505c\u8bf7\u6c42\u518d\u6b21\u9001\u5230\u670d\u52a1\u3002\u8bc1\u636e\u5c31\u662f\u6211\u6dfb\u52a0\u7684\u5176\u4ed6\u4e8b\u4ef6\u8bb0\u5f55\u91cc\u9762\u8bb0\u4e0b\u4e86SetServiceStatus\u7684\u8c03\u7528\u5168\u90fd\u6210\u529f\u4e86\uff0c\u8fd9\u66f4\u8fdb\u4e00\u6b65\u7684\u8bf4\u660e\u4e86\u6211\u63d0\u4ea4\u7684\u4e24\u4e2a\u6682\u505c\u8bf7\u6c42\u90fd\u7ecf\u8fc7SCM\uff0c\u7136\u540e\u5230\u8fbe\u4e86\u6211\u7684\u670d\u52a1\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u53c8\u8fdb\u884c\u4e86\u5176\u5b83\u7684\u6d4b\u8bd5\uff0c\u4f8b\u5982\u5148\u53d1\u9001\u201c\u6682\u505c\u201d\u8bf7\u6c42\uff0c\u540e\u53d1\u9001\u201c\u505c\u6b62\u201d\u8bf7\u6c42\uff0c\u548c\u5148\u53d1\u9001\u201c\u505c\u6b62\u201d\u8bf7\u6c42\uff0c\u518d\u53d1\u9001\u201c\u6682\u505c\u201d\u6216\u201c\u505c\u6b62\u201d\u8bf7\u6c42\u3002\u524d\u4e00\u79cd\u60c5\u51b5\u66f4\u52a0\u7cdf\u7cd5\uff0c\u5148\u53d1\u9001\u7684\u201c\u6682\u505c\u201d\u8bf7\u6c42\u548c\u540e\u53d1\u9001\u7684\u201c\u505c\u6b62\u201d\u8bf7\u6c42\u90fd\u6ca1\u6709\u5f97\u5230\u4ec0\u4e48\u597d\u4e0b\u573a\uff0c\u867d\u7136SCM\u8001\u8001\u5b9e\u5b9e\u7684\u5148\u6682\u505c\u4e86\u670d\u52a1\uff0c\u540e\u505c\u6b62\u4e86\u670d\u52a1\uff0c\u4f46 net.exe \u7684\u4e24\u4e2a\u5b9e\u4f8b\u7684\u8c03\u7528\u5747\u544a\u5931\u8d25\u3002\u4e0d\u8fc7\u5728\u6d4b\u8bd5\u5148\u53d1\u9001\u505c\u6b62\u201c\u8bf7\u6c42\u201d\u7684\u65f6\u5019\uff0c\u6240\u6709\u7684\u73b0\u8c61\u90fd\u8868\u793a\u8fd9\u4e24\u4e2a\u8bf7\u6c42\u53ea\u6709\u5148\u53d1\u9001\u7684\u201c\u505c\u6b62\u201d\u5230\u8fbe\u4e86SCM\uff0c\u8fd9\u8fd8\u7b97\u662f\u4e2a\u597d\u6d88\u606f&#8230;<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5f53\u670d\u52a1\u5f97\u5230\u4e00\u4e2a\u201c\u505c\u6b62\u201d\u201c\u6682\u505c\u201d\u6216\u201c\u7ee7\u7eed\u201d\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u9996\u5148\u68c0\u67e5\u670d\u52a1\u662f\u5426\u5df2\u7ecf\u5728\u5904\u7406\u53e6\u5916\u7684\u4e00\u4e2a\u8bf7\u6c42\uff0c\u5982\u679c\u662f\uff0c\u5c31\u4f9d\u60c5\u51b5\u800c\u5b9a\uff1a\u662f\u4e0d\u8c03\u7528SetServiceStatus\u76f4\u63a5\u8fd4\u56de\u8fd8\u662f\u6682\u65f6\u5fcd\u8010\u76f4\u5230\u524d\u4e00\u4e2a\u8bf7\u6c42\u52a8\u4f5c\u5b8c\u6210\u518d\u8c03\u7528SetServiceStatus\uff0c\u8fd9\u662f\u4f60\u4f5c\u4e3a\u4e00\u4e2a\u5f00\u53d1\u8005\u8981\u81ea\u5df1\u51b3\u5b9a\u7684\u3002<\/p>\n<p>\u5982\u679c\u8bf4\u524d\u9762\u7684\u95ee\u9898\u5df2\u7ecf\u8db3\u591f\u9ebb\u70e6\u4e86\uff0c\u4e0b\u9762\u7684\u95ee\u9898\u4f1a\u4ee4\u4f60\u89c9\u5f97\u66f4\u52a0\u602a\u5f02\u3002\u5b83\u5176\u5b9e\u662f\u4e00\u79cd\u53ef\u4ee5\u89e3\u51b3\u4e0a\u9762\u7684\u95ee\u9898\u7684\u65b9\u6cd5\uff1a\u5f53CtrlHandler\u51fd\u6570\u7684\u7ebf\u7a0b\u6536\u5230SERVICE_PAUSE_PENDING\u8bf7\u6c42\u4e4b\u540e\uff0c\u5b83\u8c03\u7528SetServiceStatus\u62a5\u544a\u670d\u52a1\u6b63\u5728\u6682\u505c\uff0c\u7136\u540e\u7531\u5b83\u81ea\u5df1\u8c03\u7528SuspendThread\u6765\u6682\u505c\u670d\u52a1\u7684\u7ebf\u7a0b\uff0c\u7136\u540e\u518d\u7531\u5b83\u81ea\u5df1\u8c03\u7528SetServiceStatus\u62a5\u544a\u670d\u52a1\u5df2\u7ecf\u88ab\u6682\u505c\u3002\u8fd9\u6837\u505a\u7684\u786e\u907f\u514d\u4e86\u201c\u7ade\u4e89\u6761\u4ef6\u201d\u7684\u51fa\u73b0\uff0c\u56e0\u4e3a\u6240\u6709\u7684\u5de5\u4f5c\u90fd\u662f\u7531\u4e00\u4e2a\u51fd\u6570\u6765\u505a\u7684\u3002\u73b0\u5728\u9700\u8981\u6ce8\u610f\u7684\u4e0d\u662f\u201c\u7ade\u4e89\u6761\u4ef6\u201d\u800c\u662f\u670d\u52a1\u672c\u8eab\uff0c\u6302\u8d77\u670d\u52a1\u7684\u7ebf\u7a0b\u4f1a\u4e0d\u4f1a\u6682\u505c\u670d\u52a1\u5462\uff1f\u7b54\u6848\u662f\u4f1a\u7684\u3002\u4f46\u662f\u6682\u505c\u670d\u52a1\u610f\u5473\u7740\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u5047\u5982\u6211\u7684\u670d\u52a1\u662f\u7528\u6765\u5904\u7406\u7f51\u7edc\u5ba2\u6237\u7684\u8bf7\u6c42\uff0c\u90a3\u4e48\u6682\u505c\u5bf9\u4e8e\u6211\u7684\u670d\u52a1\u6765\u8bf4\u5e94\u8be5\u662f\u505c\u6b62\u63a5\u53d7\u65b0\u7684\u8bf7\u6c42\u3002\u5982\u679c\u6211\u73b0\u5728\u6b63\u5904\u5728\u5904\u7406\u8bf7\u6c42\u7684\u8fc7\u7a0b\u4e2d\uff0c\u90a3\u4e48\u6211\u5e94\u8be5\u600e\u4e48\u529e\uff1f\u4e5f\u8bb8\u6211\u5e94\u8be5\u7ed3\u675f\u5b83\uff0c\u4f7f\u5ba2\u6237\u4e0d\u81f3\u4e8e\u65e0\u9650\u671f\u60ac\u6302\u3002\u4f46\u5982\u679c\u6211\u53ea\u662f\u7b80\u5355\u7684\u8c03\u7528SuspendThread\uff0c\u90a3\u4e48\u4e0d\u6392\u9664\u670d\u52a1\u7ebf\u7a0b\u6b63\u5904\u4e8e\u5b64\u7acb\u7684\u4e2d\u95f4\u72b6\u6001\u7684\u53ef\u80fd\uff0c\u6216\u8005\u6b63\u5728\u8c03\u7528malloc\u51fd\u6570\u53bb\u5c1d\u8bd5\u5206\u914d\u5185\u5b58\uff0c\u5982\u679c\u8fd0\u884c\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u7684\u53e6\u4e00\u4e2a\u670d\u52a1\u4e5f\u8c03\u5185\u5b58\u5206\u914d\u51fd\u6570\uff0c\u90a3\u4e48\u5b83\u4e5f\u4f1a\u88ab\u6302\u8d77\uff0c\u8fd9\u80af\u5b9a\u4e0d\u662f\u6211\u671f\u671b\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\uff1a\u7528\u6237\u8ba4\u4e3a\u81ea\u5df1\u53ef\u4ee5\u88ab\u5141\u8bb8\u53bb\u505c\u6b62\u4e00\u4e2a\u5df2\u7ecf\u88ab\u6682\u505c\u4e86\u7684\u670d\u52a1\u5417\uff1f\u6211\u8ba4\u4e3a\u662f\u8fd9\u6837\u7684\uff0c\u800c\u4e14\u5f88\u660e\u663e\u7684\uff0c\u5fae\u8f6f\u4e5f\u8fd9\u4e48\u8ba4\u4e3a\u3002\u56e0\u4e3a\u5f53\u6211\u4eec\u5728\u201c\u670d\u52a1\u201d\u7ba1\u7406\u5de5\u5177\u91cc\u9762\u9009\u4e2d\u4e00\u4e2a\u5df2\u6682\u505c\u7684\u670d\u52a1\u4e4b\u540e\uff0c\u201c\u505c\u6b62\u201d\u6309\u94ae\u662f\u53ef\u4ee5\u88ab\u6309\u4e0b\u7684\u3002\u4f46\u6211\u8981\u600e\u6837\u505c\u6b62\u4e00\u4e2a\u7531\u4e8e\u7ebf\u7a0b\u88ab\u6302\u8d77\u624d\u5904\u4e8e\u6682\u505c\u72b6\u6001\u7684\u670d\u52a1\u5462\uff1f\u4e0d\uff0c\u4e0d\u8981TerminateThread\uff0c\u8bf7\u522b\u8ddf\u6211\u63d0\u8d77\u5b83\u3002<\/p>\n<p>\u89e3\u51b3\u8fd9\u6240\u6709\u7684\u6df7\u4e71\u7684\u6700\u597d\u65b9\u6cd5\uff0c\u5c31\u662f\u6709\u4e00\u4e2a\u80fd\u591f\u628a\u6240\u6709\u4e8b\u505a\u597d\u7684\u7ebf\u7a0b\uff0c\u800c\u4e14\u5b83\u5e94\u8be5\u662f\u670d\u52a1\u7ebf\u7a0b\uff0c\u800c\u4e0d\u662fCtrlHandler\u7ebf\u7a0b\u3002\u5f53CtrlHandler\u51fd\u6570\u5f97\u5230\u63a7\u5236\u4ee3\u7801\u4e4b\u540e\uff0c\u5b83\u8981\u8fc5\u901f\u7684\u5c06\u63a7\u5236\u4ee3\u7801\u901a\u8fc7\u7ebf\u7a0b\u5185\u90e8\u901a\u8baf\u624b\u6bb5\u9001\u5230\u670d\u52a1\u7ebf\u7a0b\u4e2d\u6392\u961f\uff0c\u7136\u540eCtrlHandler\u51fd\u6570\u5c31\u5e94\u8be5\u8fd4\u56de\uff0c\u5b83\u51b3\u4e0d\u5e94\u8be5\u8c03SetServiceStatus\u3002\u8fd9\u6837\uff0c\u670d\u52a1\u53ef\u4ee5\u968f\u5fc3\u6240\u6b32\u7684\u63a7\u5236\u6bcf\u4ef6\u4e8b\u60c5\uff0c\u56e0\u4e3a\u6ca1\u6709\u4ec0\u4e48\u6bd4\u5b83\u66f4\u6709\u53d1\u8a00\u6743\u7684\u4e86\uff0c\u6ca1\u6709\u201c\u7ade\u4e89\u6761\u4ef6\u201d\u3002\u670d\u52a1\u51b3\u5b9a\u6682\u505c\u610f\u5473\u7740\u4ec0\u4e48\uff0c\u670d\u52a1\u80fd\u591f\u5141\u8bb8\u81ea\u5df1\u5728\u5df2\u7ecf\u6682\u505c\u7684\u60c5\u51b5\u4e0b\u505c\u6b62\uff0c\u670d\u52a1\u51b3\u5b9a\u4ec0\u4e48\u5185\u90e8\u901a\u8baf\u673a\u5236\u662f\u6700\u597d\u7684\u2015\u2015\u5e76\u4e14CtrlHandler\u51fd\u6570\u5fc5\u987b\u7b80\u5355\u7684\u4e0e\u8fd9\u79cd\u673a\u5236\u76f8\u4e00\u81f4\u3002<\/p>\n<p>\u4e8b\u60c5\u6ca1\u6709\u5b8c\u7f8e\u7684\uff0c\u4e0a\u9762\u7684\u65b9\u6cd5\u4e5f\u4e0d\u4f8b\u5916\uff0c\u5b83\u4ec5\u6709\u4e00\u4e2a\u5c0f\u7f3a\u9677\uff1a\u5c31\u662f\u5047\u5b9a\u5f53\u670d\u52a1\u6536\u5230\u63a7\u5236\u4ee3\u7801\u540e\uff0c\u5728\u8f83\u77ed\u7684\u65f6\u95f4\u5185\u5c31\u80fd\u505a\u51fa\u5e94\u6709\u7684\u54cd\u5e94\u3002\u5982\u679c\u670d\u52a1\u7ebf\u7a0b\u6b63\u5728\u5fd9\u4e8e\u5904\u7406\u4e00\u4e2a\u5ba2\u6237\u7684\u8bf7\u6c42\uff0c\u63a7\u5236\u4ee3\u7801\u53ef\u80fd\u8fdb\u5165\u7b49\u5f85\u961f\u5217\uff0c\u800c\u4e14SetServiceStatus\u53ef\u80fd\u4e5f\u65e0\u6cd5\u8fc5\u901f\u7684\u88ab\u8c03\u7528\u3002\u5982\u679c\u771f\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u8d1f\u8d23\u53d1\u9001\u901a\u77e5\u7684SCP\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4f60\u7684\u670d\u52a1\u5df2\u7ecf\u5931\u8d25\uff0c\u5e76\u5411\u7528\u6237\u62a5\u544a\u4e00\u4e2a\u6d88\u606f\u6846\u3002\u4e8b\u5b9e\u4e0a\u670d\u52a1\u5e76\u6ca1\u6709\u5931\u8d25\uff0c\u800c\u4e14\u4e5f\u4e0d\u4f1a\u88ab\u7ec8\u6b62\u3002<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u591f\u7cdf\u7cd5\u4e86\uff0c\u6ca1\u6709\u7528\u6237\u4f1a\u53bb\u8d23\u602aSCP\u2015\u2015\u867d\u7136SCP\u5c06\u4ed6\u4eec\u5f15\u5bfc\u5230\u4e86\u9519\u8bef\u7684\u72b6\u6001\uff0c\u4ed6\u4eec\u53ea\u4f1a\u8d23\u602a\u670d\u52a1\u7684\u4f5c\u8005\u2015\u2015\u5c31\u662f\u6211\u6216\u4f60&#8230;\u56e0\u6b64\uff0c\u5728\u670d\u52a1\u4e2d\u600e\u4e48\u505a\u624d\u80fd\u9632\u6b62\u8fd9\u79cd\u95ee\u9898\u53d1\u751f\u5462\uff1f\u5f88\u7b80\u5355\uff0c\u4f7f\u670d\u52a1\u5feb\u901f\u6709\u6548\u7684\u8fd0\u884c\uff0c\u5e76\u4e14\u603b\u4fdd\u6301\u4e00\u4e2a\u6d3b\u52a8\u7ebf\u7a0b\u7b49\u5f85\u53bb\u5904\u7406\u63a7\u5236\u4ee3\u7801\u3002<\/p>\n<p>\u8bf4\u8d77\u6765\u597d\u50cf\u5f88\u5bb9\u6613\uff0c\u4f46\u5b9e\u9645\u505a\u8d77\u6765\u5c31\u88ab\u90a3\u4e48\u7b80\u5355\u4e86\uff0c\u8fd9\u4e5f\u4e0d\u662f\u6211\u80fd\u591f\u5411\u5404\u4f4d\u89e3\u91ca\u7684\u4e86\uff0c\u53ea\u6709\u8ba4\u771f\u7684\u8c03\u8bd5\u81ea\u5df1\u7684\u670d\u52a1\uff0c\u624d\u80fd\u627e\u51fa\u6700\u4e3a\u9002\u5408\u5904\u7406\u65b9\u6cd5\u3002\u6240\u4ee5\u6211\u7684\u6587\u7ae0\u4e5f\u771f\u7684\u5230\u4e86\u8be5\u7ed3\u675f\u7684\u65f6\u5019\u4e86\uff0c\u611f\u8c22\u5404\u4f4d\u7684\u6d4f\u89c8\u3002\u5982\u679c\u6211\u6709\u4ec0\u4e48\u5730\u65b9\u8bf4\u7684\u4e0d\u5bf9\uff0c\u8bf7\u4e0d\u541d\u8d50\u6559\uff0c\u8c22\u8c22\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u6211\u5199\u7684\u4e00\u4e2a\u670d\u52a1\u7684\u6e90\u4ee3\u7801\uff0c\u6ca1\u4ec0\u4e48\u529f\u80fd\uff0c\u53ea\u80fd\u542f\u52a8\u3001\u505c\u6b62\u548c\u5b89\u88c5\u3002<\/p>\n<p>#include &lt;windows.h&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;tchar.h&gt;<\/p>\n<p>#define SZAPPNAME &#8220;basicservice&#8221;<br \/>\n#define SZSERVICENAME &#8220;KService&#8221;<br \/>\n#define SZSERVICEDISPLAYNAME &#8220;KService&#8221;<br \/>\n#define SZDEPENDENCIES &#8220;&#8221;<\/p>\n<p>void WINAPI KServiceMain(DWORD argc, LPTSTR * argv);<br \/>\nvoid InstallService(const char * szServiceName);<br \/>\nvoid LogEvent(LPCTSTR pFormat, &#8230;);<br \/>\nvoid Start();<br \/>\nvoid Stop();<\/p>\n<p>SERVICE_STATUS ssStatus;<br \/>\nSERVICE_STATUS_HANDLE sshStatusHandle;<\/p>\n<p>int main(int argc, char * argv[])<br \/>\n{<br \/>\nif ((argc==2) &amp;&amp; (::strcmp(argv[1]+1, &#8220;install&#8221;)==0))<br \/>\n{<br \/>\nInstallService(&#8220;KService&#8221;);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p>SERVICE_TABLE_ENTRY\u00a0\u00a0\u00a0service_table_entry[] =<br \/>\n{<br \/>\n{&#8220;KService&#8221;, KServiceMain},<br \/>\n{NULL, NULL}<br \/>\n};<br \/>\n::StartServiceCtrlDispatcher(service_table_entry);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p>void InstallService(const char * szServiceName)<br \/>\n{<br \/>\nSC_HANDLE handle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);<br \/>\nchar szFilename[256];<br \/>\n::GetModuleFileName(NULL, szFilename, 255);<br \/>\nSC_HANDLE hService = ::CreateService(handle, szServiceName,<br \/>\nszServiceName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,<br \/>\nSERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, szFilename, NULL,<br \/>\nNULL, NULL, NULL, NULL);<br \/>\n::CloseServiceHandle(hService);<br \/>\n::CloseServiceHandle(handle);<br \/>\n}<\/p>\n<p>SERVICE_STATUS servicestatus;<br \/>\nSERVICE_STATUS_HANDLE servicestatushandle;<\/p>\n<p>void WINAPI ServiceCtrlHandler(DWORD dwControl)<br \/>\n{<br \/>\nswitch (dwControl)<br \/>\n{<\/p>\n<p>\/\/\u4e0b\u9762\u867d\u7136\u6dfb\u52a0\u4e86\u6682\u505c\u3001\u7ee7\u7eed\u7b49\u8bf7\u6c42\u7684\u5904\u7406\u4ee3\u7801\uff0c\u4f46\u6ca1\u6709\u5b9e\u9645\u4f5c\u7528<br \/>\n\/\/\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u5230\u4e86\u4e0b\u9762\u7684KServiceMain\u51fd\u6570\u91cc\u9762\u5c31\u660e\u767d\u4e86&#8230;<\/p>\n<p>case SERVICE_CONTROL_PAUSE:<br \/>\nservicestatus.dwCurrentState = SERVICE_PAUSE_PENDING;<br \/>\n\/\/ TODO: add code to set dwCheckPoint &amp; dwWaitHint<br \/>\n\/\/ This value need to try a lot to confirm<br \/>\n\/\/ &#8230;<br \/>\n::SetServiceStatus(servicestatushandle, &amp;servicestatus);<br \/>\n\/\/ TODO: add code to pause the service<br \/>\n\/\/ not called in this service<br \/>\n\/\/ &#8230;<br \/>\nservicestatus.dwCurrentState = SERVICE_PAUSED;<br \/>\n\/\/ TODO: add code to set dwCheckPoint &amp; dwWaitHint to 0<br \/>\nbreak;<\/p>\n<p>case SERVICE_CONTROL_CONTINUE:<br \/>\nservicestatus.dwCurrentState = SERVICE_CONTINUE_PENDING;<br \/>\n\/\/ TODO: add code to set dwCheckPoint &amp; dwWaitHint<br \/>\n::SetServiceStatus(servicestatushandle, &amp;servicestatus);<br \/>\n\/\/ TODO: add code to unpause the service<br \/>\n\/\/ not called in this service<br \/>\n\/\/ &#8230;<br \/>\nservicestatus.dwCurrentState = SERVICE_RUNNING;<br \/>\n\/\/ TODO: add code to set dwCheckPoint &amp; dwWaitHint to 0<br \/>\nbreak;<\/p>\n<p>case SERVICE_CONTROL_STOP:<br \/>\nservicestatus.dwCurrentState = SERVICE_STOP_PENDING;<br \/>\n\/\/ TODO: add code to set dwCheckPoint &amp; dwWaitHint<br \/>\n::SetServiceStatus(servicestatushandle, &amp;servicestatus);<br \/>\n\/\/ TODO: add code to stop the service<br \/>\nStop();<br \/>\nservicestatus.dwCurrentState = SERVICE_STOPPED;<br \/>\n\/\/ TODO: add code to set dwCheckPoint &amp; dwWaitHint to 0<br \/>\nbreak;<\/p>\n<p>case SERVICE_CONTROL_SHUTDOWN:<br \/>\n\/\/ TODO: add code for system shutdown<br \/>\n\/\/ as quick as possible<br \/>\nbreak;<\/p>\n<p>case SERVICE_CONTROL_INTERROGATE:<br \/>\n\/\/ TODO: add code to set the service status<br \/>\n\/\/ &#8230;<br \/>\nservicestatus.dwCurrentState = SERVICE_RUNNING;<br \/>\nbreak;<br \/>\n}<br \/>\n::SetServiceStatus(servicestatushandle, &amp;servicestatus);<br \/>\n}<\/p>\n<p>void WINAPI KServiceMain(DWORD argc, LPTSTR * argv)<br \/>\n{<br \/>\nservicestatus.dwServiceType = SERVICE_WIN32;<br \/>\nservicestatus.dwCurrentState = SERVICE_START_PENDING;<br \/>\nservicestatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;\/\/\u4e0a\u9762\u7684\u95ee\u9898\u7684\u7b54\u6848\u5c31\u5728\u8fd9\u91cc<br \/>\nservicestatus.dwWin32ExitCode = 0;<br \/>\nservicestatus.dwServiceSpecificExitCode = 0;<br \/>\nservicestatus.dwCheckPoint = 0;<br \/>\nservicestatus.dwWaitHint = 0;<\/p>\n<p>servicestatushandle =<br \/>\n::RegisterServiceCtrlHandler(&#8220;KService&#8221;, ServiceCtrlHandler);<br \/>\nif (servicestatushandle == (SERVICE_STATUS_HANDLE)0)<br \/>\n{<br \/>\nreturn;<br \/>\n}<\/p>\n<p>bool bInitialized = false;<br \/>\n\/\/ Initialize the service<br \/>\n\/\/ &#8230;<br \/>\nStart();<\/p>\n<p>bInitialized = true;<\/p>\n<p>servicestatus.dwCheckPoint = 0;<br \/>\nservicestatus.dwWaitHint = 0;<br \/>\nif (!bInitialized)<br \/>\n{<br \/>\nservicestatus.dwCurrentState = SERVICE_STOPPED;<br \/>\nservicestatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;<br \/>\nservicestatus.dwServiceSpecificExitCode = 1;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nservicestatus.dwCurrentState = SERVICE_RUNNING;<br \/>\n}<br \/>\n::SetServiceStatus(servicestatushandle, &amp;servicestatus);<br \/>\nreturn;<br \/>\n}<\/p>\n<p>void Start()<br \/>\n{<br \/>\nLogEvent(&#8220;Service Starting&#8230;&#8221;);<br \/>\n}<\/p>\n<p>void LogEvent(LPCTSTR pFormat, &#8230;)<br \/>\n{<br \/>\nTCHAR chMsg[256];<br \/>\nHANDLE hEventSource;<br \/>\nLPTSTR lpszStrings[1];<br \/>\nva_list pArg;<\/p>\n<p>va_start(pArg, pFormat);<br \/>\n_vstprintf(chMsg, pFormat, pArg);<br \/>\nva_end(pArg);<\/p>\n<p>lpszStrings[0] = chMsg;<\/p>\n<p>if (1)<br \/>\n{<br \/>\n\/\/ Get a handle to use with ReportEvent().<br \/>\nhEventSource = RegisterEventSource(NULL, &#8220;KService&#8221;);<br \/>\nif (hEventSource != NULL)<br \/>\n{<br \/>\n\/\/ Write to event log.<br \/>\nReportEvent(hEventSource, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (LPCTSTR*) &amp;lpszStrings[0], NULL);<br \/>\nDeregisterEventSource(hEventSource);<br \/>\n}<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n\/\/ As we are not running as a service, just write the error to the console.<br \/>\n_putts(chMsg);<br \/>\n}<br \/>\n}<\/p>\n<p><span class=\"htd\">void Stop()<br \/>\n{<br \/>\nLogEvent(&#8220;Service Stoped.&#8221;);<br \/>\n}<\/span><\/p>\n<\/div>\n<\/div>\n<\/article>\n<div class=\"article-bar-bottom\">\n<div class=\"tags-box artic-tag-box\"><span class=\"label\">\u6587\u7ae0\u6807\u7b7e\uff1a<\/span>\u00a0<a class=\"tag-link\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=windows&amp;t=blog\" target=\"_blank\" rel=\"noopener\">windows<\/a><a class=\"tag-link\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=service&amp;t=blog\" target=\"_blank\" rel=\"noopener\">service<\/a><a class=\"tag-link\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=manager&amp;t=blog\" target=\"_blank\" rel=\"noopener\">manager<\/a><a class=\"tag-link\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=null&amp;t=blog\" target=\"_blank\" rel=\"noopener\">null<\/a><a class=\"tag-link\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=winapi&amp;t=blog\" target=\"_blank\" rel=\"noopener\">winapi<\/a><a class=\"tag-link\" href=\"http:\/\/so.csdn.net\/so\/search\/s.do?q=binding&amp;t=blog\" target=\"_blank\" rel=\"noopener\">binding<\/a><\/div>\n<div class=\"tags-box\"><span class=\"label\">\u4e2a\u4eba\u5206\u7c7b\uff1a<\/span>\u00a0<a class=\"tag-link\" href=\"https:\/\/blog.csdn.net\/zhongguoren666\/article\/category\/855946\" target=\"_blank\" rel=\"noopener\">\u7f51\u7edc\u67b6\u6784<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Windows\u670d\u52a1\u7f16\u5199\u539f\u7406\u53ca\u63a2\u8ba8 201<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-1470","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=1470"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1470\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=1470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=1470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=1470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}