0

我正在开发一个 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

我需要重写代码还是可以使用其他功能?谢谢你。

4

1 回答 1

1

您需要首先确定哪些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声明以期望指针已经转换为 aLong并复制到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复制到 a 的声明。Longguid

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.

于 2013-07-31T05:38:52.457 回答