Using Functions In a DLL

I have a function that is loaded via the LoadLibrary function and I am woundering if there is a way to use a function defined in the program, in a dll.

So imagine I have a function, created and used in program.exe, and program.exe loads plugin.dll. I have a function, loadblock(std::string name) that is defined in program.exe that I want used in plugin.dll. Is there any way to do it using windows APIs
Yes.

Method 1

You export the function from the main application and then use GetProcAddress with the handle return from GetModuleHandle(NULL) and the required function name or ordinal in DllMain in response to DLL_PROCESS_ATTACH (and clean up during DLL_PROCESS_DETACH.)

You will probably need to deal with name mangling issues (by using extern "C" when you declare your function, and maybe resort to a def file?)

Method 2 (which I prefer)

You provide your plugin with some kind of init function which allows you to hook it up on load (and a corresponding terminate function to unhook.)

This is the approach used by NPAPI (the Netscape Plugin API) -- they have NP_Initialize and NP_Shutdown.

Among other things, I find this second approach works better for testing (e.g. with a plugin harness.)

I trust you are aware that whichever approach you take, you need to be careful with calling convention. And that, as you are using a function with a std::string parameter, you should be careful to avoid mixing a debug DLL and release app, or vice versa.

Andy
Last edited on
There's actually an article on this website covering "Making a Plugin System" which seems to be what you are talking about:
http://www.cplusplus.com/articles/48TbqMoL/
Topic archived. No new replies allowed.