{"id":1323,"date":"2014-04-04T07:00:00","date_gmt":"2014-04-04T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/04\/04\/its-bad-enough-for-a-guest-to-demolish-the-hosts-house-demolishing-an-unrelated-persons-house-is-taking-it-to-a-whole-new-level\/"},"modified":"2014-04-04T07:00:00","modified_gmt":"2014-04-04T07:00:00","slug":"its-bad-enough-for-a-guest-to-demolish-the-hosts-house-demolishing-an-unrelated-persons-house-is-taking-it-to-a-whole-new-level","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140404-00\/?p=1323","title":{"rendered":"It&#039;s bad enough for a guest to demolish the host&#039;s house; demolishing an unrelated person&#039;s house is taking it to a whole new level"},"content":{"rendered":"<p>\n&#8220;How do I destroy a window that belongs to another process?&#8221;\n<\/p>\n<p>\nThe <code>Destroy&shy;Window<\/code> function will not destroy\nwindows that belong to another thread,\nmuch less another process.\nThe best you can do is post a\n<code>WM_CLOSE<\/code> message to the window to ask it nicely.\nThe <code>Def&shy;Window&shy;Proc<\/code> function response to the\n<code>WM_CLOSE<\/code> message by destroying the window,\nbut the window is free to add a custom handler for the\n<code>WM_CLOSE<\/code> message which rejects the request.\n<\/p>\n<p>\n(If you are thinking of posting the\n<code>WM_DESTROY<\/code> message,\nthen you&#8217;re the sort of person who\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/09\/26\/10216420.aspx\">\nprank-calls somebody pretending to be the police<\/a>.)\n<\/p>\n<p>\nIf you want to distinguish between user-initiated requests to close\nthe window (say, by clicking the X button in the corner),\nand your special, &#8220;No, really, just destroy the window&#8221; demand\nto close the window,\nyou can invent a private message for this purpose.\n<\/p>\n<pre>\n#define WM_FORCECLOSE (WM_APP + 3141)\nLRESULT CALLBACK VictimWndProc(HWND hwnd, UINT uMsg,\n    WPARAM wParam, LPARAM lParam)\n{\n switch (uMsg) {\n ...\n case WM_FORCECLOSE: DestroyWindow(hwnd); return 0;\n ...\n }\n return DefWindowProc(hwnd, uMsg, wParam, lParam);\n}\nvoid ForceCloseWindow(HWND hwnd)\n{\n PostMessage(hwnd, WM_FORCECLOSE, 0, 0);\n}\n<\/pre>\n<p>\nOf course, this\n<code>WM_FORCE&shy;CLOSE<\/code> message works only\nwith windows specially designed to understand it.\n<\/p>\n<p>\n&#8220;I can&#8217;t do that because the window I want to force closed\nbelongs to some application I did not write,\nso I cannot modify its window procedure.&#8221;\n<\/p>\n<p>\nThis is another case of\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/04\/30\/10004931.aspx\">\n<i>If it&#8217;s not yours,\nthen don&#8217;t mess with it without permission from the owner<\/i><\/a>.\nOne of the rules of thumb for real life is that if something\ndoesn&#8217;t belong to you,\nthen you shouldn&#8217;t mess with it unless you have permission from the owner.\n<\/p>\n<p>\nYou need to work with the application vendor\nto come to some sort of agreement on how you can tell the\napplication to close the window unconditionally.\nOtherwise, you&#8217;re even worse than the guest who\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/11\/18\/10238335.aspx\">\nvisits a house and calls the demolition company to have the house\ndestroyed<\/a>.\nYou&#8217;re the guy who calls the demolition company to\n<i>order the destruction of some completely unrelated house<\/i>.\n<\/p>\n<p>\nHow would you like it if a random person called\nthe demolition company to destroy your house?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How do I destroy a window that belongs to another process?&#8221; The Destroy&shy;Window function will not destroy windows that belong to another thread, much less another process. The best you can do is post a WM_CLOSE message to the window to ask it nicely. The Def&shy;Window&shy;Proc function response to the WM_CLOSE message by destroying the [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-1323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>&#8220;How do I destroy a window that belongs to another process?&#8221; The Destroy&shy;Window function will not destroy windows that belong to another thread, much less another process. The best you can do is post a WM_CLOSE message to the window to ask it nicely. The Def&shy;Window&shy;Proc function response to the WM_CLOSE message by destroying the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=1323"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1323\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=1323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=1323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=1323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}