{"id":2934,"date":"2019-10-11T13:13:15","date_gmt":"2019-10-11T05:13:15","guid":{"rendered":"https:\/\/www.aoisnow.net\/blog\/?p=2934"},"modified":"2019-10-11T13:13:15","modified_gmt":"2019-10-11T05:13:15","slug":"tont-38373-%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bd%a0%e4%b8%8d%e8%83%bd%e6%8d%95%e8%8e%b7terminateprocess%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.aoisnow.net\/blog\/archives\/2934","title":{"rendered":"TONT 38373 \u4e3a\u4ec0\u4e48\u4f60\u4e0d\u80fd\u6355\u83b7TerminateProcess\uff1f"},"content":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040722-00\/?p=38373\" target=\"_blank\" rel=\"noopener\">https:\/\/devblogs.microsoft.com\/oldnewthing\/20040722-00\/?p=38373<\/a><\/p>\n<p class=\"\">If a user fires up Task Manager and clicks \u201cEnd Task\u201d on your program, Windows first tries to shut down your program nicely, by sending WM_CLOSE messages to GUI programs and CTRL_CLOSE_EVENT events to console programs. But you don\u2019t get a chance to intercept TerminateProcess. Why not?<\/p>\n<p>\u5982\u679c\u7528\u6237\u6253\u5f00\u4efb\u52a1\u7ba1\u7406\u5668\uff0c\u9009\u4e2d\u4f60\u7684\u7a0b\u5e8f\u4e4b\u540e\u70b9\u51fb\u300e\u7ed3\u675f\u4efb\u52a1\u300f\uff0cWindows\u5c06\u9996\u5148\u5c1d\u8bd5\u4f18\u96c5\u5730\u901a\u77e5\u4f60\u7684\u7a0b\u5e8f\u8fdb\u884c\u5173\u95ed\uff0c\u65b9\u6cd5\u662f\u5411GUI\u5e94\u7528\u53d1\u9001WM_CLOSE\u6d88\u606f\uff0c\u6216\u8005\u5411\u63a7\u5236\u53f0\u5e94\u7528\u53d1\u9001CTRL_CLOSE_EVENT\u4e8b\u4ef6\u3002\u4e0d\u8fc7\uff0c\u4f60\u662f\u4e0d\u53ef\u80fd\u62e6\u622a TerminateProcess \u7684\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n<p class=\"\"><a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/dllproc\/base\/terminateprocess.asp\" target=\"_blank\" rel=\"noopener\">TerminateProcess<\/a>\u00a0is the low-level process killing function. It bypasses DLL_PROCESS_DETACH and anything else in the process. Once you kill with TerminateProcess, no more user-mode code will run in that process. It\u2019s gone.\u00a0<a class=\"\" href=\"http:\/\/www.bartleby.com\/66\/76\/15776.html\" target=\"_blank\" rel=\"noopener\">Do not pass go. Do not collect $200<\/a>.<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/processthreadsapi\/nf-processthreadsapi-terminateprocess?redirectedfrom=MSDN\" target=\"_blank\" rel=\"noopener\">TerminateProcess<\/a>\uff08\u8bd1\u6ce8\uff1a\u539f\u6587\u94fe\u63a5\u5df2\u5931\u6548\uff0c\u6b64\u5904\u4e3aMSDN\u4e0a\u5bf9\u5e94\u7684\u65b0\u94fe\u63a5\uff09\u662f\u4e00\u79cd\u5e95\u5c42\u7ed3\u675f\u4efb\u52a1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7ed5\u8fc7\u00a0DLL_PROCESS_DETACH \u53ca\u8fdb\u7a0b\u4e2d\u7684\u4efb\u4f55\u4e1c\u897f\u3002\u4e00\u65e6\u8c03\u7528 TerminateProcess \u6765\u7ed3\u675f\u8fdb\u7a0b\u7684\u8bdd\uff0c\u8be5\u8fdb\u7a0b\u4e2d\u5c06\u4e0d\u518d\u6709\u4efb\u4f55\u7528\u6237\u6a21\u5f0f\u7684\u4ee3\u7801\u53ef\u4ee5\u8fd0\u884c\u3002\u5c31\u90a3\u4e48\u7ed3\u675f\u4e86\u3002\u522b\u518d\u60f3\u7740\u8fc7\u8d77\u70b9\u4e86\uff0c\u4e5f\u522b\u518d\u60f3\u6323200\u5757\u7684\u4e8b\u4e86\u3002\uff08\u8bd1\u6ce8\uff1aDo not pass go. Do not collect $200\u662f\u539f\u7248\u201c\u5927\u5bcc\u7fc1\u201d\u6e38\u620f\u4e2d\u76f4\u63a5\u5c06\u5176\u5b83\u73a9\u5bb6\u9001\u5165\u76d1\u72f1\u683c\u7684\u4e8b\u4ef6\u5361\u7247\u80cc\u9762\u6587\u5b57\uff0c\u7528\u4e8e\u5f3a\u8c03\u201c\u5565\u4e5f\u522b\u60f3\u4e86\uff0c\u6ca1\u6551\u4e86\u201d\u7684\u610f\u601d\u3002\uff09<\/p>\n<p class=\"\">If you could intercept TerminateProcess, then you would be escalating\u00a0<a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/02\/16\/73780.aspx\" target=\"_blank\" rel=\"noopener\">the arms race between programs and users<\/a>. Suppose you could intercept it. Well, then if you wanted to make your program unkillable, you would just hang in your TerminateProcess handler!<\/p>\n<p>\u5047\u8bbe\u6709\u529e\u6cd5\u62e6\u622a\u00a0TerminateProcess \u7684\u8bdd\uff0c\u4e0d\u8fc7\u662f\u6076\u5316\u4e86<a href=\"https:\/\/www.aoisnow.net\/blog\/archives\/2506\" target=\"_blank\" rel=\"noopener\">\u7a0b\u5e8f\u548c\u7528\u6237\u4e4b\u95f4\u7684\u519b\u5907\u7ade\u8d5b<\/a>\u800c\u5df2\u3002\u8bbe\u60f3\u5982\u679c\u53ef\u4ee5\u62e6\u622a\u5b83\uff0c\u90a3\u4e48\uff0c\u5982\u679c\u60f3\u8ba9\u4f60\u7684\u7a0b\u5e8f\u65e0\u6cd5\u88ab\u4e2d\u6b62\uff0c\u53ea\u8981\u5728\u4f60\u7684 TerminateProcess \u5904\u7406\u8fdb\u7a0b\u4e2d\u6302\u8d77\u5c31\u597d\u5566\uff01<\/p>\n<p class=\"\">And then people would ask for \u201ca way to kill a process that is refusing to be killed with TerminateProcess,\u201d and we\u2019d be back to where we started.<\/p>\n<p>\u7136\u540e\u5c31\u4f1a\u6709\u4eba\u63d0\u51fa\u300e\u5982\u4f55\u4e2d\u6b62\u62d2\u7edd\u88ab TerminateProcess \u6740\u6389\u7684\u8fdb\u7a0b\u300f\u8fd9\u6837\u7684\u95ee\u9898\uff0c\u4e8e\u662f\u6211\u4eec\u53c8\u56de\u5230\u4e86\u95ee\u9898\u7684\u5f00\u5934\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/devblogs.microsoft.com\/oldnewthing\/2004072 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2934","post","type-post","status-publish","format-standard","hentry","category-tont_history"],"_links":{"self":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/2934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/comments?post=2934"}],"version-history":[{"count":0,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/2934\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/media?parent=2934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/categories?post=2934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/tags?post=2934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}