title: Win32 module loading from multiple directories
body: <p>I have a program which stores plugins in multiple directories, like so:</p>
  
  <pre><code>root/
    core/bin/
      app.exe
      core.dll
      plugin.dll
      support.dll
    a/bin/
      a.dll
      a_support.dll
  </code></pre>
  
  <p>In this example, <code>a.dll</code> imports <code>core.dll</code>, <code>support.dll</code>, and <code>a_support.dll</code> (they are in that order in the import table). <code>a_support.dll</code> imports <code>support.dll</code>. I can change all but the support modules, those are redists of a third-party library.</p>
  
  <p>My code calls <code>LoadLibraryEx(name, NULL, LOAD_WITH_ALTERED_SEARCH_PATH)</code> to load each plugin. For <code>core.dll</code> and <code>plugin.dll</code>, this works fine. </p>
  
  <p>When I try to load <code>a.dll</code>, it fails <em>saying <code>a_support.dll</code> was not found</em>. No errors about <code>core.dll</code> or <code>support.dll</code>, perhaps because they're already in memory. </p>
  
  <p>My suspicion is that when <code>a_support.dll</code> is loaded, <code>support.dll</code> cannot be found, but this seems unusual as <code>a.dll</code> appears to import <code>support.dll</code> before <code>a_support.dll</code>.</p>
  
  <p>Is this layout of modules <em>even possible</em> to use? Will the system be able to use the already-loaded support DLLs, or will it go searching for them and fail? Would there be a way to handle this through manifests? Is there a way to make this work, or will I have to relocate all modules into a single directory?</p>
  
  <p><strong>Edit:</strong> At Adrian McCarthy's suggestion, I ran the loading sequence with Process Monitor tracking, and it seems that when I call <code>LoadLibrary("root/a/bin/a.dll", ...)</code>, it starts by searching the root directory, then system directories, then down through the path. For some reason, it never searches <code>a/bin/</code>, which it very much should.</p>
  
  <p>I double-checked the paths, and noticed that my calls to load <code>plugin.dll</code> where using the wrong path (root, instead of root/core/bin). Either way, <code>core.dll</code> was loading correctly. After fixing that, I tried it again and this time <code>a.dll</code> does find <code>a_support.dll</code> and seems to load. However, that makes absolutely no sense, unless the loader is successfully using <code>support.dll</code> from... somewhere. The procmon log doesn't show it even attempting to load <code>support.dll</code> again, so I'm not entirely sure at this point if there actually is a problem (besides behavior from the loader that makes no sense).</p>