Archived

This topic is now archived and is closed to further replies.

Importing functions from host program to DLL

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I am wondering if it''s possible to call a function in the host application from the DLL. For example, let''s say I have a function in the host program called "SetMarker()" I want to call that function from the DLL. Is that possible? If so, how can I do it? Thanks.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can do a GetProcAddress(GetModuleHandle(NULL),"SetMarker" to get an EXE exported function from the DLL. This assumes the exported function will always come from an EXE. If it''s one function, then that''s easy. For an entire API, you could package it as a structure of function pointers that the DLL can extract.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
This assumes the exported function will always come from an EXE.



It assumes that the exported function always comes from the exe that loads the dll . If the exe that loads the dll doesn''t also export this function, the acquired pointer will be null. This limits the modularity that dlls provide, but could still be useful in other ways.

But as mputters points out, it''s a lot easier to pass a function pointer to the dll. Remember, once loaded, a dll resides in the address space of the process - along with all the functions, global variables, the heap, the stack and all the other parts of the process.

Share this post


Link to post
Share on other sites
Passing the function pointer to the DLL function is the best way to do.

That''s basically how connection points work for events in COM.


Oooh, you found the horadric nuke!
Did you know, that by using a horadric nuke, you can blow up Diablo and solve all our problems!

Share this post


Link to post
Share on other sites