none
M<arshaling under vb 2010. Getting error. RRS feed

  • Question

  • In short I'm trying to get code that works in C# ported over to VB. THe orignal code is as follows:

    C#:

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
        private struct FieldDescriptor
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
            public string fieldName;
            public char fieldType;
            public Int32 address;
            public byte fieldLen;
            public byte count;
            public Int16 reserved1;
            public byte workArea;
            public Int16 reserved2;
            public byte flag;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)]
            public byte[] reserved3;
            public byte indexFlag;
        }
    
    
    //Later in the code THis line exists:
    
                    buffer = br.ReadBytes(Marshal.SizeOf(typeof(FieldDescriptor)));
    

     

    That works fine.

    THe VB version is:

      <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, pack:=1)>
       Public Structure FieldDescriptor
        <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=11)>
        Public fieldname As String
        Public fieldType As Char
        Public address As Int32
        Public fieldLen As Byte
        Public count As Byte
        Public reserved1 As Int16
        Public workArea As Byte
        Public reserved2 As Int16
        Public flag As Byte
        <MarshalAs(UnmanagedType.ByValTStr, Sizeconst:=7)>
        Public reserved3 As Byte()
        Public indexFlag As Byte
      End Structure
    
    'Later in the code:
    
            buffer = br.ReadBytes(Marshal.SizeOf(GetType(FieldDescriptor)))
    

    THe VB version gives the following runtime error:

    System.ArgumentException was unhandled
      Message=Type 'TestDBF.clsDBF2Net+FieldDescriptor' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.

    Any Ideas as why?  I must port to VB.

    • Edited by Mudoch2505 Tuesday, October 18, 2011 8:56 PM
    Tuesday, October 18, 2011 8:49 PM

Answers

  • Works fine in VB2008E with continuation characters:

    Imports System.Runtime.InteropServices
    Public Class Form1
      <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
        
    Private Structure FieldDescriptor
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> _
        
    Public fieldName As String
        Public fieldType As Char
        Public address As Int32
        
    Public fieldLen As Byte
        Public count As Byte
        Public reserved1 As Int16
        
    Public workArea As Byte
        Public reserved2 As Int16
        
    Public flag As Byte
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=7)> _
        
    Public reserved3() As Byte
        Public indexFlag As Byte
      End Structure
      Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        
    Dim lenFieldDescriptor As Integer = Marshal.SizeOf(GetType(FieldDescriptor))
      
    End Sub
    End
     Class

    • Marked as answer by Mudoch2505 Wednesday, October 19, 2011 1:26 AM
    Tuesday, October 18, 2011 10:45 PM

All replies

  • Works fine in VB2008E with continuation characters:

    Imports System.Runtime.InteropServices
    Public Class Form1
      <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
        
    Private Structure FieldDescriptor
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> _
        
    Public fieldName As String
        Public fieldType As Char
        Public address As Int32
        
    Public fieldLen As Byte
        Public count As Byte
        Public reserved1 As Int16
        
    Public workArea As Byte
        Public reserved2 As Int16
        
    Public flag As Byte
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=7)> _
        
    Public reserved3() As Byte
        Public indexFlag As Byte
      End Structure
      Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        
    Dim lenFieldDescriptor As Integer = Marshal.SizeOf(GetType(FieldDescriptor))
      
    End Sub
    End
     Class

    • Marked as answer by Mudoch2505 Wednesday, October 19, 2011 1:26 AM
    Tuesday, October 18, 2011 10:45 PM
  • Thanks, it actually was the

    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=7)> 

    Public reserved3() As Byte

    that fixed it.  I thought I had that muffed up.  This marshalling stuff is new to me.  Thanks again.

    Wednesday, October 19, 2011 1:28 AM