digitalmars.D - using d dll/lib with msvc program
- crashtua (2/2) May 13 2012 Is it possible to build dll or lib with dmc to use it in program
- Froglegs (5/7) May 13 2012 Yes you can download Visual D, build a D DLL with DMD, and call
- crashtua (38/38) May 13 2012 And how to load that dll? I have dll:
- Froglegs (1/10) May 13 2012 Well did LoadLibrary or did GetProcAddress fail?
- crashtua (3/3) May 14 2012 LoadLibrary returns 100000(maybe wrong number of zeros,i dont
- crashtua (2/2) May 14 2012 So, thanks to all. It works after adding 'export' keyword and '_'
- crashtua (23/23) May 14 2012 But it is something strange, one function need '_' and another
- Rainer Schuetze (4/25) May 14 2012 I guess you are hitting this linker bug:
- Rainer Schuetze (5/41) May 13 2012 add an "export" to the function:
- Manu (3/12) May 14 2012 It seems GetProcAddress pulls that off for you. I thought that too, but ...
Is it possible to build dll or lib with dmc to use it in program builded in microsoft c++ compiler?
May 13 2012
On Sunday, 13 May 2012 at 19:07:41 UTC, crashtua wrote:Is it possible to build dll or lib with dmc to use it in program builded in microsoft c++ compiler?Yes you can download Visual D, build a D DLL with DMD, and call it from a Visual C++ program. Unfortunately you have to use C to communicate between them, and the D IDE experience is pretty bad. -this forum likes to reject posts with nonsense errors... btw
May 13 2012
And how to load that dll? I have dll: module dllmain; import std.c.windows.windows; import core.sys.windows.dll; __gshared HINSTANCE g_hInst; extern (Windows) BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved) { final switch (ulReason) { case DLL_PROCESS_ATTACH: g_hInst = hInstance; dll_process_attach( hInstance, true ); break; case DLL_PROCESS_DETACH: dll_process_detach( hInstance, true ); break; case DLL_THREAD_ATTACH: dll_thread_attach( true, true ); break; case DLL_THREAD_DETACH: dll_thread_detach( true, true ); break; } return true; } extern (C) int trololo(){return 10;} Than try to load it: HINSTANCE LoadME; LoadME = LoadLibrary(L"C:\\Users\\CrashTUA\\Documents\\visual studio 2010\\Projects\\DynamicLib1\\DynamicLib1\\Release\\DynamicLib1.dll"); func dllprintt; dllprintt = (func)GetProcAddress(LoadME,"trololo"); int i = dllprintt(); return 0; And it gives me acces violation...
May 13 2012
HINSTANCE LoadME; LoadME = LoadLibrary(L"C:\\Users\\CrashTUA\\Documents\\visual studio 2010\\Projects\\DynamicLib1\\DynamicLib1\\Release\\DynamicLib1.dll"); func dllprintt; dllprintt = (func)GetProcAddress(LoadME,"trololo"); int i = dllprintt(); return 0; And it gives me acces violation...Well did LoadLibrary or did GetProcAddress fail?
May 13 2012
LoadLibrary returns 100000(maybe wrong number of zeros,i dont remember exactly:)). And GetProcAddress returns 0, so dllprintt fails:(
May 14 2012
So, thanks to all. It works after adding 'export' keyword and '_' symbol before function name.
May 14 2012
But it is something strange, one function need '_' and another dont:( In D dll i have: extern (C) int function() f; alias typeof(f) myfunc; export extern (C) int retten() { return 10; } export extern (C) void setf(myfunc ff) { f=ff; } export extern (C) int ret() { return f(); } In C code: int(*retten)() = (int (*)())GetProcAddress(dLib,"_retten"); int(*ret)() = (int (*)())GetProcAddress(dLib,"ret"); void(*setf)(int(*)()) = (void (*)(int(*)()))GetProcAddress(dLib,"setf"); Why so strange?
May 14 2012
On 5/14/2012 7:59 PM, crashtua wrote:But it is something strange, one function need '_' and another dont:( In D dll i have: extern (C) int function() f; alias typeof(f) myfunc; export extern (C) int retten() { return 10; } export extern (C) void setf(myfunc ff) { f=ff; } export extern (C) int ret() { return f(); } In C code: int(*retten)() = (int (*)())GetProcAddress(dLib,"_retten"); int(*ret)() = (int (*)())GetProcAddress(dLib,"ret"); void(*setf)(int(*)()) = (void (*)(int(*)()))GetProcAddress(dLib,"setf"); Why so strange?I guess you are hitting this linker bug: http://d.puremagic.com/issues/show_bug.cgi?id=3956 A work around is to add the exports to the .def file.
May 14 2012
On 5/14/2012 5:44 AM, crashtua wrote:And how to load that dll? I have dll: module dllmain; import std.c.windows.windows; import core.sys.windows.dll; __gshared HINSTANCE g_hInst; extern (Windows) BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved) { final switch (ulReason) { case DLL_PROCESS_ATTACH: g_hInst = hInstance; dll_process_attach( hInstance, true ); break; case DLL_PROCESS_DETACH: dll_process_detach( hInstance, true ); break; case DLL_THREAD_ATTACH: dll_thread_attach( true, true ); break; case DLL_THREAD_DETACH: dll_thread_detach( true, true ); break; } return true; } extern (C) int trololo(){return 10;}add an "export" to the function: export extern (C) int trololo(){return 10;}Than try to load it: HINSTANCE LoadME; LoadME = LoadLibrary(L"C:\\Users\\CrashTUA\\Documents\\visual studio 2010\\Projects\\DynamicLib1\\DynamicLib1\\Release\\DynamicLib1.dll"); func dllprintt; dllprintt = (func)GetProcAddress(LoadME,"trololo");The C calling convention prepends an underscore to the function name: dllprintt = (func)GetProcAddress(LoadME,"_trololo");int i = dllprintt(); return 0; And it gives me acces violation...
May 13 2012
On 14 May 2012 09:16, Rainer Schuetze <r.sagitario gmx.de> wrote:Than try to load it:It seems GetProcAddress pulls that off for you. I thought that too, but it always workout without adding the _ for me.HINSTANCE LoadME; LoadME = LoadLibrary(L"C:\\Users\\**CrashTUA\\Documents\\visual studio 2010\\Projects\\DynamicLib1\\**DynamicLib1\\Release\\**DynamicLib1.dll"); func dllprintt; dllprintt = (func)GetProcAddress(LoadME,"**trololo");The C calling convention prepends an underscore to the function name: dllprintt = (func)GetProcAddress(LoadME,"_**trololo");
May 14 2012