Plugins on win32

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Plugins on win32

Thomas Martitz
Hello,

my name is Thomas and I'm a computer scientist from Berlin, working in
the embedded industry. In my spare time I work in the Geany IDE and Rockbox.

I'm struggling to make plugins on win32 work (for Geany), using dll
import/export mechanism. The following two files display my case:

--- main.c i586-mingw32msvc-gcc main.c -o main
#include <windows.h>
#include <stdio.h>

__declspec(dllexport)
void foo(const char *s)
{
     printf("%s\n", s);
}

int main()
{
     void *p = LoadLibrary("plugin.dll");

     void (*fn)() = (void *)GetProcAddress(p, "plugin_func");

     fn();

     return 0;
}

--- plugin.c i586-mingw32msvc-gcc -shared -Wl,--dll
-Wl,--allow-shlib-undefined -Wl,--enable-auto-import plugin.c -o plugin.dll
#include <stdio.h>

__declspec(dllimport)
void foo(const char *s);

void plugin_func()
{
     foo("plugin");
}

When compiling the plugin I get:
/tmp/ccncPmiG.o:plugin.c:(.text+0xf): undefined reference to `__imp__foo'
collect2: ld returned 1 exit status

I assume this is the expected behavior. So now my question is how to
make this work? On linux it "just works" because shared libraries can
have undefined symbols, which are resolved to the main binary symbols
when the library is loaded. For Win32, I cannot make the linker succeed,
even with --enable-auto-import or --allow-shlib-undefined.

Can you please give me advice on how to resolve my issue? Many thanks in
advance

Best regards

------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Plugins on win32

Michael Gerdau
Hi Thomas !

[concise problem desription skipped]

> When compiling the plugin I get:
> /tmp/ccncPmiG.o:plugin.c:(.text+0xf): undefined reference to `__imp__foo'
> collect2: ld returned 1 exit status
>
> I assume this is the expected behavior.

I agree with that assumption :)

> So now my question is how to
> make this work? On linux it "just works" because shared libraries can
> have undefined symbols, which are resolved to the main binary symbols
> when the library is loaded. For Win32, I cannot make the linker succeed,
> even with --enable-auto-import or --allow-shlib-undefined.
>
> Can you please give me advice on how to resolve my issue? Many thanks in
> advance

My suggestion is to use function pointers, i.e. declare a variable that's
a pointer to a function. Use that pointer to call foo instead of calling
foo directly.

Your function pointer should then be initialized by your DLL InitRoutine.
That's a function that is executed by Windows upon loading the DLL. There
you retrieve the actual addr of foo, initialize the function pointer
accordingly and all should work.

I have done things like that ages ago and am writing this from memory. If
the above pointer, especially the phrase "InitTerm" or "_DLL_InitTerm" does
not help you find proper documentation I'll elaborate a bit more at some
later time. For now I've got to catch a train.

HTH,
Michael
--
 Michael Gerdau       email: [hidden email]
 GPG-keys available on request or at public keyserver

------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Plugins on win32

John Brown
In reply to this post by Thomas Martitz

Hello Thomas,

As Michael Gerdau suggested, you can load foo() just as you load plugin_func().

typedef void (*FOO_FUNCTION_TYPE)(const char *s);

__declspec(dllexport)
void plugin_func()
{
  // Get handle to calling .exe
  HANDLE hMain = GetModuleHandle(NULL);
  FOO_FUNCTION_TYPE fn= (FOO_FUNCTION_TYPE)GetProcAddress(hMain, "foo");
  fn("plugin");
}

You may want to call GetModuleHandleEx instead of GetModuleHandle.

Regards,
John Brown.

     
------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Plugins on win32

Thomas Martitz
Am 27.08.2014 um 11:05 schrieb John Brown:

> Hello Thomas,
>
> As Michael Gerdau suggested, you can load foo() just as you load plugin_func().
>
> typedef void (*FOO_FUNCTION_TYPE)(const char *s);
>
> __declspec(dllexport)
> void plugin_func()
> {
>    // Get handle to calling .exe
>    HANDLE hMain = GetModuleHandle(NULL);
>    FOO_FUNCTION_TYPE fn= (FOO_FUNCTION_TYPE)GetProcAddress(hMain, "foo");
>    fn("plugin");
> }
>
> You may want to call GetModuleHandleEx instead of GetModuleHandle.
>

I already solved it with the help of Gisle Vanem.

Besides, I specifically wanted to avoid anything that is manual like that.

Best regards

------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Plugins on win32

John Brown
On Wed, 27 Aug 2014 12:12:39 +0200, Thomas Martitz wrote:
>
> I already solved it with the help of Gisle Vanem.
>
> Besides, I specifically wanted to avoid anything that is manual like that.
>

Hello Thomas,

Could you post your solution? 

Regards,
John Brown.    
------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Plugins on win32

Thomas Martitz
Am 27.08.2014 um 14:07 schrieb John Brown:
> On Wed, 27 Aug 2014 12:12:39 +0200, Thomas Martitz wrote:
>> I already solved it with the help of Gisle Vanem.
>>
>> Besides, I specifically wanted to avoid anything that is manual like that.
>>
> Hello Thomas,
>
> Could you post your solution?
>


Oh, Gisle Vanem replied in private apparently. Here's his solution that
works for me as well:

> You need this to generate an import-library for plugin.dll to
> use. Untested:
>   i586-mingw32msvc-gcc main.c -o main.exe -Wl,--out-implib,main.dll.a
>
> And then when you link plugin.dll:
>
>  i586-mingw32msvc-gcc -shared -Wl,--dll plugin.c -o plugin.dll main.dll.a
>
> I think the '--allow-shlib-undefined' does nothing for PE-files.
>
> PS. what package has a 'i586-mingw32msvc-gcc.exe' compiler?
>
> --gv


------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Plugins on win32

John Brown
On Wed, 27 Aug 2014 14:11:30 +0200, Thomas Martitz wrote:

>> PS. what package has a 'i586-mingw32msvc-gcc.exe' compiler?
>>

The Ubuntu Linux MinGW cross-compiler.

Regards,
John Brown.

     
------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe