例如,
对于整数,我们分配一些内存并获取其地址:
Dim MyPointer As IntPtr = Marshal.AllocHGlobal(4)
Marshal.WriteInt32(MyPointer, 255)
For String,
Dim MyStrPointer As IntPtr = Marshal.StringToHGlobalAuto("Hello World")
同样,我想为无符号整数(UInt32)
分配内存答案 0 :(得分:0)
与其他.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)