title: Get current Image Base address
body: <p>I made some computations to get a relative virtual address(RVA).</p>
  
  <p>I compute a correct RVA (according to the <code>.map</code> file) and I want to translate it to a callable function pointer.</p>
  
  <p>Is there a way to translate it to a physical address?</p>
  
  <p>I have thought of taking the image base address and add it. According to <a href="https://stackoverflow.com/questions/4298331/exe-or-dll-image-base-address">this thread</a> it should be possible via <code>GetModuleHandle(NULL)</code>, but the result is "wrong". I only have a good result when I subtract a pointer from a function from its RVA defined in the <code>.map</code> file.</p>
  
  <p>Is there a WINAPI to either convert a RVA to a physical address, or get the image base address, or get the RVA of a physical address?</p>
  
  <p>Here's some example code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;WinNT.h&gt;
  
  static int count = 0;
  void reference()       // According to .map RVA = 0x00401000
  {
  }
  void toCall()          // According to .map RVA = 0x00401030
  {
      printf("Called %d\n", ++count);
  }
  
  int main()
  {
      typedef void (*fnct_t)();
      fnct_t fnct;
      fnct = (fnct_t) (0x00401030 + (((int) reference) - 0x00401000));
      fnct(); // works
      fnct = (fnct_t) (0x00401030 + ((int) GetModuleHandle(NULL)) - 0x00400000);
      fnct(); // often works
      return 0;
  }
  </code></pre>
  
  <p>My main concern is that it seems that sometimes (maybe in threaded contexts) <code>GetModuleHandle(NULL)</code> isn't correct.</p>