Sign in to follow this  
droz

Calling a host app function from a DLL?

Recommended Posts

droz    134
I typically program for POSIX based systems where this is very easy to do. I want to be able to pass a pointer of a class that is created in the host app. That works, but when I want to call a function I get a undefined reference to it, the exact same method works on POSIX with out any issues. From what I've read Windows resolves references when compiling, instead of at run-time like I'm used to.

This has to be possible, any links or pointers is much appreciated.

Thanks,
Jeremy

Share this post


Link to post
Share on other sites
SiCrane    11839
What I think you want to do is possible. However, it's not worth the effort. Here's the minimum code necessary to do it (give or take 20-odd characters).

The source file for the executable:

#include <iostream>

int __declspec(dllimport) dll_func(void);
int __declspec(dllexport) exe_func(void);

int exe_func(void) {
return 18;
}

int main(int, char **) {
std::cout << dll_func() << std::endl;
return 0;
}

The source file for the DLL:

int __declspec(dllexport) dll_func(void);
int __declspec(dllimport) exe_func(void);

int dll_func(void) {
return exe_func() * 2 + 6;
}

The first thing you''ll notice if you try compiling this code naively is that it won''t link. The exe will generate an unresovled symbol error looking for the dll function. And the dll will generate an unresolved symbol error looking for the exe function. Therefore, in order to link the exe, first we generate a dummy dll. Something like this would work:

int __declspec(dllexport) dll_func(void);

int dll_func(void) {
return 0;
}

Then we take the .lib file generated from compiling that dll and link to to our exe. When the exe is created it will generate a .lib file defining its exports. Then we take the real dll code and link it against the exe's .lib file. So after fiddling with boostrap dlls and weird circular library dependencies we finally get an exe/dll pair that will run.

Now there's a distinct downside here, because there is this weird circular library dependency. Whenever a new .lib file for the dll is generated the exe will want to relink itself, which causes a new .lib file for the exe to be generated. Whenever a new .lib file for the exe is generated, the dll will want to relink itself, which causes a new .lib file for the dll to be generated. This basically means everytime you run or compile your exe the dll will get relinked and vice versa, even if no actual code changes. (You usually won't go into a infinite loop because of the way project dependencies are handled, but it's still annoying.)

In the end, it's probably be just easier to pass a function pointer to the dll when you initialize.

Share this post


Link to post
Share on other sites
droz    134
I've found that this is possible to do with MinGW. My project has all core functionality inside of a shared library (dll on windows), if I link that to the plugin then this works the way I need it to. So is this something that is only supported by GCC or does MSVC also allow linking a DLL to another?

Thanks,
Jeremy

Share this post


Link to post
Share on other sites
C0D1F1ED    452
Just make the method you want to call virtual. That way its pointer will be added to the class' vtable, and you won't get an undefined reference error because it never needs to link the static address.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this