Public Function VarPtr(ByVal obj As Object) As Integer
Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(obj, Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return ret
End Function
Public Function StrPtr(ByVal str As String) As Integer
Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(str, Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return ret
End Function
博客展示了.NET中两个函数的实现代码。分别是VarPtr函数,用于获取Object对象指针;StrPtr函数,用于获取String对象指针。通过GCHandle固定对象并获取其地址,最后释放GCHandle并返回地址的整数值。
842

被折叠的 条评论
为什么被折叠?



