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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Nick Hall (nickh_at_aslan.nospam.co.uk)
Date: 04/07/04


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



Relevant Pages

  • Re: [array / List]Unknown number of objects
    ... Oliver Wong wrote: ... declaration is saying that that the "type parameter" ... might read this as "A list of strings is a collection of strings". ... Comparator interface also accepts a generic type argument, ...
    (comp.lang.java.help)
  • Re: [array / List]Unknown number of objects
    ... very strange as the name of the object you want to instanciate has a ... declaration is saying that that the "type parameter" ... read this as "A list of strings is a collection of strings". ... Comparator interface also accepts a generic type argument, ...
    (comp.lang.java.help)
  • Re: const keyword usage
    ... > Suppose the structure name is 'str', than the typical declaration I ... arr is an initialiser for the .array member of type const char **. ... You could use a cast here. ... strings its elements point to. ...
    (comp.lang.c)
  • Re: enumerated static string table
    ... The strings will be of widely ... > (enum name,string) to maintain. ... I was briefly thrown off by the missing semicolon on the declaration ... an input file; the input file might look like this: ...
    (comp.lang.c)
  • Converting C API to VB6 declaration
    ... I'm trying to convert this to a VB6 declaration. ... Declare Function SetupDiLoadClassIcon Lib "setupapi" (ByVal lpGUID As ... String, hIcon as Long, lpInt as Long) As Boolean ...
    (microsoft.public.vb.winapi)