Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a Delphi 5 dll from VB.net 1

Status
Not open for further replies.

bustell

Programmer
Mar 22, 2002
159
US
Does anyone have any experience using a Delphi 5 DLL from VB.net?

I have been given a Delphi 5 dll to use for a certain processing. However, I am having trouble figuring out how to connect to it. Seems there are type problems. The Delphi types I need to use are as follows: Byte, Boolean, Pchar, Integer, Double and array of bytes.

Any help would be greatly appreciated.

Thank you!


Pat B
 
I've used Delphi 5 DLL's in VB 6, which should be similar to using them in VB .NET. I had quite a few troubles with type problems when I was first using the DLL in VB 6. It turns out that all my type problems were due to passing strings from my VB 6 program to my Delphi 5 DLL. According to Delphi's documentation, passing strings to a Delphi DLL can cause quite a few problems:

Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters.

At first I followed the advice of the Delphi documentation and programmed my Delphi 5 DLL so that it used the ShareMem library and the BORLNDMM.DLL, along with PChar and ShortString parameters. As it turns out, this won't work when you are calling a Delphi DLL from VB. I had to completely disregard the Delphi advice and reprogram my Delphi 5 DLL, removing the reference to ShareMem and the BORLNDMM.DLL, and take out all PChar parameters and replace them with String parameters. I don't know if you will have the luxury of being able to do this.

You shouldn't have any type problems with the Delphi types Byte, Integer, and Double, because they are all the same size as the equivalent VB .NET data types. A Delphi 5 boolean type is 1 byte, whereas VB .NET will assign up to 2 bytes for a boolean value, though the VB .NET documentation claims that the amount of space allocated for a Boolean value could be platform dependent and use less than 2 bytes. My guess is that the PChar data type is what is causing you problems. There shouldn't be any problems with passing an array of bytes, but I can't say for certain as I don't have any direct experience with passing arrays to a Delphi 5 DLL.

To make a long story short, the problems I experienced with calling a Delphi 5 DLL from VB were problems that I could only really fix from the Delphi 5 side of things, though someone else may be able to prove me wrong.

Jay
 
Jay,

Thanks for your thoughts. It is indeed the Pchars that are giving me trouble. VB.Net does not seem to give you a way to get the pointer to a variable. There was a way in VB6 using VarPtr(), but that has been removed in VB.net. :-(

The search goes on...


Pat B
 
This website may have some advice on how to convert strings to pointers in VB .NET:
More specifically, it has a short section on strings and pointers:

Writing and reading strings
In this example we will write a managed string to heap and then read it back.

Dim str as String = "hello world"
Dim ptr As IntPtr = Marshal.StringToHGlobalAuto(str)
Dim mystring As String = Marshal.PtrToStringAuto(ptr)

Here, we used StringToHGlobalAuto() method of marshal class that copies the contents of a managed string into unmanaged memory. We collect the memory address in IntPtr structure. To read the string back we used PtrToStringAuto() method of Marshal class that returns a string at specified memory location.

I haven't tried it myself so I can't say for certain that it will work with PChar's, but it looks promising.

Jay
 
Jay,

It does look promising. I will be giving it or something similar to it soon.

Thanks for the help.


Pat B
 
Jay,

You're comments did indeed help me get it working.

Thanks a lot!


Pat B
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top