Re: Exporting function from EXE problem
German Koninin wrote:
Good afternoon.
My problem is that I'm trying to export a function from my EXE. LoadLibrary
and GetProcAddress wrks fine, but when I call function I'm getting access
violation error. I mean when function is empty or I have some simple code
there like int q = 0; it works fine, but when I initialize a class or simple
call another function I'm getting access violation. It looks like exe is not
loaded correctly in to caller's address space or something like that. Could
you please help me to sort it out. The point is that I need exe with
exported function and need to be able to call that function from other exe
or dll by dynamic linking.
I think that normally DllMain initializes the runtime library and global
objects. If you don't have a dll entry point, and the exe hasn't already
been loaded (e.g. it is not the process's exe), then I think you won't
be able to use the CRT or to access global non-POD objects, which
obviously seriously limits things. Linking to the DLL version of the CRT
might help matters here, since the loading DLL or exe should already
have initialized the CRT (if it also links to the same DLL version).
However, if your exe has some exported functions, it has loaded a DLL
and that DLL wants to call back into the exe via an exported function,
that should work fine as far as I know, and you don't need to call
LoadLibrary, just GetModuleHandle.
Another problem is that exes aren't normally compiled to have
"relocatable" code. If the exe isn't the main process exe, it isn't
going to be loaded at the address it normally is - in the linker
advanced settings, try setting fixed base address to /FIXED:NO.
Make sure calling conventions match (cdecl vs stdcall).
I've never done this though, so my advice might not work. Overall, I
think it may be easier to split the exe in question into an exe and a dll...
Tom