digitalmars.D.learn - Mixing D and C - Windows
- DNewbie (7/7) Dec 31 2011 C program loads D dll
- Jakob Ovrum (23/27) Dec 31 2011 Both are possible.
- Exec (3/6) Dec 31 2011 Alternatively, you can load the DLL via the Windows API.
- DNewbie (7/14) Jan 02 2012 Thank you both.
- Mike Parker (7/10) Jan 02 2012 That's because the object file formats used by DMC and MSVC are
- Mike Parker (3/20) Jan 02 2012 Or did you do that already? I just realized that would likely give a
- Andrej Mitrovic (2/2) Jan 02 2012 Maybe I'm wrong, but IIRC objconv won't work on import libs.
- DNewbie (6/10) Jan 02 2012 Resolved.
C program loads D dll or D program loads C dll Is it possible? -- D
Dec 31 2011
On Saturday, 31 December 2011 at 19:05:44 UTC, DNewbie wrote:C program loads D dll or D program loads C dll Is it possible?Both are possible. Here's how to create a DLL in D, usable from C: http://dlang.org/dll.html As for loading C symbols in a DLL from a D program, you must link against an import library of the DLL, and create an interface module (similar to a header file) with the C declarations. For example, if this is your C DLL: ----------- #include <stdio.h> __declspec(dllexport) void sayHello(const char* name) { printf("hello, %s!", name); } ----------- Then your D interface module should look like this: ----------- extern(C) void sayHello(const char* name); ----------- The import library must be in the OMF format. The easiest way to get such an import library is to use the `impllib` tool which can be downloaded from here: http://ftp.digitalmars.com/bup.zip
Dec 31 2011
Jakob Ovrum Wrote:As for loading C symbols in a DLL from a D program, you must link against an import library of the DLL, and create an interface module (similar to a header file) with the C declarations.Alternatively, you can load the DLL via the Windows API. http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175%28v=vs.85%29.aspx
Dec 31 2011
Thank you both. I've created a D DLL [http://dlang.org/dll.html], then I've loaded it from a C program [compiled with dmc]. However, I'd want to be able to call it from a C program compiled with MSVC, and I got a link error - unresolved external symbol [link testdll.obj /implib:mydll.lib /out:testdll-msvc.exe]. The LoadLibrary works with both DMC/MSVC, but it isn't 'handy'.?. On Sat, Dec 31, 2011, at 07:20 PM, Exec wrote:Jakob Ovrum Wrote:-- DAs for loading C symbols in a DLL from a D program, you must link against an import library of the DLL, and create an interface module (similar to a header file) with the C declarations.Alternatively, you can load the DLL via the Windows API. http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175%28v=vs.85%29.aspx
Jan 02 2012
On 1/3/2012 10:02 AM, DNewbie wrote:Thank you both. I've created a D DLL [http://dlang.org/dll.html], then I've loaded it from a C program [compiled with dmc]. However, I'd want to be able to call it from a C program compiled with MSVC, and I got a link error - unresolved external symbol [link testdll.obj /implib:mydll.lib /out:testdll-msvc.exe]. The LoadLibrary works with both DMC/MSVC, but it isn't 'handy'.?.That's because the object file formats used by DMC and MSVC are different. DMC outputs OMF, MSCV uses COFF. If you download objconv[1], you can use it to convert between the two formats. Something like objconv -fcoff -nu mydll.lib mydll_mscv.lib should do the trick. [1] http://www.agner.org/optimize/objconv.zip
Jan 02 2012
On 1/3/2012 12:43 PM, Mike Parker wrote:On 1/3/2012 10:02 AM, DNewbie wrote:Or did you do that already? I just realized that would likely give a corrupt file error, not an unresolved symbol.Thank you both. I've created a D DLL [http://dlang.org/dll.html], then I've loaded it from a C program [compiled with dmc]. However, I'd want to be able to call it from a C program compiled with MSVC, and I got a link error - unresolved external symbol [link testdll.obj /implib:mydll.lib /out:testdll-msvc.exe]. The LoadLibrary works with both DMC/MSVC, but it isn't 'handy'.?.That's because the object file formats used by DMC and MSVC are different. DMC outputs OMF, MSCV uses COFF. If you download objconv[1], you can use it to convert between the two formats. Something like objconv -fcoff -nu mydll.lib mydll_mscv.lib should do the trick. [1] http://www.agner.org/optimize/objconv.zip
Jan 02 2012
Maybe I'm wrong, but IIRC objconv won't work on import libs. But there are other ways to do it: http://support.microsoft.com/kb/131313
Jan 02 2012
Resolved. Thanks everyone. On Tue, Jan 3, 2012, at 04:49 AM, Andrej Mitrovic wrote:Maybe I'm wrong, but IIRC objconv won't work on import libs. But there are other ways to do it: http://support.microsoft.com/kb/131313-- D
Jan 02 2012