How do I interface D to a C library based on a DLL?

torhu fake at address.dude
Tue Jun 12 21:09:56 PDT 2007


Christopher Grantham wrote:
> Hi,
>      I'm very new to D, I have worked with other typical languages 
> previously (C, C++, Java etc) and have some familiarity with .NET 
> (mainly Visual C++), I need to use a C API in the form of a DLL for my 
> uni work (research) and would like to try some experiments in D (I hate 
> VC++ - IJW & M$ interop be damned!), I have the interface (.h), .LIB and 
> .OBJ files as well as the DLL itself, but I'm not sure how to go about 
> using/Linking the API in D, I've used it in VS.NET (VC++) but want to 
> get away from it and use D if possible.
> The DLL is closed source/proprietary so I can only edit the interface; 
> I've read the 'Converting C .h files to D Modules' 
> (http://www.digitalmars.com/d/htomodule.html), 'Writing Win32 DLLs in 
> D'(http://www.digitalmars.com/d/dll.html), 'Converting C .h Files to D 
> Modules' (http://www.digitalmars.com/d/htomodule.html), and 'Interfacing 
> to C' (http://www.digitalmars.com/d/interfaceToC.html), however it isn't 
> clear from these articles (at least to a n00b like myself) how I can use 
> this C interfaced DLL in D code.
> So far I've tried converting the header to D (manually), which hasn't 
> worked (I get error: 138 data corruption), I think I should be able to 
> interface directly to the C code, but am not confident.
> If someone could assist/explain how to get D code working with a C 
> interface from a DLL, then I'd be most grateful.

It would be nice to know what you did when you got that error.  But in 
case you got it when trying to link with a lib file meant for a 
different linker, I'll explain how to get one that works with dmd.

Download:
ftp://ftp.digitalmars.com/bup.zip

In that zip, you'll find implib.exe.  It creates dmd-compatible import 
libs, based on DLL files.

implib /s thelib.lib thedll.dll

'thedll.lib' is the output, so move the lib file you've got somewhere 
else first.  Try linking with that file, if that's what you were doing.


More information about the Digitalmars-d-learn mailing list