Re: Converting VB6 Structures to .NET



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

"Tom Shelton" wrote:


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


.



Relevant Pages

  • Re: Unsigned 32 bit
    ... This helper dll has the base adress 0x02400000. ... Its equivalent VB declare is ... Dim MyVar As Long, AdressOfMyVar1 As Long, AdressOfMyVar2 As Long ... Both project internal adresses are inside the jump table VB has build. ...
    (microsoft.public.vb.general.discussion)
  • Re: Unsigned 32 bit
    ... > For this helper dll I wrote a typelib in IDL called ukVBHLB.tlb. ... Its equivalent VB declare is ... > Both project internal adresses are inside the jump table VB has build. ...
    (microsoft.public.vb.general.discussion)
  • Re: Exported function mangaled name
    ... that header file; it must be declared as __declspec ... You have to declare the function the same way in your .cpp file as in ... #define LIBSPEC __declspec ... See my essay on The Ultimate DLL Header File on my MVP Tips site. ...
    (microsoft.public.vc.mfc)
  • Re: D3 odbc and .Net, almost there!
    ... that he was passing values ByVal instead of ByRef in his VB.NET code, ... The DLL itself follows well known standards ... as the parameters are correct it will work because the standards for calling ... there is no reason that code shouldn't work properly. ...
    (comp.databases.pick)
  • Re: VB6 Error: Bad DLL calling convention
    ... that is done as I was not doing it in my dll code. ... This method of changing the Calling Convention best suits me as I can use ... Declare the function in the DLL as (note the absence of the decorated ... Dim ret As Long ...
    (microsoft.public.vb.general.discussion)