digitalmars.D - using d dll/lib with msvc program
- "crashtua" <crashtua gmail.com> May 13 2012
- "Froglegs" <lugtug yahoo.com> May 13 2012
- Rainer Schuetze <r.sagitario gmx.de> May 13 2012
- Rainer Schuetze <r.sagitario gmx.de> May 14 2012
- "crashtua" <crashtua gmail.com> May 13 2012
- "Froglegs" <lugtug yahoo.com> May 13 2012
- Manu <turkeyman gmail.com> May 14 2012
- "crashtua" <crashtua gmail.com> May 14 2012
- "crashtua" <crashtua gmail.com> May 14 2012
- "crashtua" <crashtua gmail.com> May 14 2012
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
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 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
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
--20cf3074b2501dac0b04bffa2f2f Content-Type: text/plain; charset=UTF-8 On 14 May 2012 09:16, Rainer Schuetze <r.sagitario gmx.de> wrote: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");
It seems GetProcAddress pulls that off for you. I thought that too, but it always workout without adding the _ for me. --20cf3074b2501dac0b04bffa2f2f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On 14 May 2012 09:16, Rainer Schuetze <span dir= =3D"ltr"><<a href=3D"mailto:r.sagitario gmx.de" target=3D"_blank">r.sagi= tario gmx.de</a>></span> wrote:<br><blockquote class=3D"gmail_quote" sty= le=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div class=3D"im"><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .= 8ex;border-left:1px #ccc solid;padding-left:1ex">Than try to load it:<br> HINSTANCE LoadME;<br> LoadME =3D LoadLibrary(L"C:\\Users\\<u></u>CrashTUA\\Documents\\visual= studio<br> 2010\\Projects\\DynamicLib1\\<u></u>DynamicLib1\\Release\\<u></u>DynamicLib= 1.dll");<br> func dllprintt;<br> dllprintt =3D (func)GetProcAddress(LoadME,"<u></u>trololo");<br> </blockquote> <br></div> The C calling convention prepends an underscore to the function name:<br> dllprintt =3D (func)GetProcAddress(LoadME,"_<u></u>trololo");</bl= ockquote><div>=C2=A0</div><div>It seems GetProcAddress pulls that off for y= ou. I thought that too, but it always workout without adding the _ for me.<= /div> </div> --20cf3074b2501dac0b04bffa2f2f--
May 14 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









Rainer Schuetze <r.sagitario gmx.de> 