{"id":2322,"date":"2018-09-11T08:27:35","date_gmt":"2018-09-11T00:27:35","guid":{"rendered":"https:\/\/www.aoisnow.net\/blog\/?p=2322"},"modified":"2018-09-11T08:27:35","modified_gmt":"2018-09-11T00:27:35","slug":"tont-41673-gwlp_wndproc%e8%bf%94%e5%9b%9e%e7%9a%84%e8%bf%99%e4%ba%9b%e5%a5%87%e6%80%aa%e7%9a%84%e5%80%bc%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.aoisnow.net\/blog\/archives\/2322","title":{"rendered":"TONT 41673 GWLP_WNDPROC\u8fd4\u56de\u7684\u8fd9\u4e9b\u5947\u602a\u7684\u503c\u662f\u4ec0\u4e48\u610f\u601d\uff1f"},"content":{"rendered":"<p>\u4e0d\u77e5\u4e0d\u89c9\u95f4\u3002<\/p>\n<p><!--more--><\/p>\n<p>GetWindowLongPtr(hwnd, GWLP_WNDPROC) [or GetWindowLong(hwnd, GWL_WNDPROC) if you haven&#8217;t yet made your code 64-bit compatible] is supposed to return the current window procedure. Why do I sometimes get wacko values?<\/p>\n<p>GetWindowLongPtr(hwnd, GWLP_WNDPROC)\u3010\u6216\u8005\u662f GetWindowLong(hwnd, GWL_WNDPROC)\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u8fd8\u6ca1\u6709\u4e3a64\u4f4d\u517c\u5bb9\u6027\u4f18\u5316\u7684\u8bdd\u3011\u5e94\u5f53\u8fd4\u56de\u5f53\u524d\u7684\u7a97\u4f53\u8fc7\u7a0b\uff0c\u90a3\u4e3a\u4ec0\u4e48\u6709\u65f6\u8fd4\u56de\u503c\u770b\u4e0a\u53bb\u602a\u602a\u7684\u5462\uff1f<\/p>\n<p>Because sometimes &#8220;you can&#8217;t handle the truth&#8221;.<\/p>\n<p>\u56e0\u4e3a\uff0c\u6709\u65f6\u4f60\u662f\u300e\u63a5\u53d7\u4e0d\u4e86\u4e8b\u5b9e\u300f\u7684\u3002<\/p>\n<p>If the current window procedure is incompatible with the caller of GetWindowLongPtr, then the real function pointer cannot be returned since you can&#8217;t call it. Instead, a &#8220;magic cookie&#8221; is returned. The sole purpose of this cookie is to be recognized by CallWindowProc so it can translate the message parameters into the format that the window procedure expects.<\/p>\n<p>\u5982\u679c\u5f53\u524d\u7684\u7a97\u4f53\u8fc7\u7a0b\u4e0eGetWindowLongPtr\u7684\u8c03\u7528\u8005\u4e0d\u517c\u5bb9\uff0c\u90a3\u4e48\u662f\u4e0d\u80fd\u8fd4\u56de\u5b9e\u9645\u7684\u51fd\u6570\u6307\u9488\u7684\uff0c\u56e0\u4e3a\u8c03\u7528\u65b9\u65e0\u6cd5\u8c03\u7528\u5b83\u3002\u4f5c\u4e3a\u66ff\u4ee3\u65b9\u6848\uff0cGetWindowLongPtr\u4f1a\u8fd4\u56de\u4e00\u4e2a\u300e\u795e\u5947\u5c0f\u751c\u997c\u300f\uff0c\u5176\u610f\u4e49\u5728\u4e8e\u53ef\u4ee5\u88abCallWindowProc\u8bc6\u522b\uff0c\u7136\u540e\u5c06\u7a97\u4f53\u6d88\u606f\u7684\u53c2\u6570\u8f6c\u5316\u4e3a\u8be5\u7a97\u4f53\u8fc7\u7a0b\u6240\u671f\u671b\u7684\u683c\u5f0f\u3002<\/p>\n<p>For example, suppose that you are running Windows XP and the window is a UNICODE window, but a component compiled as ANSI calls GetWindowLong(hwnd, GWL_WNDPROC). The raw window procedure can&#8217;t be returned, because the caller is using ANSI window messages, but the window procedure expects UNICODE window messages. So instead, a magic cookie is returned. When you pass this magic cookie to CallWindowProc, it recognizes it as a &#8220;Oh, I need to convert the message from ANSI to UNICODE and then give the UNICODE message to that window procedure over there.&#8221;<\/p>\n<p>\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u7684\u73af\u5883\u662fWindows XP\uff0c\u7a97\u4f53\u662fUnicode\u7a97\u4f53\uff0c\u4f46\u67d0\u4e2a\u7528ANSI\u7f16\u7801\u7f16\u8bd1\u7684\u7ec4\u4ef6\u8c03\u7528\u4e86GetWindowLong(hwnd, GWL_WNDPROC)\u3002\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\uff09\u662f\u4e0d\u80fd\u8fd4\u56de\u539f\u59cb\u7684\u7a97\u4f53\u8fc7\u7a0b\u7684\uff0c\u56e0\u4e3a\u8c03\u7528\u65b9\u4f7f\u7528\u7684\u662fANSI\u7f16\u7801\u7684\u7a97\u4f53\u6d88\u606f\uff0c\u4f46\u7a97\u4f53\u8fc7\u7a0b\u671f\u671b\u7684\u662fUnicode\u7a97\u4f53\u6d88\u606f\u3002\u4f5c\u4e3a\u66ff\u4ee3\uff0c\u8fd4\u56de\u503c\u5c31\u662f\u90a3\u4e2a\u300e\u795e\u5947\u5c0f\u751c\u997c\u300f\u3002\u5f53\u4f60\u628a\u8fd9\u5757\u5c0f\u751c\u997c\u4f20\u9012\u7ed9CallWindowProc\u65f6\uff0cCallWindowProc\u5c31\u4f1a\u610f\u8bc6\u5230\uff1a\u300e\u5662\uff0c\u6211\u9700\u8981\u628a\u4f20\u5165\u7684ANSI\u7a97\u4f53\u6d88\u606f\u8f6c\u6362\u4e3aUnicode\u7684\uff0c\u7136\u540e\u518d\u628a\u8f6c\u6362\u540e\u7684Unicode\u7a97\u4f53\u6d88\u606f\u4f20\u9012\u7ed9\u76ee\u6807\u7a97\u4f53\u8fc7\u7a0b\u3002\u300f<\/p>\n<p>As another example, suppose that you are running Windows 95 and the window was created by a 32-bit component, but a 16-bit component calls GetWindowLong(hwnd, GWLP_WNDPROC). Again, the raw 32-bit window procedure can&#8217;t be returned since the message needs to be converted from 16-bit to 32-bit. (And besides, a 16-bit program can&#8217;t jump to a 32-bit flat address.) So instead, again, a magic cookie is returned which CallWindowProc recognizes as a &#8220;Oh, I need to convert the message from 16-bit to 32-bit and then give the converted message to that window procedure over there.&#8221;<\/p>\n<p>\u518d\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u4f60\u7684\u73af\u5883\u662fWindows 95\uff0c\u6709\u4e2a\u7a97\u4f53\u662f\u7531\u67d0\u4e2a32\u4f4d\u7ec4\u4ef6\u521b\u5efa\u7684\uff0c\u4f46\u67d0\u4e2a16\u4f4d\u7684\u7ec4\u4ef6\u8c03\u7528\u4e86GetWindowLong(hwnd, GWLP_WNDPROC)\u3002\u540c\u6837\uff0c\u539f\u59cb\u768432\u4f4d\u7a97\u4f53\u8fc7\u7a0b\u4e0d\u80fd\u76f4\u63a5\u8fd4\u56de\uff0c\u56e0\u4e3a\u63a5\u4e0b\u6765\u7684\u7a97\u4f53\u6d88\u606f\u9700\u8981\u753116\u4f4d\u8f6c\u6362\u4e3a32\u4f4d\u3002\uff08\u6b64\u5916\uff0c16\u4f4d\u4ee3\u7801\u4e5f\u4e0d\u80fd\u76f4\u63a5\u8df3\u8f6c\u523032\u4f4d\u7684\u5e73\u9762\u5730\u5740\u3002\uff09\u6240\u4ee5\u540c\u6837\u4f5c\u4e3a\u66ff\u4ee3\uff0c\u8fd4\u56de\u7684\u300e\u795e\u5947\u5c0f\u751c\u997c\u300f\u518d\u5ea6\u88abCallWindowProc\u8bc6\u522b\uff0c\u4f7f\u5176\u610f\u8bc6\u5230\u300e\u5662\uff0c\u6211\u5f97\u628a\u4f20\u5165\u7684\u7a97\u4f53\u6d88\u606f\u4ece16\u4f4d\u8f6c\u6362\u4e3a32\u4f4d\uff0c\u7136\u540e\u628a\u8f6c\u6362\u540e\u7684\u6d88\u606f\u4f20\u9012\u7ed9\u76ee\u6807\u7a97\u4f53\u8fc7\u7a0b\u3002\u300f<\/p>\n<p>(These conversions are known as &#8220;thunks&#8221;.)<\/p>\n<p>\uff08\u8fd9\u79cd\u8f6c\u6362\u901a\u5e38\u88ab\u79f0\u4f5cthunks\u3002\uff09\uff08\u8bd1\u6ce8\uff1athunk function\u5373\u5305\u88c5\u4e86\u4f20\u540d\u8c03\u7528\u65b9\u6cd5\u7684\u51fd\u6570\uff0c\u4f3c\u4e4e\u6ca1\u6709\u4e2d\u6587\u8bd1\u540d\u3002\uff09<\/p>\n<p>So remember, the only things you can do with the values you get from GetWindowLongPtr(hwnd, GWLP_WNDPROC) are to (1) pass them to CallWindowProc, or (2) pass them back to SetWindowLongPtr(hwnd, GWLP_WNDPROC).<\/p>\n<p>\u6240\u4ee5\u8bf7\u8bb0\u4f4f\uff0c\u5bf9\u4e8eGetWindowLongPtr(hwnd, GWLP_WNDPROC)\u7684\u8fd4\u56de\u503c\uff0c\u4f60\u6240\u80fd\u505a\u7684\u662f\u53ea\u6709\uff1a\uff081\uff09\u5c06\u8fd4\u56de\u503c\u8f6c\u624b\u53d1\u7ed9CallWindowProc\uff082\uff09\u5c06\u8fd4\u56de\u503c\u56de\u5934\u4ea4\u7ed9SetWindowLongPtr(hwnd, GWLP_WNDPROC)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0d\u77e5\u4e0d\u89c9\u95f4\u3002<\/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-2322","post","type-post","status-publish","format-standard","hentry","category-tont_history"],"_links":{"self":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/2322","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=2322"}],"version-history":[{"count":0,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/2322\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/media?parent=2322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/categories?post=2322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/tags?post=2322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}