16,554
社区成员
发帖
与我相关
我的任务
分享
Imports System.Runtime.InteropServices
Module Module1
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Structure Test
<VBFixedArray(8), MarshalAs(UnmanagedType.ByValArray, SizeConst:=9)> _
Dim id() As Byte
Dim test As Short
Dim test2 As Integer
End Structure
Sub testStr()
Dim t As New Test
ReDim t.id(8)
Console.WriteLine("size = {0}", Marshal.SizeOf(t))
Dim file_byte() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 1, 2, 3, 4}
Dim p2 As IntPtr = GCHandle.Alloc(file_byte, GCHandleType.Pinned).AddrOfPinnedObject()
t = Marshal.PtrToStructure(p2, t.GetType)
For i = 0 To t.id.Count - 1
If i = 0 Then
Console.Write("id() = {")
Else
Console.Write(",")
End If
Console.Write(t.id(i))
Next
Console.WriteLine("}")
Console.WriteLine("test = {0:X4}", t.test)
Console.WriteLine("test2 = {0:X8}", t.test2)
End Sub
Sub Main()
testStr()
Console.ReadLine()
End Sub
End Module
size = 15
id() = {1,2,3,4,5,6,7,8,9}
test = 0201
test2 = 04030201
Imports System.Runtime.InteropServices
Module Module1
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As IntPtr, _
ByVal pSrc As IntPtr, _
ByVal ByteLen As Integer)
Structure Test
Dim id() As Byte
Dim test As Short
Dim test2 As Integer
End Structure
Sub testStr()
Dim t As New Test
ReDim t.id(8)
Dim file_byte() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim p1 As IntPtr = GCHandle.Alloc(t.id, GCHandleType.Pinned).AddrOfPinnedObject()
Dim p2 As IntPtr = GCHandle.Alloc(file_byte, GCHandleType.Pinned).AddrOfPinnedObject()
CopyMemory(p1, p2, 9)
For i = 0 To t.id.Count - 1
If i = 0 Then
Console.Write("{")
Else
Console.Write(",")
End If
Console.Write(t.id(i))
Next
Console.WriteLine("}")
End Sub
Sub Main()
testStr()
Console.ReadLine()
End Sub
End Module
{1,2,3,4,5,6,7,8,9}