Vb.net 在Visual Basic.NET中为无符号整数分配物理内存?

Vb.net 在Visual Basic.NET中为无符号整数分配物理内存?,vb.net,Vb.net,比如说, 对于integer,我们分配一些内存并获取其地址: Dim MyPointer As IntPtr = Marshal.AllocHGlobal(4) Marshal.WriteInt32(MyPointer, 255) 对于字符串 Dim MyStrPointer As IntPtr = Marshal.StringToHGlobalAuto("Hello World") 类似地,我希望为无符号整数(UInt32)分配内存。无符号整数与其他.Net数值类型一样,都是结构,因

比如说,

对于integer,我们分配一些内存并获取其地址:

Dim MyPointer As IntPtr =   Marshal.AllocHGlobal(4)

Marshal.WriteInt32(MyPointer, 255)
对于字符串

Dim MyStrPointer As IntPtr = Marshal.StringToHGlobalAuto("Hello World")

类似地,我希望为无符号整数(UInt32)分配内存。

无符号整数与其他.Net数值类型一样,都是结构,因此可以使用Marshal.StructureToPtr和Marshal.PtrToStructure封送

Dim origUI32 As UInt32 = UInt32.MaxValue
Dim MyPointer As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(origUI32))
Marshal.StructureToPtr(origUI32, MyPointer, True)

Dim retrievedUI32 As UInt32 = DirectCast(Marshal.PtrToStructure(MyPointer, GetType(UInt32)), UInt32)
Marshal.FreeHGlobal(MyPointer)
Debug.Assert(retrievedUI32 = origUI32) 

与其他.Net数值类型一样,无符号整数也是结构,因此可以使用Marshal.StructureToPtr和Marshal.PtrToStructure封送

Dim origUI32 As UInt32 = UInt32.MaxValue
Dim MyPointer As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(origUI32))
Marshal.StructureToPtr(origUI32, MyPointer, True)

Dim retrievedUI32 As UInt32 = DirectCast(Marshal.PtrToStructure(MyPointer, GetType(UInt32)), UInt32)
Marshal.FreeHGlobal(MyPointer)
Debug.Assert(retrievedUI32 = origUI32)