다음을 통해 공유


방법: 특성을 사용하여 C/C++ 공용 구조체 만들기(Visual Basic)

특성을 사용하여 메모리에 구조체를 배치하는 방법을 사용자 지정할 수 있습니다. 예를 들어 C/C++에서 StructLayout(LayoutKind.Explicit)FieldOffset 속성을 사용하여 공용 구조체로 알려진 것을 만들 수 있습니다.

예제 1

이 코드 세그먼트에서는 TestUnion의 모든 필드가 메모리 내 동일한 위치에서 시작합니다.

' Add an Imports statement for System.Runtime.InteropServices.

<System.Runtime.InteropServices.StructLayout(
      System.Runtime.InteropServices.LayoutKind.Explicit)>
Structure TestUnion
    <System.Runtime.InteropServices.FieldOffset(0)>
    Public i As Integer

    <System.Runtime.InteropServices.FieldOffset(0)>
    Public d As Double

    <System.Runtime.InteropServices.FieldOffset(0)>
    Public c As Char

    <System.Runtime.InteropServices.FieldOffset(0)>
    Public b As Byte
End Structure

예제 2

다음은 명시적으로 설정된 다른 위치에서 필드가 시작되는 또 다른 예입니다.

' Add an Imports statement for System.Runtime.InteropServices.

 <System.Runtime.InteropServices.StructLayout(
      System.Runtime.InteropServices.LayoutKind.Explicit)>
Structure TestExplicit
     <System.Runtime.InteropServices.FieldOffset(0)>
     Public lg As Long

     <System.Runtime.InteropServices.FieldOffset(0)>
     Public i1 As Integer

     <System.Runtime.InteropServices.FieldOffset(4)>
     Public i2 As Integer

     <System.Runtime.InteropServices.FieldOffset(8)>
     Public d As Double

     <System.Runtime.InteropServices.FieldOffset(12)>
     Public c As Char

     <System.Runtime.InteropServices.FieldOffset(14)>
     Public b As Byte
 End Structure

두 정수 필드 i1i2같은 메모리 위치를 lg공유합니다. 구조체 레이아웃에 대한 이러한 종류의 제어는 플랫폼 호출을 사용할 때 유용합니다.

참고하십시오