Creating A D DLL For Use By VB

James Dunne james.jdunne at gmail.com
Sat Mar 18 23:40:03 PST 2006


strkweatherr at mchsi.com wrote:
> I'm having the same problem with DigitalMars D as I had with DigitalMars C/C++,
> but nobody over there has answered any of my questions. 
> 
> I started with the MyDLL package and modified it so that it accepts two ints and
> returns the sum. Test.exe works fine, but VB says the .lib file doesn't exist. I
> am assuming that is because it isn't in a recognizable format.

VB can only work with DLLs, not LIBs.  LIBs are statically linked 
libraries.  DLLs are dynamically linked libraries.  Worlds apart.

> 
> I'm really desperate so I tried:
> 
> Public Declare Function addInt _
> Lib "E:\DMDWork\MyDll.dll" _
> Alias "D5mydll6addIntFiiZi" _
> (lngInt1 As Long, _
> lngInt2 As Long) _
> As Long
> 
> As you might expect, I got a Type 49 error.
> 

Write your D DLL to use the Windows calling convention (a.k.a. STDCALL). 
  Just put an 'extern (Windows)' before each function definition, then 
you won't have to use mangled symbol name of the function.

Also, VB uses ByRef parameters by default; you want to use ByVal 
parameters instead.  So your new VB function declaration should look 
like this:

Public Declare Function addInt Lib "E:\DMDWork\MyDLL.dll" (ByVal lngInt1 
As Long, lngInt2 As Long) As Long

and define your D function like this:

extern (Windows) int addInt (int a, int b) {
	return a + b;
}

> Another question. When I try to put EXPORTS in the .def file I get error
> messages that say there is no entry point for the function. ???
> 

Not sure what you mean here... some detailed output would help.

> Does anyone have a piece of source code, that works, for a DLL that is
> accessible from non-D programs and that is less than 50 lines long?

I'd say to stick with Windows calling convention for interoperating with 
VB.  I've written a number of D DLLs that work with VBA (in Excel) quite 
easily.  Debugging becomes another problem, however...

Good luck!  You can reach me at my posted e-mail address if you need 
further assistance or sample code.

-- 
Regards,
James Dunne



More information about the Digitalmars-d-learn mailing list