{"id":2553,"date":"2013-11-27T07:00:00","date_gmt":"2013-11-27T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/11\/27\/the-case-of-the-dll-that-refuses-to-load\/"},"modified":"2013-11-27T07:00:00","modified_gmt":"2013-11-27T07:00:00","slug":"the-case-of-the-dll-that-refuses-to-load","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20131127-00\/?p=2553","title":{"rendered":"The case of the DLL that refuses to load"},"content":{"rendered":"<p>\nA customer reported that they had a problem that occurred\nonly on some machines but not others.\nTheir application called\n<\/p>\n<pre>\nHINSTANCE hinst = LoadLibraryEx(strModule, LOAD_WITH_ALTERED_SEARCH_PATH);\n<\/pre>\n<p>\nand the call succeeded on some machines, but failed on others\nwith error\n<code>ERROR_MOD_NOT_FOUND<\/code>\n(&#8220;The specified module could not be found&#8221;).\nThe path was a fully-qualified path to a file that\nwas confirmed to exist and be readable.\n<\/p>\n<pre>\nstrModule = 0x09e875b4 \"C:\\Users\\Bob\\Desktop\\CopyAndRun\\Contoso.dll\"\n<\/pre>\n<p>\nIf the <code>sxe ld Contoso.dll<\/code> command was used in the debugger\nto break when the DLL loads,\nthe breakpoint does fire,\nbut a breakpoint on Contoso&#8217;s <code>Dll&shy;Main<\/code> is never hit.\n&#8220;I think this means that the problem is not that Contoso failed\nto initialize,\nbut what <i>does<\/i> it mean?&#8221;\n<\/p>\n<p>\nIf you get a break from <code>sxeld<\/code> but no breakpoint on\n<code>Dll&shy;Main<\/code>,\nthen it means that the DLL was found but couldn&#8217;t be loaded.\nYou can use\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/hardware\/ff556886(v=vs.85).aspx\">\nloader snaps<\/a>\nwill\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/junfeng\/archive\/2006\/11\/20\/debugging-loadlibrary-failures.aspx\">\ntell you what went wrong<\/a>.\n&#8220;My psychic powers tell me that a dependent DLL could not be found\nor initialized.&#8221;\n<\/p>\n<p>\nThe customer replied,\n&#8220;Ah, of course. We&#8217;ll look into that.&#8221;\n<\/p>\n<p>\nA short while later, they confirmed the diagnosis.\n&#8220;The Contoso DLL was dependent on a version of the C runtime library\nthat was not installed on the machines where it failed to load.\nBut as a follow-up question:\nI would have expected that the standard\n<i>The program can&#8217;t start because XYZ is missing from your computer.<\/i>\ndialog to appear in this case.\nWhy isn&#8217;t it?&#8221;\n<\/p>\n<p>\nThe reason is there in the error message:\nThe &#8220;missing file&#8221; error message is shown only when a program\ncannot <i>start<\/i> due to a missing file.\nSpecifically, it is raised by the loader only during the initial\nDLL resolution phase that occurs as part of process initialization.\nThese are the DLLs linked implicitly via the module header\nbecause you linked against <code>kernel32.lib<\/code>, for example.\nDLLs loaded explicitly via <code>Load&shy;Library<\/code> do not\ndisplay this error message;\ninstead, the error is returned back to the program,\nwhere it is expected to take appropriate recovery steps.\nBy comparison,\nif DLL resolution fails during process initialization,\nthere is nowhere to return the failure code.\nYou can&#8217;t return it to the program since the program isn&#8217;t running yet.\nThe only place to put the error is on the screen.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer reported that they had a problem that occurred only on some machines but not others. Their application called HINSTANCE hinst = LoadLibraryEx(strModule, LOAD_WITH_ALTERED_SEARCH_PATH); and the call succeeded on some machines, but failed on others with error ERROR_MOD_NOT_FOUND (&#8220;The specified module could not be found&#8221;). The path was a fully-qualified path to a file [&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-2553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer reported that they had a problem that occurred only on some machines but not others. Their application called HINSTANCE hinst = LoadLibraryEx(strModule, LOAD_WITH_ALTERED_SEARCH_PATH); and the call succeeded on some machines, but failed on others with error ERROR_MOD_NOT_FOUND (&#8220;The specified module could not be found&#8221;). The path was a fully-qualified path to a file [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2553","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=2553"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2553\/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=2553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}