www.digitalmars.com         C & C++   DMDScript  

D - extern functions in DLL

reply Lars Ivar Igesund <larsivar igesund.net> writes:
How do I use extern functions in a DLL?
printf is an excellent example.

I'm able to build a dll and create an export library for it.
I'm also able to link my test program against it and even
call the functions in the dll as long is it don't use any
external functions. calling printf or any other external
function from my dll leads to
Error: Access violation

What did I miss?

Lars Ivar Igesund
Feb 25 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Lars Ivar Igesund wrote:

 How do I use extern functions in a DLL?
 printf is an excellent example.
 
 I'm able to build a dll and create an export library for it.
 I'm also able to link my test program against it and even
 call the functions in the dll as long is it don't use any
 external functions. calling printf or any other external
 function from my dll leads to
 Error: Access violation
 
 What did I miss?
 
 Lars Ivar Igesund
Here's an example that works for me (as far as I can tell): http://www.digitalmars.com/drn-bin/wwwnews?D/11884 Change: import windows; to: import std.c.windows.windows; If this doesn't help, we might be able to help better if you post some code. -- Justin http://jcc_7.tripod.com/d/
Feb 25 2004
parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
J C Calvarese wrote:

 Lars Ivar Igesund wrote:
 
 How do I use extern functions in a DLL?
 printf is an excellent example.

 I'm able to build a dll and create an export library for it.
 I'm also able to link my test program against it and even
 call the functions in the dll as long is it don't use any
 external functions. calling printf or any other external
 function from my dll leads to
 Error: Access violation

 What did I miss?

 Lars Ivar Igesund
Here's an example that works for me (as far as I can tell): http://www.digitalmars.com/drn-bin/wwwnews?D/11884 Change: import windows; to: import std.c.windows.windows;
Burtons example won't link if I try to use printf. If I don't, it links but says that there is no start address. (I use export instead of a .def file.) What DOES link, is this: bar.d ---------------- import std.c.stdio; export void p() { printf("print something\n"); } ---------------- dll.d ---------------- import std.c.windows.windows; HINSTANCE g_hInst; extern (C) { void gc_init(); void gc_term(); void _minit(); void _moduleCtor(); } export: extern (Windows) BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved) { switch (ulReason) { case DLL_PROCESS_ATTACH: gc_init(); _minit(); _moduleCtor(); break; case DLL_PROCESS_DETACH: gc_term(); break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: return false; } g_hInst = hInstance; return true; } ------------------- Then I do this: dmd -L/implib -ofbar.dll bar.d dll.d To test, I do this: ptest.d ------------------- import bar; int main() { p(); return 0; } ------------------- and do this: dmd -L+bar -ofptest.exe ptest.d Executing ptest.exe results in Error: Access Violation Lars Ivar Igesund
Feb 26 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Lars Ivar Igesund wrote:
 J C Calvarese wrote:
 
 Lars Ivar Igesund wrote:

 How do I use extern functions in a DLL?
 printf is an excellent example.
[...]
 
 and do this:
 dmd -L+bar -ofptest.exe ptest.d
Try: dmd -L+bar -ofptest.exe ptest.d bar.d I can't explain WHY adding bar.d to the command line makes it work, but apparently it does. This seems like either a compiler bug or a linker bug that doesn't show up until runtime. Output: F:\pgm\d\testing\newsgroup\dll_printf>dmd -L/implib -ofbar.dll bar.d dll.d d:\dmd\bin\..\..\dm\bin\link.exe bar+dll,bar.dll,,user32+kernel32/noi/implib; F:\pgm\d\testing\newsgroup\dll_printf>dmd -L+bar -ofptest.exe ptest.d bar.d d:\dmd\bin\..\..\dm\bin\link.exe ptest+bar,ptest.exe,,user32+kernel32/noi+bar; F:\pgm\d\testing\newsgroup\dll_printf>ptest.exe print something
 Lars Ivar Igesund
-- Justin http://jcc_7.tripod.com/d/
Feb 27 2004
parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
J C Calvarese wrote:
 Lars Ivar Igesund wrote:
 
 J C Calvarese wrote:

 Lars Ivar Igesund wrote:

 How do I use extern functions in a DLL?
 printf is an excellent example.
[...]
 and do this:
 dmd -L+bar -ofptest.exe ptest.d
Try: dmd -L+bar -ofptest.exe ptest.d bar.d I can't explain WHY adding bar.d to the command line makes it work, but apparently it does. This seems like either a compiler bug or a linker bug that doesn't show up until runtime.
Yes, I know that this works. But I suppose that that is because bar.d is compiled into the executable. In this case ptest.exe works without bar.dll/lib too, so I suppose the dll isn't used at all. Interestingly enough, embracing the printf in a try/catch block, makes ptest execute without output or errors. I guess this suggest some runtime exception/error of some sort, but the provided info isn't really enough to think anything. Lars Ivar Igesund
Feb 28 2004
next sibling parent J C Calvarese <jcc7 cox.net> writes:
Lars Ivar Igesund wrote:
 J C Calvarese wrote:
[...]
 dmd -L+bar -ofptest.exe ptest.d
Try: dmd -L+bar -ofptest.exe ptest.d bar.d I can't explain WHY adding bar.d to the command line makes it work, but apparently it does. This seems like either a compiler bug or a linker bug that doesn't show up until runtime.
Yes, I know that this works. But I suppose that that is because bar.d is compiled into the executable. In this case ptest.exe works without bar.dll/lib too, so I suppose the dll isn't used at all.
Sorry, I didn't realize that my fix wasn't a fix at all. It sounds like a bug to me. You might try reposting your example with a "[bug] creating a dll in d" topic to have a better chance of getting Walter's attention. I don't think many people have been using D to create .dll's to use in D. I've created a .dll in C to use D. That worked. I've used existing .dll's (compiled in C) in D. I know others have done the same. Delphi .dll's can be used in D. D .dll's should be usable in D, but there might be a bug preventing this.
 
 Interestingly enough, embracing the printf in a try/catch block,
 makes ptest execute without output or errors. I guess this suggest
 some runtime exception/error of some sort, but the provided info
 isn't really enough to think anything.
 
 Lars Ivar Igesund
-- Justin http://jcc_7.tripod.com/d/
Feb 28 2004
prev sibling parent uwem <uwem_member pathlink.com> writes:
In article <c1pi9b$2808$1 digitaldaemon.com>, Lars Ivar Igesund says...
J C Calvarese wrote:
 Lars Ivar Igesund wrote:
Yes, I know that this works. But I suppose that that is because bar.d is compiled into the executable. In this case ptest.exe works without bar.dll/lib too, so I suppose the dll isn't used at all.
Hi! I have the same problem with exported classes in a library (dll). I have no plan for a solution. :-( Sorry. Uwe
Feb 29 2004