VB6 -> VB.Net

  • Thread starter Thread starter Ken Kast
  • Start date Start date
K

Ken Kast

I need some help. The following third-part VB6 code works:

Type cdSourceInfo
SourceType As Long
rsrvd As Long Name As String * 64
NameInOS As String * 64
PortType As Long
u As cdPortDescripUnion
End Type

Declare Function CDEnumDeviceNext Lib "CDSDK.dll" (ByVal hEnum As Long,
ByRef pSourceInfo As cdSourceInfo) As Long

Private SourceInfo As cdSourceInfo
err = CDEnumDeviceNext(hEnum, SourceInfo)

The dll is unmanaged.

My conversion of this is:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> Structure
cdSourceInfo
Public SourceType As Integer
Public rsrvd As Integer
<VBFixedString(64)> Public Name As String
<VBFixedString(64)> Public NameInOS As String Public PortType As Integer
Public u As cdPortDescripUnion
End Structure

Declare Function CDEnumDeviceNext Lib "CDSDK.dll" (ByVal hEnum As Integer,
ByRef pSourceInfo As cdSourceInfo) As Integer

Private m_sourceInfo As cdSourceInfo
SourceInfo = New cdSourceInfo()
err = CDEnumDeviceNext(hEnum, SourceInfo)
I get a null-reference exception here.
Can someone guess as to what I'm doing wrong?

Ken
 
Ken Kast said:
I need some help. The following third-part VB6 code works:

Type cdSourceInfo
SourceType As Long
rsrvd As Long Name As String * 64
NameInOS As String * 64
PortType As Long
u As cdPortDescripUnion
End Type

Declare Function CDEnumDeviceNext Lib "CDSDK.dll" (ByVal hEnum As
Long, ByRef pSourceInfo As cdSourceInfo) As Long

Private SourceInfo As cdSourceInfo
err = CDEnumDeviceNext(hEnum, SourceInfo)

The dll is unmanaged.

My conversion of this is:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Structure cdSourceInfo
Public SourceType As Integer
Public rsrvd As Integer
<VBFixedString(64)> Public Name As String
<VBFixedString(64)> Public NameInOS As String
Public PortType As Integer
Public u As cdPortDescripUnion
End Structure

Declare Function CDEnumDeviceNext Lib "CDSDK.dll" (ByVal hEnum As
Integer, ByRef pSourceInfo As cdSourceInfo) As Integer

Private m_sourceInfo As cdSourceInfo
SourceInfo = New cdSourceInfo()
err = CDEnumDeviceNext(hEnum, SourceInfo)
I get a null-reference exception here.
Can someone guess as to what I'm doing wrong?


Try to add the MarshalAs attribute to the fixed strings:

Imports System.Runtime.InteropServices.

Private Structure cdSourceInfo
Dim SourceType As Integer
Dim rsrvd As Integer
<VBFixedString(64), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)>
Public Name As String
<VBFixedString(64),MarshalAs(UnmanagedType.ByValTStr,SizeConst:=64)>
Public NameInOS As String
Dim PortType As Integer
Dim u As cdPortDescripUnion
End Structure


BTW, why don't you use the upgrade wizard? It automatically added the
attributes.
 
Promblem is most likely the string members of the structure. You need to
initialize those. VBFixedString won't help you here.

-Rob Teixeira [MVP]
 
Rob, Armin, you were both right. It took both of your recommendations to
make the code work. To implement Rob's I added a method to initialize the
string to null, then pad it. I actually haven't checked if the padding is
needed.

Ken
 
Back
Top