加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 07:51:47 所属栏目:百科 来源:网络整理
导读: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,必须传递类

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 ; 但是,它可对其进行修改以根据需要添加连字符:

步骤按步骤示例

  1. 为新的 vba 项目中添加一个标准模块。默认情况下创建 Form1。
  2. 将下面的代码粘贴到代码的模块:
    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),"") & Hex$(udtGUID.Data4(1)) & _
    IIf((udtGUID.Data4(2) < &H10),"") & Hex$(udtGUID.Data4(2)) & _
    IIf((udtGUID.Data4(3) < &H10),"") & Hex$(udtGUID.Data4(3)) & _
    IIf((udtGUID.Data4(4) < &H10),"") & Hex$(udtGUID.Data4(4)) & _
    IIf((udtGUID.Data4(5) < &H10),"") & Hex$(udtGUID.Data4(5)) & _
    IIf((udtGUID.Data4(6) < &H10),"") & Hex$(udtGUID.Data4(6)) & _
    IIf((udtGUID.Data4(7) < &H10),"") & Hex$(udtGUID.Data4(7))
    End If
    
    End Function
    					
  3. 将命令按钮添加到该的表单并将下面的代码添加到窗体:
    Private Sub Command1_Click() MsgBox GetGuid End Sub
  4. 按 f5 键运行该的项目并单击命令按钮。
RESULT: 一个 GUID 生成并在 MessageBox 中所示。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读