Re: In-source way to call any C Library
Adrian Iliescu Wrote: > Is there a way to call a C function without having to screw around with the > linker on the command line? In C#, for example, this is all you have to do: > > [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
Re: In-source way to call any C Library
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).
Re: In-source way to call any C Library
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 linker on the command line? In C#, for example, this is all you have to do: [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
In-source way to call any C Library
Is there a way to call a C function without having to screw around with the linker on the command line? In C#, for example, this is all you have to do: [DllImport( @"..\Debug\CLibTest.dll" )]//location internal static extern int MyTestResult(); //name of function void CSUsingCLib() { int result = MyTestResult(); //use it }