{"id":3143,"date":"2020-08-21T09:40:10","date_gmt":"2020-08-21T01:40:10","guid":{"rendered":"https:\/\/www.aoisnow.net\/blog\/?p=3143"},"modified":"2020-08-21T09:40:10","modified_gmt":"2020-08-21T01:40:10","slug":"tont-34913-%e5%a6%82%e6%9e%9c-initcommoncontrols-%e4%bb%80%e4%b9%88%e4%b9%9f%e6%b2%a1%e5%81%9a%ef%bc%8c%e4%b8%ba%e4%bb%80%e4%b9%88%e8%bf%98%e5%be%97%e8%b0%83%e7%94%a8%e5%ae%83%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.aoisnow.net\/blog\/archives\/3143","title":{"rendered":"TONT 34913 \u5982\u679c InitCommonControls \u4ec0\u4e48\u4e5f\u6ca1\u505a\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u5f97\u8c03\u7528\u5b83\uff1f"},"content":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050718-16\/?p=34913\" target=\"_blank\" rel=\"noopener\">https:\/\/devblogs.microsoft.com\/oldnewthing\/20050718-16\/?p=34913<\/a><\/p>\n<p>One of the problems beginners run into when they start using shell common controls is that they forget to call the InitCommonControls function. But if you were to disassemble the InitCommonControls function itself, you\u2019ll see that it, like the FlushInstructionCache function, doesn\u2019t actually do anything.<\/p>\n<p>\u521d\u5b66\u8005\u5b66\u4e60\u4f7f\u7528\u7cfb\u7edf\u5916\u58f3\u901a\u7528\u63a7\u4ef6\u65f6\uff0c\u7ecf\u5e38\u9047\u5230\u7684\u5176\u4e2d\u4e00\u4e2a\u95ee\u9898\u662f\u5fd8\u8bb0\u8c03\u7528\u00a0InitCommonControls\u00a0 \u65b9\u6cd5\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u5bf9\u00a0InitCommonControls \u65b9\u6cd5\u53cd\u7f16\u8bd1\u4e00\u4e0b\u7684\u8bdd\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u50cf\u00a0FlushInstructionCache \u4e00\u6837\uff0c\u4e8b\u5b9e\u4e0a\u4ec0\u4e48\u4e8b\u60c5\u4e5f\u6ca1\u505a\u3002<\/p>\n<p>Then why do you need to call it?<\/p>\n<p>\u90a3\u4e48\uff0c\u5fc5\u987b\u8c03\u7528\u5b83\u7684\u610f\u4e49\u4f55\u5728\u5462\uff1f<\/p>\n<p>As with FlushInstructionCache, what\u2019s important is not what it performs, but just the fact that you called it.<\/p>\n<p>\u5c31\u50cf\u00a0FlushInstructionCache \u4e00\u6837\uff0c\u91cd\u70b9\u4e0d\u5728\u5b83\u505a\u4e86\u4ec0\u4e48\uff0c\u800c\u5728\u4e8e\u4f60\u8c03\u7528\u4e86\u5b83\u8fd9\u4ef6\u4e8b\u4e0a\u3002<\/p>\n<p>Recall that merely listing a lib file in your dependencies doesn\u2019t actually cause your program to be bound to the corresponding DLL. You have to call a function in that DLL in order for there to be an import entry for that DLL. And InitCommonControls is that function.<\/p>\n<p>\u56de\u60f3\u4e00\u4e0b\uff0c\u53ea\u662f\u5c06\u67d0\u4e2a\u5e93\u6587\u4ef6\u5217\u5728\u4f60\u7684\u4f9d\u8d56\u5217\u8868\u91cc\uff0c\u5e76\u4e0d\u610f\u5473\u7740\u4f60\u7684\u7a0b\u5e8f\u5c31\u4e0e\u5bf9\u5e94\u7684DLL\u7ed1\u5b9a\u4e86\u3002\u4f60\u5f97\u8c03\u7528\u8fd9\u4e2aDLL\u4e2d\u7684\u67d0\u4e2a\u65b9\u6cd5\uff0c\u624d\u80fd\u4fdd\u8bc1\u5176\u5165\u53e3\u70b9\u7684\u5b58\u5728\uff0c\u800c\u00a0InitCommonControls \u505a\u7684\u5c31\u662f\u8fd9\u4ef6\u4e8b\u3002<\/p>\n<p>Without the InitCommonControls function, a program that wants to use the shell common controls library would otherwise have no reference to COMCTL32.DLL in its import table. This means that when the program loads, COMCTL32.DLL is not loaded and therefore is not initialized. Which means that it doesn\u2019t register its window classes. Which means that your call to the CreateWindow function fails because the window class has not been registered.<\/p>\n<p>\u6ca1\u6709\u5bf9 InitCommonControls \u7684\u8c03\u7528\uff0c\u8981\u4f7f\u7528\u7cfb\u7edf\u5916\u58f3\u901a\u7528\u63a7\u4ef6\u5e93\u7684\u7a0b\u5e8f\uff0c\u5176\u5bfc\u5165\u8868\u4e2d\u5c31\u4e0d\u5b58\u5728\u5bf9 COMCTL32.DLL \u7684\u5f15\u7528\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u5f53\u7a0b\u5e8f\u52a0\u8f7d\u65f6\uff0cCOMCTL32.DLL \u5e76\u6ca1\u6709\u88ab\u52a0\u8f7d\uff0c\u56e0\u6b64\u4e5f\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff0c\u4e5f\u5c31\u610f\u5473\u7740\u6ca1\u6709\u6ce8\u518c\u5176\u7a97\u53e3\u7c7b\uff0c\u6700\u7ec8\u610f\u5473\u7740\u5f53\u4f60\u8c03\u7528\u00a0CreateWindow \u65f6\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u7a97\u53e3\u7c7b\u5c1a\u672a\u88ab\u6ce8\u518c\u3002<\/p>\n<p>That\u2019s why you have to call a function that does nothing. It\u2019s for your own good.<\/p>\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u4f60\u9700\u8981\u8c03\u7528\u4e00\u4e2a\u4ec0\u4e48\u4e5f\u4e0d\u505a\u7684\u65b9\u6cd5\u7684\u539f\u56e0\u2014\u2014\u8fd9\u662f\u4e3a\u4f60\u597d\u3002<\/p>\n<p>(Of course, there\u2019s the new InitCommonControlsEx function that lets you specify which classes you would like to be registered. Only the classic Windows 95 classes are registered when COMCTL32.DLL loads. For everything else you have to ask for it explicitly.)<\/p>\n<p>\uff08\u5f53\u7136\u4e86\uff0c\u540e\u6765\u8fd8\u6709\u4e2a\u00a0InitCommonControlsEx \u5141\u8bb8\u4f60\u6307\u5b9a\u8981\u6ce8\u518c\u54ea\u4e9b\u7c7b\u3002\u5f53 COMCTL32.DLL \u52a0\u8f7d\u65f6\uff0c\u53ea\u6709\u4f20\u7edf\u7684 Windows 95 \u7c7b\u662f\u9ed8\u8ba4\u6ce8\u518c\u7684\uff0c\u8981\u60f3\u7528\u5230\u5176\u5b83\u7684\u7c7b\uff0c\u4f60\u5fc5\u987b\u8fdb\u884c\u660e\u786e\u7684\u6307\u5b9a\u3002\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/devblogs.microsoft.com\/oldnewthing\/2005071 [&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-3143","post","type-post","status-publish","format-standard","hentry","category-tont_history"],"_links":{"self":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/3143","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=3143"}],"version-history":[{"count":0,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/3143\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/media?parent=3143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/categories?post=3143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/tags?post=3143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}