{"id":14553,"date":"2010-03-19T07:00:00","date_gmt":"2010-03-19T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/03\/19\/how-does-delay-loading-use-binding-information\/"},"modified":"2010-03-19T07:00:00","modified_gmt":"2010-03-19T07:00:00","slug":"how-does-delay-loading-use-binding-information","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100319-00\/?p=14553","title":{"rendered":"How does delay-loading use binding information?"},"content":{"rendered":"<p>In the documentation for delay-loading, there&#8217;s a remark that says that  <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hdx9xk46.aspx\"> the call to <code>GetProcAddress<\/code> can be avoided if there is binding information<\/a>. A customer who received the explanation of <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2010\/02\/01\/9956102.aspx\"> why you can&#8217;t delay-load <code>kernel32<\/code><\/a> pointed out that paragraph and asked whether this means that you can delay-load <code>kernel32<\/code> if you bind to it. (Getting around to answering this question was the point of <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2010\/03\/17\/9980011.aspx\"> the past<\/a> <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2010\/03\/18\/9980802.aspx\"> few days<\/a>.)\n Let&#8217;s take another look at what that <code>GetProcAddress<\/code>-avoidance optimization does. Actually, it&#8217;s just another look at what the module loader does when it&#8217;s time to resolve imports to a bound DLL: At build time, the actual function pointers are precomputed and cached, along with the timestamp of the DLL those precomputed values came from. At run time, the delay-load stubs check the timestamp of the target DLL and compare it against the timestamp that it had cached. If they are the same, then they skip the call to <code>GetProcAddress<\/code> and use the cached value.\n In other words, the delay-load stubs use binding information in exactly the same way the module loader does.\n Does this mean that you can now delay-load <code>kernel32<\/code>?\n No. First of all, if the timestamps don&#8217;t match or if the target DLL was not loaded at its preferred address, then the binding information is of no use&mdash;you have a cache miss. In that case, the module loader (and the delay-load stubs) must obtain the function pointers the old-fashioned way. You can&#8217;t assume that your binding information will always be accurate. (For example, after your module was bound to <code>kernel32<\/code>, there may have been a security update which modified <code>kernel32<\/code>, which invalidates your binding information.)<\/p>\n<p> And besides, even if the binding information were used, you still have to call <code>LoadLibrary<\/code> to get the target DLL loaded in the first place. Even though binding may have optimized away one call to <code>kernel32<\/code>, you still have that <code>LoadLibrary<\/code> to deal with. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the documentation for delay-loading, there&#8217;s a remark that says that the call to GetProcAddress can be avoided if there is binding information. A customer who received the explanation of why you can&#8217;t delay-load kernel32 pointed out that paragraph and asked whether this means that you can delay-load kernel32 if you bind to it. (Getting [&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-14553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>In the documentation for delay-loading, there&#8217;s a remark that says that the call to GetProcAddress can be avoided if there is binding information. A customer who received the explanation of why you can&#8217;t delay-load kernel32 pointed out that paragraph and asked whether this means that you can delay-load kernel32 if you bind to it. (Getting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/14553","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=14553"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/14553\/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=14553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=14553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=14553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}