{"id":99065,"date":"2018-06-21T07:00:00","date_gmt":"2018-06-21T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=99065"},"modified":"2019-03-13T00:42:34","modified_gmt":"2019-03-13T07:42:34","slug":"20180621-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180621-00\/?p=99065","title":{"rendered":"What can influence how much stack is consumed when sending a message?"},"content":{"rendered":"<p>Last time, we looked at <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20180620-00\/?p=99055#comment-1348075\">a simple program that recursively sent itself a message<\/a>, and we wondered, &#8220;Which will run out first? The user-mode stack or the kernel-mode stack?&#8221; Some of the calculations are under your control, but others can change at runtime. <\/p>\n<p>If the destination window belongs to the same thread as the sender, and there are no applicable window hooks, then the user-mode portion of <code>Send&shy;Message<\/code> says, &#8220;Well, I can handle this all by myself. I don&#8217;t need to ask for any help from kernel mode at all!&#8221; In that case, the entire message processing happens in user mode, and no kernel mode stack is consumed at all. <\/p>\n<p>On the other hand, if a window hook exists, then the user-mode portion of <code>Send&shy;Message<\/code> needs to call into kernel mode to dispatch the hooks, and in that case, it means that there will be stack consumed in kernel mode. <\/p>\n<p>The effect of window hooks means that the same program may crash in different ways depending on the configuration of the system it is running on. A program that installs a global message hook will change the failure mode. <\/p>\n<p>If you ran the program from last time and encountered a stack overflow in user mode, then you can make these changes to install your own hook and watch the failure mode change: <\/p>\n<pre>\n<font COLOR=\"blue\">HHOOK hhk;\n\nLRESULT CALLBACK MessageHookProc(int nCode,\n     WPARAM wParam, LPARAM lParam)\n{\n  return CallNextHookEx(hhk, nCode, wParam, lParam);\n}<\/font>\n\nint WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev,\n                   LPSTR lpCmdLine, int nShowCmd)\n{\n    ...\n        <font COLOR=\"blue\">hhk = SetWindowsHookEx(WH_CALLWNDPROC, MessageHookProc,\n            hinst, GetCurrentThreadId());<\/font>\n\n        while (GetMessage(&amp;msg, NULL, 0, 0)) {\n            ...\n        }\n\n        <font COLOR=\"blue\">UnhookWindowsHookEx(hhk);<\/font>\n        ...\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Who wants to know? No really, that&#8217;s the answer.<\/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-99065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Who wants to know? No really, that&#8217;s the answer.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/99065","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=99065"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/99065\/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=99065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=99065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=99065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}