Creating A D DLL For Use By VB
Derek Parnell
derek at psych.ward
Sun Mar 19 23:37:38 PST 2006
On Sun, 19 Mar 2006 00:51:15 +0000 (UTC), 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.
> ...
> 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 just had a go now, following the instructions in the DigitalMars docs.
First I took the 'mydll' source in the docs and saved it to a file called
'mydll.d'. Then I created a new file called 'testdll.d' and here is its
source ...
--------testdll.d-----------
import mydll;
import std.math;
extern (Windows)
{
int sqrab(short a, short b)
{
// Calculates the square root of the sum of two squares.
// (eg. used to find the length of the hypotenuse)
return cast(int)sqrt((cast(real)a * cast(real)a) + (cast(real)b *
cast(real)b));
}
}
--------------
I then created a .DEF file which contained ...
-------------testdll.def----------------
LIBRARY TESTDLL
DESCRIPTION 'My test DLL written in D'
EXETYPE NT
CODE PRELOAD DISCARDABLE
DATA PRELOAD SINGLE
EXPORTS
sqrab @2
------------------------------------------
I next compiled it with the following command line ...
dmd -oftestdll.dll mydll.d testdll.d testdll.def
Then moved the testdll.dll file to the windows system32 folder,
Finally I created a VB module that contained the line ...
Public Declare Function sqrab Lib "testdll" _
(ByVal A As Integer, ByVal B As Integer) As Long
and the VB form used the DLL command as ...
Sub Form_Click()
Dim x As Integer
Dim y As Integer
x = CInt(Text1.Text)
y = CInt(Text2.Text)
result = sqrab(x, y)
Label1.Caption = CStr(result)
Label2.Caption = CStr(Sqr(CLng(x) * CLng(x) + CLng(y) * CLng(y)))
End Sub
And it all just worked.
--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
20/03/2006 6:04:50 PM
More information about the Digitalmars-d-learn
mailing list