PC Review


Reply
Thread Tools Rate Thread

C dll reference/call works in VB6, not working in VB.NET

 
 
John
Guest
Posts: n/a
 
      7th Apr 2004
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
 
Reply With Quote
 
 
 
 
Nick Hall
Guest
Posts: n/a
 
      7th Apr 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
when i reference a com object by regsvr32 it then find it in the COM tab, it works ok. but when i reference a com object by referencing the .dll file i get this error in my C# application: DR Microsoft Dot NET 1 19th Jan 2008 05:01 PM
when i reference a com object by regsvr32 it then find it in the COM tab, it works ok. but when i reference a com object by referencing the .dll file i get this error in my C# application: DR Microsoft C# .NET 1 18th Jan 2008 08:18 AM
when i reference a com object by regsvr32 it then find it in the COM tab, it works ok. but when i reference a com object by referencing the .dll file i get this error in my C# application: DR Microsoft Dot NET Framework 0 18th Jan 2008 03:22 AM
Re: Call By Value and Call By Reference Nicholas Paldino [.NET/C# MVP] Microsoft C# .NET 0 30th Aug 2004 04:33 PM
Using a project reference rather than assembly reference prevents an interface from working Scott James Microsoft VB .NET 0 8th Oct 2003 02:17 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:31 AM.