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

VB.net console app calling a Delphi (win32 DLL)

Status
Not open for further replies.

SpeedBWild

Programmer
Apr 29, 2004
117
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.

Thanks,


Thanks
 
Dim ptXVal As IntPtr = Marshal.StringToHGlobalAnsi(sXVal)
GetData(sX :string; var sY :string) :integer;

are you sure "sx" and "ptxVal" are the same type.

what is As IntPtr

I assume "sX :string;" is one argument.

excuse my intrusion, I am just excercising my
frontal lobe.

tomcruz.net
 
sX and ptXval are not of the same type. IntPTr is a integer pointer in VB.net.

In my VB.net code I need to marshal my string to a pointer.

In my Delphi DLL, (here the problem) I don't know how to read the value of the pointer I pass in.

Any ideas?
 
dont see how you are going to get any results untill
sX and ptXval are the same type.


tomcruz.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top