digitalmars.D.learn - In-source way to call any C Library
- Adrian Iliescu (8/8) Mar 18 2011 Is there a way to call a C function without having to screw around with ...
- Jacob Carlborg (8/16) Mar 18 2011 With pragma(lib, "lib"); you can link to a library:
- Andrej Mitrovic (35/35) Mar 18 2011 For runtime linking with DLLs, you're looking for LoadLibrary,
- Jesse Phillips (3/13) Mar 18 2011 You may find the Library referenced here useful:
Is there a way to call a C function without having to screw around with the [DllImport( "..\Debug\CLibTest.dll" )] //location internal static extern int MyTestResult(); //name of function void CSUsingCLib() { int result = MyTestResult(); //use it }
Mar 18 2011
On 2011-03-18 19:54, Adrian Iliescu wrote:Is there a way to call a C function without having to screw around with the [DllImport( "..\Debug\CLibTest.dll" )] //location internal static extern int MyTestResult(); //name of function void CSUsingCLib() { int result = MyTestResult(); //use it }With pragma(lib, "lib"); you can link to a library: http://www.digitalmars.com/d/2.0/pragma.html And then using extern(C) as usual to declare the function. You can also use "dlopen" and friends on Posix and whatever the equivalent is for Windows. -- /Jacob Carlborg
Mar 18 2011
For runtime linking with DLLs, you're looking for LoadLibrary, GetProcAddress and friends. They're in core.sys.windows.windows. The static constructor is useful if you want to have C functions in module scope. Personally, I wrap C libraries in classes and hide all the loading details there. However the following is what I think you're after: module testDllLoad; import std.stdio; import std.string; import std.path : join, curdir; import core.sys.windows.windows; import std.exception; extern(C) int function() MyTestResult; static this() { string dllFileName = join(r"..\Debug\", "CLibTest.dll"); HMODULE dllModule; enforce(dllModule = LoadLibraryA(toStringz(dllFileName))); enforce(MyTestResult = GetProcAddress(dllModule, "_MyTestResult")); } void CSUsingCLib() { int result = MyTestResult(); // use it writeln(result); } void main() { CSUsingCLib(); } I've tested this with a C DLL which exports the function _MyTestResult. The DLL was located in the previous directory, under Debug, just like your example. It worked fine. Btw, in case you don't know, its very important to specify the calling convention and the /correct/ calling convention for a function. For a C DLL this is always extern(C).
Mar 18 2011
Adrian Iliescu Wrote:Is there a way to call a C function without having to screw around with the [DllImport( "..\Debug\CLibTest.dll" )] //location internal static extern int MyTestResult(); //name of function void CSUsingCLib() { int result = MyTestResult(); //use it }You may find the Library referenced here useful: http://stackoverflow.com/questions/3818229/loading-plugins-dlls-on-the-fly
Mar 18 2011