Dynamic loading - using symbols from exe in dll

Hello,
I am porting program to Windows. It consists of two parts: program and plugin.
Program dynamically loads plugin using dlopen+dlsym, or LoadLibrary+GetProcAddress.

Problem is that plugin needs to use a few symbols from program.

On Linux, I can do that:
$ cc -rdynamic program.c -o program
$ cc -fPIC plugin.c -o plugin.o
$ cc -shared plugin.o -o plugin.so
Everything works fine. But if I tried to compile it for Windows:
$ mingw-cc program.c -o program
$ mingw-cc plugin.c -o plugin.o
$ mingw-cc -shared plugin.o -o plugin.dll
I get 'undefined reference' error for all symbols from program.c. What can I do with it?

Thank you.
Last edited on
You cannot use symbols from exe inside a dll, only symbols from dll in your executable program.
Why? If I perform

$ mingw-nm program.exe

I will see my-defined symbols. So using symbols from exe inside a dll may be possible, or I'm wrong?
Last edited on
You could use LoadLibrary to (re)load the exe from inside the dll, and then use GetProcAddress to get a pointer to an exported callback function.

But this is not the standard way. Usually an exe would load a plug-in and then give it a pointer to call back on. That way the exe is in full control.

Andy

P.S. What symbols are you referring to?

Last edited on
Andy, thank you, but..
I was used second way. I have something like that:
header.h:
typedef struct {
int (*first_function)(int x, float *z);
.
.
void (*last)(void *data);
} callbacks_t;

but using functions in this style:
plugin.c
int plugin_init( callbacks_t *f) {
f->last(&something)
}
wasn't ideal. First way like that:

plugin.c:
int (*first_function)(int x, float *z);
.
.
void (*last)(void *data);

void init()
{
first_function=GetProcAddr(handler, "first_func")
.
.
last_function=GetProcAddr(handler, "last_func")
}
looks nasty.


PS: I am referring the my-defined functions, which forms the interface.
I prefer the struct of function pointer approach to the other.

But I would prob use the C++ equivalent: a vtable-only interface class.
There is another solution:

plugin:
int (*f1)(int x);

...
y=f1(5);

program:

int f1(int x){return 2*x;}
..
void *hndl=LoadLibrary(plugin);
void **overwrite=GetProcAddress(hndl, "f1");
*overwrite=f1;

Thank you. :-)
Topic archived. No new replies allowed.