title: How do I get the HMODULE for the currently executing code?
body: <p>I have a static library that may get linked into either a <code>.exe</code> or a <code>.dll</code>.  At runtime I want one of my library functions to get the <code>HMODULE</code> for whatever thing the static library code has been linked into.</p>
  <p>I currently use the following trick (inspired from <a href="http://www.ureader.com/message/1095160.aspx" rel="nofollow noreferrer">this forum</a>):</p>
  <pre><code>const HMODULE GetCurrentModule()
  {
      MEMORY_BASIC_INFORMATION mbi = {0};
      ::VirtualQuery( GetCurrentModule, &amp;mbi, sizeof(mbi) );
  
      return reinterpret_cast&lt;HMODULE&gt;(mbi.AllocationBase);
  }
  </code></pre>
  <p>Is there a better way to do this that doesn't look so hacky?</p>
  <p><em>(Note: The purpose of this is to load some Win32 resources that I know my users will have linked in at the same time as my static library.)</em></p>