Re: C dll reference/call works in VB6, not working in VB.NET
From: Nick Hall (nickh_at_aslan.nospam.co.uk)
Date: 04/07/04
- Next message: Raja: "A simple ComboBox question"
- Previous message: post2shan_at_hotmail.com: "Re: dim as Vs. dim as new"
- In reply to: John: "C dll reference/call works in VB6, not working in VB.NET"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 7 Apr 2004 13:34:19 +0100
A couple of things immediately spring to mind: -
1. It definitely needs to be a Sub!
2. The original VB6 declaration causes the strings to be passed as ANSI
strings rather than Unicode strings (you couldn't directly pass strings as
Unicode using a Declare). Therefore I think the CharSet needs to be
Charset.Ansi.
3. As strings are immutable in VB.NET you should probably declare them As
StringBuilder instead of String. This will ensure that P/Invoke will copy
the data into StringBuilder objects once the call has completed.
4. i_ByPass was declared As Integer in the original, meaning it needs to be
declared As Short in VB.NET (Integer is now 32 bits rather than 16)
5. I notice there is no Alias in the VB6 declaration (which would be the
equivalent of the EntryPoint field). Was this an omission?
That being said I think your declaration needs to look something like the
following: -
Imports System.Text
Class Polk
<DllImport("rlpolk.dll", EntryPoint:="microvin", _
SetLastError:=True, CharSet:=CharSet.Ansi, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Sub csub(ByVal T_VININ As StringBuilder, _
ByVal T_VINOUT As StringBuilder, ByVal i_byPass As Short)
End Function
End Class
And call it like this: -
Dim T_VININ as new StringBuilder(255)
Dim T_VINOUT as new StringBuilder(108)
Polk.csub(T_VININ, T_VINOUT, 98)
I hope this helps,
Nick Hall
"John" <john_germani@adp.com> wrote in message
news:f6bf31ef.0404070335.fb56c78@posting.google.com...
> I am trying to call a third party company's dll that I have no control
> over. They tell me it was written in C. The declaration in VB6 is as
> follows:
>
> Private Declare Sub csub Lib "rlpolk.dll" (ByVal T_VININ As String, _
> ByVal T_VINOUT As String, ByVal i_bypass As
> Integer)
>
> The T_VININ string is 25 characters and the T_VINOUT string is 108. I
> have tried to convert the declaration to VB.NET with little success:
>
> Class Polk
> <DllImport("c:\rlpolk.dll", EntryPoint:="microvin", _
> SetLastError:=True, CharSet:=CharSet.Unicode, _
> ExactSpelling:=True, _
> CallingConvention:=CallingConvention.StdCall)> _
> Public Shared Function csub(ByVal T_VININ As String, _
> ByVal T_VINOUT As String, ByVal i_byPass As Integer)
> End Function
> End Class
>
> The call to the routine is:
>
> Polk.csub(T_VININ, T_VINOUT, 98)
>
> When I do this, I get the message that PInvoke cannot return a
> Variant.
>
> If I change it to a Sub, the call to the routine returns nothing.
> T_VININ is the variable sent to the routine and T_VINOUT should be the
> return string. (T_VININ is a VIN number and T_VINOUT is a 108
> character explosion of the vin.)
>
> I have tried many combinations including trying to Marshal the
> strings, but all have failed. Please help. Thank you.
>
> John Germani
- Next message: Raja: "A simple ComboBox question"
- Previous message: post2shan_at_hotmail.com: "Re: dim as Vs. dim as new"
- In reply to: John: "C dll reference/call works in VB6, not working in VB.NET"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|