Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess Violation calling imported function
    primarykey
    data
    text
    <p>I've got a function imported from a DLL. I control the source of both the host executable and the dynamic library. Now, in DLLMain then I used MessageBox to pop up the address of the function I'm exporting, and compared it using a breakpoint to the function pointer returned by GetProcAddress, and they're identical. </p> <p>However, when I try to call the function, I get an access violation. The function in question just returns NULL and has no logic, so it can't be thrown by the function specifically. </p> <p>How can calling a known valid function pointer, with the correct signature, and verified safe logic, yield an access violation?</p> <p>Edit: Information gained through another separate question about why the debugger is dying in this situation suggests that my stack is being smashed too? That would make more sense than an AV, but the function pointer and the function are completely compatible and the address is correct.</p> <pre><code>extern "C" Render* __cdecl CreateRender(WindowsOS* ptr) { return nullptr; } typedef Render*(__cdecl *RendererCreateFunction)(WindowsOS*); </code></pre> <p>I used a simple, small piece of code in DLLMain to qualify that they are in fact compatible as far as the compiler is concerned.</p> <pre><code>BOOL WINAPI DllMain( __in HINSTANCE hinstDLL, __in DWORD fdwReason, __in LPVOID lpvReserved ) { RendererCreateFunction func = &amp;CreateRender; } </code></pre> <p>If they aren't compatible (they include the same header) then the compiler should throw an error and refuse to build the DLL, but it accepts this just fine.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload