Jump to page: 1 2 3
Thread overview
Creating A D DLL For Use By VB
Mar 19, 2006
strkweatherr
Mar 19, 2006
James Dunne
Mar 19, 2006
starkweatherr
Mar 19, 2006
starkweatherr
Mar 19, 2006
starkweatherr
Mar 20, 2006
James Dunne
Mar 20, 2006
starkweatherr
Mar 20, 2006
James Dunne
Mar 20, 2006
David L. Davis
Mar 20, 2006
James Dunne
Mar 21, 2006
David L. Davis
Mar 21, 2006
Rory Starkweather
Mar 21, 2006
Regan Heath
Mar 21, 2006
Rory Starkweather
Mar 21, 2006
Regan Heath
Mar 21, 2006
Rory Starkweather
Mar 21, 2006
John Reimer
Mar 21, 2006
James Dunne
Mar 21, 2006
David L. Davis
Mar 20, 2006
starkweatherr
Mar 20, 2006
James Dunne
Mar 20, 2006
Derek Parnell
Mar 20, 2006
starkweatherr
March 19, 2006
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.

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.

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. ???

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?


March 19, 2006
strkweatherr@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
March 19, 2006
Is it safe to assume that I need to put the export (Windows) in fron to the declaration as well as the definition?


March 19, 2006
No joy. Here's the code.

//MyDLL.d
extern (Windows) int addInt(int int1, int int2);

//MyDLL2.d
module mydll;
extern (Windows) int addInt(int int1, int int2)
{
return (int1 + int2);
}

;MyDLL.def
LIBRARY "mydll.dll"
EXETYPE NT
SUBSYSTEM WINDOWS
CODE SHARED EXECUTE
DATA WRITE

@echo off
REM Build.bat
E:\DigitalMars\dmd\bin\dmd -ofmydll.dll mydll2.d dll.d mydll.def
E:\DigitalMars\dmd\bin\implib /system mydll.lib mydll.dll
E:\DigitalMars\dmd\bin\dmd test.d mydll.lib

I got two errors. Implib says no functions are exported and OptLink says Undefined Symbol: intAdd@8

Am I supposed to put 'export extern (Windows) ' instead of just 'extern
(Windows)'?


March 19, 2006
In article <dvkad2$2dcj$1@digitaldaemon.com>, starkweatherr@mchsi.com says...
>
>Is it safe to assume that I need to put the export (Windows) in fron to the declaration as well as the definition?
>
>

I don't know where the other message went, but I tried both
//MyDLL.d
extern (Windows) int addInt(int int1, int int2);
//MyDLL.d
extern (Windows) int addInt(int int1, int int2);

and

//MyDLL.d
export extern (Windows) int addInt(int int1, int int2);
//MyDLL.d
export extern (Windows) int addInt(int int1, int int2);

and I get the same thing: Error 42: Symbol undefined _addInt@8

I also tried:

LIBRARY "mydll.dll"
EXETYPE NT
SUBSYSTEM WINDOWS
CODE SHARED EXECUTE
DATA WRITE
EXPORTS
addInt  @8

and got:

Optlink : Error 180: No match found for export/Entry - : addInt Optlink : Error 81: Cannot EXPORT : addInt

DMD Error 42: Symbol Undefined _addInt@8


March 20, 2006
starkweatherr@mchsi.com wrote:
> No joy. Here's the code.
> 
> //MyDLL.d
> extern (Windows) int addInt(int int1, int int2);
> 
> //MyDLL2.d
> module mydll;
> extern (Windows) int addInt(int int1, int int2)
> {
> return (int1 + int2);
> }
> 
> ;MyDLL.def
> LIBRARY "mydll.dll"
> EXETYPE NT
> SUBSYSTEM WINDOWS
> CODE SHARED EXECUTE
> DATA WRITE
> 
> @echo off
> REM Build.bat
> E:\DigitalMars\dmd\bin\dmd -ofmydll.dll mydll2.d dll.d mydll.def
> E:\DigitalMars\dmd\bin\implib /system mydll.lib mydll.dll
> E:\DigitalMars\dmd\bin\dmd test.d mydll.lib
> 
> I got two errors. Implib says no functions are exported and OptLink says Undefined Symbol: intAdd@8
> 
> Am I supposed to put 'export extern (Windows) ' instead of just 'extern
> (Windows)'?
> 
> 

Why are you running implib on the static lib instead of relying on the compiler-generated DLL file?  I get my DMD-generated DLL file working just fine.

I think at this point it's just easiest to post a working example project. :)  Enjoy!

In summary, you need the 'export' directive on each function you intend to export; also you need an EXPORTS section in your DEF file which explicitly lists all the function names you're exporting through your DLL.  Then, it's a simple matter of declaring the function correctly in the VB code, which sometimes is not so simple. =P

Remember these important tips for VB <-> C/C++/D interop:
	* All parameters are passed ByRef by default, use ByVal instead
	* No unsigned types except Byte
	* Integer = 16 bit signed
	* Long = 32 bit signed
	* VB strings are NULL-terminated wide-character strings.
	* D strings are length-tracked dynamic arrays.

-- 
Regards,
James Dunne


March 20, 2006
On Sun, 19 Mar 2006 00:51:15 +0000 (UTC), strkweatherr@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
March 20, 2006
It doesn't appear that the zip file is accessible. A working sample is just what I need. Can you post the code un-zipped?

The reason why I a using implib is that is the way the demo app does it. The code I am using came with the DMD package, and works as long as I run it from test.exe. If there is a better way, that is exactly what I am looking for.

The reason why I am not using EXPORTS in the .def files is the error messages that produces. There is something wrong there also, and I have no idea what that problem is either.

I'm not worried about the VB side. Maybe I should explain the project.

I've been writing bare frameworks for using DLLs generated by various C/C++ compilers from VB for upload to PSC. So far I have finished VC++ 6 and BCB 5.5. Someone who saw the code suggested that I write one for DMC, which I had never heard of before. That effort is still awaiting a response to questions in the Command Line forum on the DMC side. Similar problems, different language.

I noticed 'D' while I was trying to get DMC to work and decided to give that a try. It has been really frustrating to try to put together what is in the documentation with the demo app that comes with DMD, especially since that has a typo in it.

I would appreciate any help that I can get, but this is a one time thing for me. Once I get a working framework, I'll never look back.

Thanks for your help.


March 20, 2006
Indeed it does work. Thanks.

Now, a post-mortem, if you don't mind.

I assume that, since it worked, the file you called mydll.d is the one with the DllMain entry point in it and that you didn't make any changes to it?

Nothing new in testdll.d.

The differences in the .def file seem to be significant. I'll need to experiment to find out why, but, with your .def file I don't get the messages about not being able to export the function.

The compile part is very different from the sample. I don't work with command line stuff very often, so please bear with me. The build.bat file I used came with the demo app. Now it seems clear that the second call to dmd was just to compile test.dll. The call to implib, though . . . I'm still having trouble grasping the multiple different ways to create a dll. In this approach you are using the .def file without implib. As I understand it, if you use implib you don't need the .def file, or am I lost again?

Anyway, this works, and I will post it with proper credit to you. D looks interesting. naturally, it has annnoying similarities to C++, but it seems a lot cleaner. The reason why I got into this in the beginning is that VB, although great for RAD, lacks both power and sophistication. C++ and D have the power, but are pretty much useless for RAD. The obvious solution is a combination. Unfortunately, my programming path went straight from C to VB, with no stop at C++, so I need the C++/D part to be as easy as possible with eht emphasis on ease of use of DLLs.

Thanks a lot for your help.

Rory






March 20, 2006
starkweatherr@mchsi.com wrote:
> It doesn't appear that the zip file is accessible. A working sample is just what I need. Can you post the code un-zipped?
> 
> The reason why I a using implib is that is the way the demo app does it. The code I am using came with the DMD package, and works as long as I run it from test.exe. If there is a better way, that is exactly what I am looking for.
> 
> The reason why I am not using EXPORTS in the .def files is the error messages that produces. There is something wrong there also, and I have no idea what that problem is either.
> 
> I'm not worried about the VB side. Maybe I should explain the project.
> 
> I've been writing bare frameworks for using DLLs generated by various C/C++ compilers from VB for upload to PSC. So far I have finished VC++ 6 and BCB 5.5. Someone who saw the code suggested that I write one for DMC, which I had never heard of before. That effort is still awaiting a response to questions in the Command Line forum on the DMC side. Similar problems, different language.
> 
> I noticed 'D' while I was trying to get DMC to work and decided to give that a try. It has been really frustrating to try to put together what is in the documentation with the demo app that comes with DMD, especially since that has a typo in it.
> 
> I would appreciate any help that I can get, but this is a one time thing for me. Once I get a working framework, I'll never look back.
> 
> Thanks for your help.
> 
> 

The ZIP is accessible, just not through the crappy web interface provided at digitalmars.com.  Read with a real newsgroup reader, like Thunderbird and you should be fine.  I'll attach the extracted code here.

Derek Parnell's response's code is extremely similar to mine.  Both approaches seem to work.

BTW, what is PSC?  ... and why are you using that abortion of a language called VB? =P

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O
M--@ V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e
h>--->++ r+++ y+++
------END GEEK CODE BLOCK------

James Dunne


« First   ‹ Prev
1 2 3