Converting VB6 Structures to .NET

G

Guest

In VB6, the code for a structure is

Structure zFuheader
Dim Id1(80) As Byte
Dim Id2(80) As Byte
End Structure

However, in .NET, it doesn't let you declare the array size in the
structure.

I am trying to use an old external .dll that worked with the VB6 structure
syntax but doesn't appear to be working when I try and do it in .NET.

The problem in a little more detail:

I am using a number of "Declare Function" statments to access the functions
in the .dll. I am getting an error when I try to pass the structure to one
of the functions in the .dll. (Header Pointer is Null or somthing like that).

Module MainMod

Declare Function initFuHeader Lib "mtsadfconv.dll" Alias
"_initFuHeader_VB@4" _ (ByVal zHeader As zFuheader) As Integer

Public Structure zFuheader
Dim Id1() As Byte 'in VB6 declared to size 80
Dim Id2() As Byte 'in VB6 declared to size 80
End Structure

Public Sub Main()
Dim zHead As zFuheader
Dim result As Integer

result = initFuHeader(zHead) '<-- This returns a "result" that Header
Pointer is Null

End Sub

End Module

So I'm wondering if the error is because the structure doesn't declare a
size for the arrays??? If so, does anyone know a work around?

John
 
S

Steve Long

I wonder if this will work:

Dim zHead As zFuheader

redim zHead.Id1(79)

Just a thought.
Steve
 
G

Guest

Hi Steve-

Thanks for the reply. Unfortunatly it does not. Although in the interest
of complete disclosure, the actual structure I am trying to pass (zFuheader)
references another structure that also has an array.

Structure zChType
Dim Ref() As Byte '8
Dim Res() As Byte '8
End Structure

Structure zFuheader
Dim Id1() As Byte '20
Dim Id2() As Byte '20
Dim ch() As zChType
End Structure
 
T

Tom Shelton

redeagle said:
In VB6, the code for a structure is

Structure zFuheader
Dim Id1(80) As Byte
Dim Id2(80) As Byte
End Structure

However, in .NET, it doesn't let you declare the array size in the
structure.

I am trying to use an old external .dll that worked with the VB6 structure
syntax but doesn't appear to be working when I try and do it in .NET.

The problem in a little more detail:

I am using a number of "Declare Function" statments to access the functions
in the .dll. I am getting an error when I try to pass the structure to one
of the functions in the .dll. (Header Pointer is Null or somthing like that).

Module MainMod

Declare Function initFuHeader Lib "mtsadfconv.dll" Alias
"_initFuHeader_VB@4" _ (ByVal zHeader As zFuheader) As Integer

Public Structure zFuheader
Dim Id1() As Byte 'in VB6 declared to size 80
Dim Id2() As Byte 'in VB6 declared to size 80
End Structure

Public Sub Main()
Dim zHead As zFuheader
Dim result As Integer

result = initFuHeader(zHead) '<-- This returns a "result" that Header
Pointer is Null

End Sub

End Module

So I'm wondering if the error is because the structure doesn't declare a
size for the arrays??? If so, does anyone know a work around?

John

Ok... Your dealing with interop, so things are little different. You
have to add marshalling attributes to this structure, so that the
runtime marshaller knows how to pass it to the dll call. Here is what
my first stab would be:
Declare Function initFuHeader Lib "mtsadfconv.dll" Alias _
"_initFuHeader_VB@4" _ > (ByVal zHeader As zFuheader) As Integer

I have to question this declare a little bit. First question I have
to ask - is this the original VB6 or declare? Because if it is, the As
Integer should be As Short. VB.NET changed the size of the data types.
A Integer in .NET is 32-bit, not 16-bit (though, this would probably
not cause you the error your receiving - though it might give
unexpected return results). Further, is this function actually
updating this structure? Because if it is, then you should probably be
passing it ByRef rather then ByVal. That would most likely cause the
error your receiving...
Public Structure zFuheader
Dim Id1() As Byte 'in VB6 declared to size 80
Dim Id2() As Byte 'in VB6 declared to size 80
End Structure

I would probably declare this as:

<StructLayout (LayoutKind.Sequential)> _ ' not strictly necessary
Public Structure zFuheader
<MarshalAs (UnmanagedType.ByValArray, SizeConst:=80)> _
Public Id1() As Byte 'in VB6 declared to size 80

<MarshalAs (UnmanagedType.ByValArray, SizeConst:=80)> _
Public Id2() As Byte 'in VB6 declared to size 80
End Structure

Anyway, give it a go, and let us know what happens :)
 
G

Guest

Hi Tom-

Thanks for the tips.

In VB6 the original declare was "As Long" so I changed it to "As Integer" in
..NET.
Further, is this function actually
updating this structure? Because if it is, then you should probably be
passing it ByRef rather then ByVal. That would most likely cause the
error your receiving...

I actually don't know what its doing. This dll was written by a third party
software vendor for its customers to use to interact with its proprietary
file types. The dll was written in VB6 and they released a "user guide" for
the dll. In the user guide, their example uses ByVal. Maybe it is a typo.
I will try ByRef.

I will also add the Marshalling attributes.

I'll keep let you know how it goes...

John
 
G

Guest

You are the MAN!

I added the Marshalling attributes and changed the "Function Declare"
statements from "ByVal" to "ByRef" (that is apparantly a typo throughout the
"user guide").

Everything seems to be working now!

Thanks again Tom (and Steve) I appreciate your time.

John
 

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