I'd like to write C bindings for InpOut32 library.
First of all I downloaded binaries for this library.
There are three files in it:
Here's what i've tried to get D to use this lib.
//io.d
extern(C)
{
void Out32(short PortAddress, short data);
short Inp32(short PortAddress);
}
// test.d
import std.stdio;
import io;
// parallel port address
short port = 0x0378;
void main()
{
/* data */
short data = 0b_00000000;
Out32(port, data);
}
Compile: dmd -c test.d io.d Result: successfull
Link: link test.obj io.obj inpout32.lib
But when I try to link I get this linker error:
OPTLINK (R) for Win32 Release 8.00.12 Copyright (C) Digital Mars
1989-2010 All rights reserved.
http://www.digitalmars.com/ctg/optlink.html inpout32.lib Offset 00000H
Record Type 0021 Error 138: Module or Dictionary corrupt
The library file probably is in the COFF format. I think coffimplib
tool is not free, so I used Borland's coff2omf
tool to OMF format.After converting I still get linker errors like this:
OPTLINK (R) for Win32 Release 8.00.12
Copyright (C) Digital Mars 1989-2010 All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
test.obj(test) Error 42: Symbol Undefined _Out32
Any ideas how to use this lib? Thanks..
Update: Today I read an interesting article about creating bindings to C libraries for the D programming language.
Now test.exe works as expected. So far I've done these steps.
Compile : dmd -c -g test.d io.d
Thanks Ali Çehreli for mentioning -g option. After adding -g flag access violation errors
disappeared.
Generate OMF import library: implib -a inpout32.lib inpout32.dll
Link: link test.obj io.obj inpout32.lib
After this I'd like to try manually loading DLL's. Thanks for taking the time to answer, everyone!
You may need to fiddle with coff2omf or similar tools' flags regarding the leading _ characters (check the lib file to make sure symbols include them).
Alternatively, you may find it easier to generate an OMF import library directly from the .dll, using an implib utility.
Finally, if you only need to use a small number of functions, loading the DLL dynamically using LoadLibrary
and getting the functions' addresses using GetProcAddress
will avoid all hassle with .lib files.