VB6 -> VB.Net

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
 
A

Armin Zingler

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.
 
R

Rob Teixeira [MVP]

Promblem is most likely the string members of the structure. You need to
initialize those. VBFixedString won't help you here.

-Rob Teixeira [MVP]
 
K

Ken Kast

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top