# Calling DLL export. Access error

This topic is 3494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello ! I've got a strange error and have no idea how to move on. I'm getting a function address as follows : FunctionFunc _FunctionFunc; HINSTANCE hInstLibrary = LoadLibraryEx(_T"C:\\My.dll"),0,DONT_RESOLVE_DLL_REFERENCES); if (hInstLibrary){ _FunctionFunc = (FunctionFunc)GetProcAddress(hInstLibrary,"RTGHit"); if (_FunctionFunc){ _FunctionFunc(); } FreeLibrary(hInstLibrary); } else{ DWORD reply = GetLastError(); } and this works fine and retrieves the correct function address. The code jumps to the function in the dll correctly (monitored in debug), but when actioning the first instruction it fails with an read access violation. The statement is just an outputdebugstring. why is this ?

##### Share on other sites
What happens if you just use LoadLibrary() instead of LoadLibraryEx()? And are you sure that your FunctionFunc function pointer is the same type as the function being exported, particularly with respect to calling conventions (I.e. are they both __stdcall or both __cdecl)?

EDIT: DONT_RESOLVE_DLL_REFERENCES looks suspicious. That tells the OS not to load any other DLLs that DLL needs - which you almost certainly don't want to do.

##### Share on other sites
Yeah I'm with Evil Steve, my money is on this being a calling convention mismatch. Either that, or the signature used for "FunctionFunc" is otherwise different from the signature of the function exported by the DLL. If you don't mind, could post both of those here? (using the code tags, please)

##### Share on other sites

Yes, I started with LoadLibrary then when it didn't work i've been playing around with other things hence the LoadLibraryEx !

The exported function in the dll is shown below and the line below it shows the typdef of the calling function

extern "C" __declspec(dllexport) void RTGHit();typedef void (*FunctionFunc)();

[Edited by - Oobydoobyscoobydo on July 31, 2008 10:27:25 AM]

##### Share on other sites
woohoo fixed it !

thanks for your help guys - as you advised calling convention was wrong
corrected to

extern "C" __declspec(dllexport) void *RTGHit();

and now works a treat. THANKS