Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

VB6 to VB.NET API Function Upgrades

Any help with the following would be appreciated, its driving me nuts..
Am I just being dumb today, or have I missed something ?

Original VB6 API declaration:

Private Declare Function Command Lib "InnTechSSP.dll" (src As UDT) As UDT
'  The user defined structure
Private Type UDT
    rxStatus As Integer
    datalen As Byte
    array1(254) As Byte
End Type

The declaration was changed to as follows:

<DllImport("InnTechSSP.dll")> Private Shared Function Command(ByVal src As

End Function

The first problem was dealing with a structure ( version of a type)
that contains an array that is initialized:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Public
Structure UDT

      Friend rxStatus As Integer
        Friend datalen As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=255,
ArraySubType:=UnmanagedType.AsAny)> _

        Friend array1() As Byte

        Public Sub Initialize()

            ReDim array1(254)

        End Sub

End Structure

The initialize method of the structure was then called before the structure is first used.

The last remaining problem is having an API declaration that returns a
structure (UDT in this case). It seems that VB.Net does not like to do this.

I tried changing the return value to return an IntPtr, and then using
MarshalAs.PtrToStructure to convert it back, but then I get a
NullReferenceException (Object reference not set to an instance of an
object) on the call to the API. If I break the code and type Command(src)
this returns the above exception. (src is an instance of the UDT structure).

This is the line that generates the error:

cpy = CType(Marshal.PtrToStructure(Command(src), GetType(NotePay.UDT)), UDT)

NotePay being my class that is interacting with the InnTechSSP.dll.


Thursday, April 15, 2004

Isn't VB6 ByRef by default (can't remember) - so shouldn't your VB.NET declaration be ByRef?

Duncan Smart
Thursday, April 15, 2004

You didn't turn the VB6 Integer into a VB.NET Short, which may be important

Dave Rothgery
Wednesday, May 12, 2004

And yes, VB6 is ByRef by default.

[obDisclaimer: I'm a web guy. I don't do API calls.]

Dave Rothgery
Wednesday, May 12, 2004


Friday, June 11, 2004

*  Recent Topics

*  Fog Creek Home