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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access win32 DLL from VB.net

Status
Not open for further replies.

SpeedBWild

Programmer
Joined
Apr 29, 2004
Messages
117
Location
US
I have been giving the task of taking a Delphi console application and making it into a Win32 DLL. From there I then need to be able to call the functions of the DLL from a VB.NET console application (during the testing stage).

I have several functions in my Delphi (Win32 DLL) and I can call all of them from the VB.NET console application. The one problem I am running into is for one of the functions I need to pass in a value and then based on that value run the function. This is not working. If I hard code in a value everything works, if I try to pass it in it fails. Below is the code I am using trying to pass in a value and use (sX)

Delphi code:
function GetData(sX :string; var sY :string) :integer;
begin
sY := GetData2('','','',sX,'');
result := 0;
end;

VB.net code:
<DllImport("C:\TestDLL.dll", EntryPoint:="GetData", SetLastError:=False, CharSet:=CharSet.Ansi, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Function GetData(ByVal pX As IntPtr, ByRef pY As IntPtr) As Int32
End Function



Dim MAXLen as int32 =255
Dim sXVal As String = "sssssss".PadRight(MAXLen , Chr(0))
Dim sYVal As String = "".PadRight(MAXLen , Chr(0))
Dim ptXVal As IntPtr = Marshal.StringToHGlobalAnsi(sXVal)
Dim ptYVal As IntPtr = Marshal.StringToHGlobalAnsi(sYVal)
Dim i As Int32 = -1
Try
i = GetData(ptXVal , ptYVal )
Catch e As System.Runtime.InteropServices.SEHException
Console.WriteLine(e.ErrorCode())
Console.WriteLine(e.Message)
Console.WriteLine(e.Source)
Catch e As Exception
Console.WriteLine(e.Message)
Finally
sYVal = Marshal.PtrToStringAnsi(ptYVal )
Marshal.FreeHGlobal(ptXVal)
Console.WriteLine(sYVal)
End Try

I am getting the System.Runtime.InteropServices.SEHException
exception. External component has thrown an exception.

Any ideas? If I am posting to the wrong forum, I apologize in advance. I'm not really sure where to post to.
 
I think that the problem might be to do with the strings. From memory you can use PChars and fixed size strings in Delphi DLLs, but if you use dynamic strings as you are you need to include the Borland Memory Manager.

Hope this helps.
 
You should be able to add the delphi dll as a COM reference.

Try looking at the e.innerexception.message, see if the delphi error is getting carried through.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
If in my Delphi DLL, I change the type to PChar what does that give me, a pointer to the first char? Right now I Marshal my string in VB.net to pointer. Any idea how I read the pointer's string value once I'm in the Delphi DLL?




 
As far as I am aware PChar is a pointer to a C type string ie an ASCIIZ or Null terminated string.

Your code also defines a MAXLen of 255

The easiest approach would therefore be to treat the string in Delphi as an array [0..MAXLen] of char, where in most cases such an array is treated pretty much as a string.

Hope this helps.
 
Having investigated a little further, the recommended approach to strings in a Delphi DLL is:

If the consumer is a Delphi program you can use either, but if you use strings include a reference to the sharemem unit and you need Borland's memeory manager.

If the consumer is not a Delphi program then follow the Windows standards which for strings is lpString ie a Long Pointer to an ASCIIZ - which equates to a PChar.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top