如何在 Excel 中生成版本 5 UUID

时间:2021-02-02 18:46:28

标签: excel vba uuid

以下代码生成版本 4 UUID。我需要将其转换为生成第 5 版,但找不到有关使用 Microsoft ole32.dll CoCreateGuid 函数生成第 5 版 UUID 的任何明确文档:

Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (guid As GUID_TYPE) As LongPtr
Private Declare PtrSafe Function StringFromGUID2 Lib "ole32.dll" (guid As GUID_TYPE, ByVal lpStrGuid As LongPtr, ByVal cbMax As Long) As LongPtr

Private Type GUID_TYPE
                Data1 As Long
                Data2 As Integer
                Data3 As Integer
                Data4(7) As Byte
End Type

Public Function GetGUID() As String
    Dim guid As GUID_TYPE
    Dim strGuid As String
    Dim retValue As LongPtr
    Const guidLength As Long = 39 'registry GUID format with null terminator {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
    
    retValue = CoCreateGuid(guid)
    If retValue = 0 Then
      strGuid = String$(guidLength, vbNullChar)
      retValue = StringFromGUID2(guid, StrPtr(strGuid), guidLength)
      If retValue = guidLength Then
         ' valid GUID as a string
         GetGUID = Mid$(strGuid, 2, 36)
      End If
    End If
End Function

0 个答案:

没有答案
相关问题