我正在开发一个VBA程序,我在VB.NET中有以下代码:
Private Function SYS_GuidPtrToGuid(ByVal p As System.IntPtr) As System.Guid
Dim b(15) As Byte
Dim myGuid As System.Guid
System.Runtime.InteropServices.Marshal.Copy(p, b, 0, b.Length)
myGuid = New System.Guid(b)
Erase b
Return myGuid
End Function
我是否需要重写代码或是否还有其他可以使用的功能?谢谢。
答案 0 :(得分:0)
您需要首先确定哪些Guid
类可用,以及它们是否提供了从指针接收GUID的方法,但是可以使用Byte
数组,例如。 NET Guid
,然后您需要使用
Marshal.Copy
来电转换为MemCpy
来电
Declare Sub MemCpy Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination() As Byte, ByVal SourcePtr As Long, ByVal Length As Long)
注意我已调整MemCpy
声明以期望指针已转换为Long
并复制到Byte
数组。
所以,您的代码可能是(但请记住我还没有检查过哪些Guid
类可用):
Private Function SYS_GuidPtrToGuid(ByVal p As Long) As Guid
Dim b(15) As Byte
MemCpy b, p, UBound(b) - LBound(b) + 1
SYS_GuidPtrToGuid = New Guid(b)
End Function
根据您的评论,这是一个MemCpy
声明,可以从Long
指针直接复制到guid
。
Declare Sub MemCpy Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As GUID, ByVal SourcePtr As Long, ByVal Length As Long)
现在电话很简单:
Private Function SYS_GuidPtrToGuid(ByVal p As Long) As Guid
MemCpy SYS_GuidPtrToGuid, p, 16
End Function
这取决于guid
包装是否正确,但这是Type
的重点。