From: Lloyd D. <ll...@no...> - 2005-05-24 23:10:51
|
I don't understand, why don't you simply do something like that? Works well wether you use DLL or not... #-- pseudo code below --------- typedef void (*CALLBACK)(...); static CALLBACK feeback = NULL; DLL_EXPORT void SetCallBack(CALLBACK calback) { feedback = cakkback; } DLL_EXPORT call_me_back(param_list_t plt) { // ...... if(feedback != null) feedback(params); } #--------------------------------- ----- Original Message ----- From: "zhong wang" <zho...@gm...> To: <min...@li...> Sent: Wednesday, May 25, 2005 8:57 AM Subject: Re: [Mingw-users] Can a DLL call the function built in main program? Thanks, Brian Actually I have little understanding on Windows DLL. I am porting a plugin system from Linux to Netware and Windows. On Linux there is a simple option -rdynamic with gcc can make this work. On netware, just simply add the export list and import list while compiling to make it works. I googled a lot about MinGW DLL, and tried to figure out if Windows can implement in a similar way. In you example, the name of .exe will never be changed. Is it possible to get a flexible solution with .def file or some other ways else? My code should be like: $ cat >main.c <<EOF #include <windows.h> #include <stdio.h> void call_func_in_main() { printf("Get function in main program\n"); return; } int main () { typedef void (*pfunc)(); HANDLE hdll; pfunc hello; hdll = LoadLibrary("tst.dll"); hello = (pfunc)GetProcAddress(hdll, "hello"); hello(); return 0; } <<EOF $ cat >tst.c <<EOF #include <stdio.h> #include <windows.h> extern void call_func_in_main(); DECLSPEC_EXPORT void hello(void) { call_func_in_main(); printf ("Hello\n"); } <<EOF On 5/24/05, Brian Dessent <br...@de...> wrote: > zhong wang wrote: > > > > Thanks for response. > > > > I am talking about doing LoadLibrary and GetProcAddress by hand. I am > > working on a "real" dynamic library. Any other advices? > > If that's what you're really doing then why did all the examples you > posted in your message just try to do regular dynamic linking? I don't > understand why you would bother bringing up any of that if you are > indeed using LoadLibrary and GetProcAddress. Just to be clear, you > don't have to go down the road of LoadLibrary if you don't want to, your > DLL can in fact call functions in the main exe, with the caveat that it > adds the requirement that the name of the .exe never change. > > $ cat >getprocaddr_dll.c <<EOF > #include <windows.h> > #include <stdio.h> > > DECLSPEC_EXPORT int i_am_a_dll_function (int x) > { > int (*i_am_a_main_function) (int); > HINSTANCE hm; > > if ((hm = LoadLibrary ("getprocaddr_main.exe")) == NULL) > perror ("LoadLibrary dll"); > > if ((i_am_a_main_function = GetProcAddress (hm, > "i_am_a_main_function")) == NULL) > perror ("GetProcAddress dll"); > > return i_am_a_main_function (x * 10); > } > EOF > > $ cat >getprocaddr_main.c <<EOF > #include <windows.h> > #include <stdio.h> > > DECLSPEC_EXPORT int i_am_a_main_function (int x) > { > return x * 50; > } > > int main (void) > { > int (*i_am_a_dll_function) (int); > HINSTANCE hm; > > if ((hm = LoadLibrary ("getprocaddr_dll.dll")) == NULL) > perror ("LoadLibrary main"); > > if ((i_am_a_dll_function = GetProcAddress (hm, "i_am_a_dll_function")) > == NULL) > perror ("GetProcAddress main"); > > printf ("%d\n", i_am_a_dll_function (42)); > } > <<EOF > > $ gcc -shared getprocaddr_dll.c -o getprocaddr_dll.dll > $ gcc getprocaddr_main.c -o getprocaddr_main > $ ./getprocaddr_main > 21000 > > > ------------------------------------------------------- > This SF.Net email is sponsored by Yahoo. > Introducing Yahoo! Search Developer Network - Create apps using Yahoo! > Search APIs Find out how you can build Yahoo! directly into your own > Applications - visit http://developer.yahoo.net/?fr=offad-ysdn-ostg-q22005 > _______________________________________________ > MinGW-users mailing list > Min...@li... > > You may change your MinGW Account Options or unsubscribe at: > https://lists.sourceforge.net/lists/listinfo/mingw-users > ------------------------------------------------------- This SF.Net email is sponsored by Yahoo. Introducing Yahoo! Search Developer Network - Create apps using Yahoo! Search APIs Find out how you can build Yahoo! directly into your own Applications - visit http://developer.yahoo.net/?fr=fad-ysdn-ostg-q22005 _______________________________________________ MinGW-users mailing list Min...@li... You may change your MinGW Account Options or unsubscribe at: https://lists.sourceforge.net/lists/listinfo/mingw-users |