redeagle wrote:
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 :)
--
Tom Shelton