www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - DLL with same conventions as VC++

reply Gilles G. <schaouette free.fr> writes:
Hello,
I am trying to build a dll using D.

In my first attempt to write it in C++, I add working calls with the main
application by using something like:
   int __stdcall DLLVersion(){return 1;}
this also only worked with VC++ using a .def file like (couldn't manage to make
it work using borland):
  EXPORTS
    DLLVersion

As I read the documentation, I understand (not sure) that in order to have
equivalent calls, I must use:
  export extern(Windows) int DLLVersion(){return 1;}

The problem is that the main application doesn't seem torecognize the D DLL.
Any help would be fantastic!

--
Gilles
Jun 20 2007
parent reply torhu <fake address.dude> writes:
Gilles G. wrote:
 Hello,
 I am trying to build a dll using D.
 
 In my first attempt to write it in C++, I add working calls with the main
application by using something like:
    int __stdcall DLLVersion(){return 1;}
 this also only worked with VC++ using a .def file like (couldn't manage to
make it work using borland):
   EXPORTS
     DLLVersion
 
 As I read the documentation, I understand (not sure) that in order to have
equivalent calls, I must use:
   export extern(Windows) int DLLVersion(){return 1;}
 
That looks correct to me.
 The problem is that the main application doesn't seem torecognize the D DLL.
 Any help would be fantastic!
Did you check out the example in dmd\samples\mydll? And this page? http://www.digitalmars.com/d/dll.html
Jun 20 2007
parent reply Gilles G. <schaouette free.fr> writes:
torhu Wrote:

 Gilles G. wrote:
 Hello,
 I am trying to build a dll using D.
 
 In my first attempt to write it in C++, I add working calls with the main
application by using something like:
    int __stdcall DLLVersion(){return 1;}
 this also only worked with VC++ using a .def file like (couldn't manage to
make it work using borland):
   EXPORTS
     DLLVersion
 
 As I read the documentation, I understand (not sure) that in order to have
equivalent calls, I must use:
   export extern(Windows) int DLLVersion(){return 1;}
 
That looks correct to me.
 The problem is that the main application doesn't seem torecognize the D DLL.
 Any help would be fantastic!
Did you check out the example in dmd\samples\mydll? And this page? http://www.digitalmars.com/d/dll.html
Yes, of course! I use the same method: export and .def file. I understood the following: export extern(Windows) <=> __stdcall export extern(C) <=> __cdecl Does anybody have any idea what could be the (subtle) difference? Thanks!
Jun 20 2007
next sibling parent reply torhu <fake address.dude> writes:
Gilles G. wrote:
 torhu Wrote:
 
 Gilles G. wrote:
 Hello,
 I am trying to build a dll using D.
 
 In my first attempt to write it in C++, I add working calls with the main
application by using something like:
    int __stdcall DLLVersion(){return 1;}
 this also only worked with VC++ using a .def file like (couldn't manage to
make it work using borland):
   EXPORTS
     DLLVersion
 
 As I read the documentation, I understand (not sure) that in order to have
equivalent calls, I must use:
   export extern(Windows) int DLLVersion(){return 1;}
 
That looks correct to me.
 The problem is that the main application doesn't seem torecognize the D DLL.
 Any help would be fantastic!
Did you check out the example in dmd\samples\mydll? And this page? http://www.digitalmars.com/d/dll.html
Yes, of course! I use the same method: export and .def file. I understood the following: export extern(Windows) <=> __stdcall export extern(C) <=> __cdecl Does anybody have any idea what could be the (subtle) difference?
Do you still have problems? Please post the exact error messages you get, in English. And maybe your source code and command lines too.
Jun 20 2007
parent reply torhu <fake address.dude> writes:
By the way, if you're trying to call C++ code from D, it has to be 
extern "C" functions.  C++ symbols are mangled differently than D or C 
symbols.
Jun 20 2007
parent Gilles G. <schaouette free.fr> writes:
I enventually managed to make it work...
It appeared that the problem wasn't the dll export convention.
I discarded some of the function I exported and now the dll is well recognised
by the main app.

Just to be exhaustive:
To have a VC++ compatible dll interface, one just need to put
extern(Windows) in front of the function declaration instead of __stdcall in
the function declaration.
You also have to use a .def file were the names of the functions you want to
export are listed like:
EXPORTS
    MyFunction1
    MyFunction2
Use the DllMain function (copy/paste) given here:
http://www.digitalmars.com/d/dll.html

Thanks a lot for your help!

--
Gilles

torhu Wrote:

 By the way, if you're trying to call C++ code from D, it has to be 
 extern "C" functions.  C++ symbols are mangled differently than D or C 
 symbols.
Jun 21 2007
prev sibling parent Hoenir <mrmocool gmx.de> writes:
Gilles G. wrote:
 I understood the following:
 export extern(Windows) <=> __stdcall
 export extern(C) <=> __cdecl
 
 Does anybody have any idea what could be the (subtle) difference?
I think the symbols are mangled differently.
Jun 21 2007