title: Simulating LoadLibraryEx's LOAD_LIBRARY_SEARCH_SYSTEM32 functionality
body: <p>Often one wants to <code>LoadLibrary</code> a system DLL like <code>Kernel32.dll</code>, handily <code>LoadLibraryEx</code> has a <code>dwFlags</code> parameter that allows you to specify <code>LOAD_LIBRARY_SEARCH_SYSTEM32</code>.</p>
  
  <p>For security reasons, this can be very important as malicious attackers can subvert the default search paths that <code>LoadLibrary</code> looks through.</p>
  
  <p>However Windows XP, which I must still support, cannot use <code>LOAD_LIBRARY_SEARCH_SYSTEM32</code> as it didn't exist eons ago.  This leads me to the following conclusion: I need to come up with a fully qualified path and pass that to <code>LoadLibrary</code>.</p>
  
  <p>However, a robust implementation of this is tricky due to WoW64 and file-system redirection.
  I have to worry about this possibility because the code in question lives in a library and can be called in strange environments.</p>
  
  <p>My current thoughts are something like the following pseudo-code:</p>
  
  <pre><code>string Path;
  BOOL Wow64Process;
  if (IsWow64Process(CurrentProcess(), &amp;Wow64Process) &amp;&amp; Wow64Process &amp;&amp; 
      WoW64FSRedirectionIsDisabled)
      Path = GetSystemWow64Directory();
  else
      Path = GetSystemDirectory();
  Path += "\Kernel32.dll";
  LoadLibrary(Path);
  </code></pre>
  
  <p>I have considered, and rejected using <code>Wow64DisableWow64FsRedirection</code> and <code>Wow64RevertWow64FsRedirection</code> but this is problematic as it will leave FS redirection disabled in the library's <code>DLLMain</code>.</p>
  
  <p>Given the above, what's the best way to go about what I am trying to do?</p>
  
  <p>Thanks!</p>