您的位置:首页 > 产品设计 > UI/UE

如何使用 CoCreateGUID API 生成与 VB 6 的 GUID

2014-10-09 09:18 651 查看
http://support.microsoft.com/kb/176790/zh-cn

下面的代码可用于创建在 Visual Basic 中的 GUID。该代码调用 CoCreateGuid API OLE32.DLL 上 Windows 95、 Windows 98、 Windows Me、 Windows NT 和 Windows
2000 中找到。若要正确地调用了 API,必须传递类型 GUID 的变量。此代码创建名 GUID,为与代表各个部分查看 CLSID 或 GUID,在系统注册表中时,您将看到的短划线分隔的四个部分的自定义类型。此代码只是返回一个 GUID ; 但是,它可对其进行修改以根据需要添加连字符:


步骤按步骤示例

为新的 vba 项目中添加一个标准模块。默认情况下创建 Form1。
将下面的代码粘贴到代码的模块:


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

Private Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) As
Long

Public Function GetGUID() As String
'(c) 2000 Gus Molina

Dim udtGUID As GUID

If (CoCreateGuid(udtGUID) = 0) Then

GetGUID = _
String(8 - Len(Hex$(udtGUID.Data1)), "0") & Hex$(udtGUID.Data1) & _
String(4 - Len(Hex$(udtGUID.Data2)), "0") & Hex$(udtGUID.Data2) & _
String(4 - Len(Hex$(udtGUID.Data3)), "0") & Hex$(udtGUID.Data3) & _
IIf((udtGUID.Data4(0) < &H10), "0", "") & Hex$(udtGUID.Data4(0)) & _
IIf((udtGUID.Data4(1) < &H10), "0", "") & Hex$(udtGUID.Data4(1)) & _
IIf((udtGUID.Data4(2) < &H10), "0", "") & Hex$(udtGUID.Data4(2)) & _
IIf((udtGUID.Data4(3) < &H10), "0", "") & Hex$(udtGUID.Data4(3)) & _
IIf((udtGUID.Data4(4) < &H10), "0", "") & Hex$(udtGUID.Data4(4)) & _
IIf((udtGUID.Data4(5) < &H10), "0", "") & Hex$(udtGUID.Data4(5)) & _
IIf((udtGUID.Data4(6) < &H10), "0", "") & Hex$(udtGUID.Data4(6)) & _
IIf((udtGUID.Data4(7) < &H10), "0", "") & Hex$(udtGUID.Data4(7))
End If

End Function


将命令按钮添加到该的表单并将下面的代码添加到窗体:


Private Sub Command1_Click()
MsgBox GetGuid
End Sub


按 f5 键运行该的项目并单击命令按钮。

RESULT: 一个 GUID 生成并在 MessageBox 中所示。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: