Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Za pomocą atrybutów można dostosować sposób stosowania struktur w pamięci. Na przykład można utworzyć tzw. unię w języku C/C++ przy użyciu atrybutów StructLayout(LayoutKind.Explicit) i FieldOffset.
Przykład 1
W tym segmencie kodu wszystkie pola TestUnion znajdują się w tej samej lokalizacji w pamięci.
' 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
Przykład 2
Poniżej przedstawiono inny przykład, w którym pola zaczynają się od różnych jawnie ustawionych lokalizacji.
' 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
Dwa pola i1 liczb całkowitych i i2, współdzielą te same lokalizacje pamięci co lg. Ten rodzaj kontroli nad układem struktury jest przydatny podczas korzystania z wywołania platformy.