Having problem with StructLayout & MarshalAs in Beta 2


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Having problem with StructLayout & MarshalAs in Beta 2

  1. #1
    Patrick Ireland Guest

    Having problem with StructLayout & MarshalAs in Beta 2


    Well I finally got my SCSI DVD drive installed. Then I went through
    the mental cruelity of install the Beta 2 on my WIN2K Pro system. Oh
    happy thoughts 8-), let's jump right in and try to run one of my Beta 1
    programs. What a cruel trick 8-(. The use of StructLayout and
    MarshalAs now gives me compiler errors with code that worked in Beta 1.
    I
    guess that's why they call the blues (sorry, Beta). Any suggestions??

    '
    ' Beta 2 documentation on structure definitions:
    '

    [ <attrlist> ] [{ Public | Protected | Friend | Protected Friend | Private
    }] Structure name
    [ variabledeclarations ]
    [ proceduredeclarations ]
    End Structure

    '
    ' VB Program:
    '

    Imports System
    Imports System.Text
    Imports System.Console
    Imports System.Runtime.InteropServices.StructLayoutAttribute

    Module Module1
    Sub Main()
    End Sub
    End Module

    Public Class Class1
    <StructLayout(LayoutKind.Sequential)> Public Structure Record1
    Public Field1 As Integer
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _
    Public Field2 () As Byte
    End Structure
    End Class

    '
    'Works in Beta1
    '
    'Public Class Class1
    ' Public Structure <StructLayout(LayoutKind.Sequential)> Record1
    ' Public Field1 As Integer
    ' Public <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _
    ' Field2 () As Byte
    ' End Structure
    'End Class
    '

    ' Note:
    ' The ordering of the <StructLayout...> and <MarshalAs...> differ in
    ' what works in Beta1 and what the documentation states in Beta 2

  2. #2
    Mattias Sjögren Guest

    Re: Having problem with StructLayout & MarshalAs in Beta 2

    Hi Patrick,

    >Imports System
    >Imports System.Text
    >Imports System.Console
    >Imports System.Runtime.InteropServices.StructLayoutAttribute


    What you're missing is an Import for just the
    System.Runtime.InteropServices namespace. Perhaps that's what you
    wanted on the last line, because there's no reason that I can see to
    Import StructLayoutAttribute (or any other attribute type).


    Mattias

    ===
    Mattias Sjögren (VB MVP)
    mattias @ mvps.org
    http://www.msjogren.net/dotnet/

  3. #3
    Patrick Ireland Guest

    Re: Having problem with StructLayout & MarshalAs in Beta 2


    Mattias,

    Thanks, that was a combination I had not tried yet and it does work.
    Looking at the Beta1 vs Beta2 code, the order placement of the attribute
    clause appears to have changed. Or I suffering under bleary eyed, fingers
    typed to the bone delusions?

    Code I used in Beta 1

    Public Structure <StructLayout(LayoutKind.Sequential)> StrucName ...

    whereas Beta 2 requires

    <StructLayout(LayoutKind.Sequential)> Public Structure StrucName ...

    mattias.dont.want.spam@mvps.org (Mattias Sjögren) wrote:
    >Hi Patrick,
    >
    >>Imports System
    >>Imports System.Text
    >>Imports System.Console
    >>Imports System.Runtime.InteropServices.StructLayoutAttribute

    >
    >What you're missing is an Import for just the
    >System.Runtime.InteropServices namespace. Perhaps that's what you
    >wanted on the last line, because there's no reason that I can see to
    >Import StructLayoutAttribute (or any other attribute type).
    >
    >
    >Mattias
    >
    >===
    >Mattias Sjögren (VB MVP)
    >mattias @ mvps.org
    >http://www.msjogren.net/dotnet/



  4. #4
    Rob Teixeira Guest

    Re: Having problem with StructLayout & MarshalAs in Beta 2


    "Patrick Ireland" <ireland@airmail.net> wrote:
    >
    >Mattias,
    >
    >Thanks, that was a combination I had not tried yet and it does work.
    >Looking at the Beta1 vs Beta2 code, the order placement of the attribute
    >clause appears to have changed. Or I suffering under bleary eyed, fingers
    >typed to the bone delusions?
    >


    It has changed.
    The attribute placement is more consistant now.

    -Rob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center