title: How to call a function located in an executable from a loaded DLL?
body: <p>I have located a function inside an executable which I'd like to call <em>from my DLL</em>. The address of it would be <code>0x0090DE00</code> according to OllyDbg. I've tried to call it directly:</p>
  
  <pre><code>luaL__openlib *f = ((luaL__openlib*)(module_handle  + 0x0090DE00));
  </code></pre>
  
  <p>but also with adding the base of the module handle to it as suggested <a href="https://stackoverflow.com/questions/31237323/call-function-from-executable">here</a>:</p>
  
  <pre><code>uint8_t * module_handle = (uint8_t *)GetModuleHandle(L"ForgedAlliance1.exe");
  
  luaL__openlib *f = ((luaL__openlib*)(module_handle  + 0x0090DE00));
  </code></pre>
  
  <p>It appears that this is not working as I get access violation exceptions - it appears that the pointer is not valid.</p>
  
  <p>So: How can I call this function by using its address?</p>
  
  <hr>
  
  <p>I just inserted a simple <code>RET</code> instruction at <code>0x00C0B530</code>. My code does now look as follows:</p>
  
  <pre><code>typedef void (*test) ();
  
  EXTERN_DLL_EXPORT void initialize(lua_State *L)
  {
      // Adding this should not be necessary. I get 0x00C0B530 from 
      // OllyDbg where the offset 0x00401000 is included
      uint8_t * module_handle = (uint8_t *)GetModuleHandle(L"ForgedAlliance1.exe");
  
      test *f = NULL;
  
      f = ((test*)(0x00C0B530));
  
      (*f)(); // Crashing 
  }
  </code></pre>
  
  <p>What I don't quite understand is why I get a different address in the exception message:</p>
  
  <blockquote>
    <p>Exception thrown at <code>0x909090C3</code> in ForgedAlliance1.exe: <code>0xC0000005</code>: Access violation executing location <code>0x909090C3</code>.</p>
  </blockquote>
  
  <hr>
  
  <p><strong>UPDATE:</strong> I just realized that <code>0x909090C3</code> is not just a pointer here, it is the code itself</p>
  
  <pre><code>90 | NOP
  90 | NOP
  90 | NOP
  C3 | RETN
  </code></pre>
  
  <p>Seems I am messing something up with pointers. Why does it try to execute "location" <code>0x909090C3</code>. That's not the location.</p>