Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Message-ID: <42D5ACEE.FB48C704@dessent.net> Date: Wed, 13 Jul 2005 17:08:14 -0700 From: Brian Dessent MIME-Version: 1.0 To: cygwin AT cygwin DOT com Subject: Re: How to create a .dll which contains a function defined in another program? References: <200507131535 DOT j6DFZqwi005679 AT diometes DOT ucdavis DOT edu> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam-Report: -5.9/5.0 ---- Start SpamAssassin results * -3.3 ALL_TRUSTED Did not pass through any untrusted hosts * -2.6 BAYES_00 BODY: Bayesian spam probability is 0 to 1% * [score: 0.0000] * 0.0 AWL AWL: From: address is in the auto white-list ---- End SpamAssassin results X-IsSubscribed: yes Reply-To: cygwin AT cygwin DOT com Yu-Cheng Chou wrote: > bar() is in the main.c which is compiled using VC++. > bar() is called inside module.c which is compiled as module.dll using > cygwin's gcc. > module.dll is loaded by LoadLibrary() in main.exe. There are two ways to do this: 1. Create an import library for the .exe file, and link against that during the link of module.dll. With gcc, you just add "-Wl,--output-implib,main.a" to the link command of main.exe. I have no idea how you do this with VC++, so you're on your own there. Note that this is rather brittle, because it hard codes the name of the .exe into the DLL, so that if the .exe is renamed the DLL will no longer work. 2. Use GetProcAddress() in module.c to get the address of bar() and then call it. Note that the HMODULE here is the handle of the .exe, not the dll. You should be able to get this with GetModuleHandle(NULL) I think. The downside here is that if you have more than a couple of imports from the .exe that you want to use, it will be a pain setting up function pointers and calling GetProcAddress for each one. Both of these solutions are not that great. For this reason it's usually considered bad design under windows to have DLLs that import from their parent .exe files. To get around this, the normal method is to isolate the desired functionality (in this case bar()) and put it in its own bar.dll, then main.exe and module.dll both import bar() from bar.dll. Another alternative is by using callbacks. For example, when you call a function in module.dll from main.exe, you pass as a parameter the address of bar() in main.exe. Brian -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/