Results 1 to 4 of 4

Thread: upgrade vb6 to vb.net execution engine exception thrown

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    1

    upgrade vb6 to vb.net execution engine exception thrown

    Hi there, this is a bit of a beefy problem that i've been having with some code with several tendrils which will need to be loped off.

    I am in the process of upgrading a software development kit from vb6 to vb.net. When i run the program i get an executionengineexception when i call the function gen2multitagidentify.

    I initialize the structure array IDNUM which is one of the arguments of the function. This is on the advice of the upgrade engine.

    Code:
     Dim IDNUM(100) As TagIds
            Dim i As Integer
            For i = 0 To 100
                IDNUM(i).Initialize()
            Next
            Dim FFZ As Short
            Dim CountID As Integer
            Try
                FFZ = Gen2MultiTagIdentify(hCom, CountID, IDNUM(0), 255)

    As you can see it takes an array of structures which is where i think the problem could be. Here is my structure declaration with the appropriate marshalling attributes.

    Code:
     <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
        Public Structure TagIds
            Dim TagType As Byte
            Dim AntNum As Byte
            <VBFixedArray(11)> Dim Ids() As Byte
    
            Public Sub Initialize()
                ReDim Ids(11)
            End Sub
        End Structure
    The gen2multitagidentify function is defined in a .dll file here with a marshalling attribute supplied for the structure:

    Code:
         Public Declare Function Gen2MultiTagIdentify Lib "\\SBS-CRM\Company\Project\RFID\visual_studio\DEMO VB Source\Mr915ApiV10.dll" (ByVal hCom As Integer, ByRef Count As Integer, <MarshalAs(UnmanagedType.Struct)> ByRef Value As TagIds, ByVal NetAddr As Byte) As Short
    Can anyone see where i've gone wrong? The execution engine exception as listed in the msdn glossary provides an incredibly vague and unhelpful description of its causes. I've been working on this problem for 3 days now with no luck. Any help would be much appreicated.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: upgrade vb6 to vb.net execution engine exception thrown

    I don't see why you need Initialize in your structure. Try removing it completely.
    If nothing helps you can also pass a 14 byte array to your function - this is the simplest of forms so it should work.
    How the code of the structure looked like in VB6?

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: upgrade vb6 to vb.net execution engine exception thrown

    No, you do need to Initialize your structure, or rather you do need to redim the size of the Ids array.
    What you're trying to do is to pass an unmanaged array by passing the first element of that array. In the unmanaged world this is correct since an array is a pointer to the first element. This was also the only way of doing it in VB6 since it used SAFEARRAYs internally.

    As you've seen this does not work in the .Net world and the reason for that is that you're telling the marshaller that you only want to marshal one element. So you get the exception because the function expects an array but only gets a single element. Try changing the declaration of the argument to this:
    <MarshalAs(UnmanagedType.Struct), Out()> ByVal Value As TagIds()
    and pass the whole array instead of a single element. Please note that I changed it to ByVal and added the OutAttribute instead.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: upgrade vb6 to vb.net execution engine exception thrown

    I would also suggest reconsidering the design of the structure. All you appear to have is a fixed size array of bytes. The other members are also bytes, so the whole thing could be turned into a slightly different design:

    Code:
    Public Structure TagIds
      Private bts(12) as Byte
      
      Public Property TagType as Byte
       Get
         Return bts(0)
       End Get
       Set (value as Byte)
        bts(0) = value
       End Set
     End Property
    
     Public Property AntNm as Byte
       Get
         Return bts(1)
       End Get
       Set (value as Byte)
        bts(1) = value
       End Set
     End Property
    
    End Structure
    I left one thing out of that design, which is the Ids concept. It could be a third property that returns or sets the remaining bytes in the array, which would be similar to the way you have the data, but based on the name you used, I wondered if there wasn't a better solution than that. The name makes it sound like it is a series of ID items stuck together. Is each byte a different ID? If so, you could add a property that takes an Integer which indexes into the array, and the code would use the whole thing as if each individual item was a different member thought they were actually a single array of bytes. Alternatively, you could do both, with properties that returned individual bytes along with properties that returned chunks of bytes, or even the whole array.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width