HOWTO: Use CoCreateGUID API to Generate a GUID with VB

ID: Q176790


The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0


SUMMARY

As a programmer, you may need to generate GUIDs (Globally Unique Identifiers) for various purposes. This article describes how to generate a GUID in Visual Basic using the CoCreateGuid API.

NOTE: The code in this article is not intended and cannot be used to create or change a GUID automatically generated by Visual Basic for custom ActiveX components. GUIDs automatically generated by Visual Basic cannot be altered.


MORE INFORMATION

The code below can be used to create a GUID in Visual Basic. The code calls the CoCreateGuid API found in OLE32.DLL on both Windows 95, Windows 98, and Windows NT. In order to call the API correctly, a variable of type GUID must be passed. This code creates a custom type, named GUID, with four parts that represent the individual parts separated by dashes that you would see when viewing a CLSID or GUID in the system registry. This code simply returns a GUID; however, it can be modified to add the dashes if desired:

Step By Step Example

  1. Add a standard module to a new Visual Basic project. Form1 is created by default.


  2. Paste the code below into the code module:
    
          Option Explicit
    
          Type GUID
             Data1 As Long
             Data2 As Integer
             Data3 As Integer
             Data4(7) As Byte
          End Type
    
          Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) _
                  As Long
    
          Global Const S_OK = 0 ' return value from CoCreateGuid
    
          Function GetGUID() As String
    
            Dim lResult As Long
            Dim lguid As GUID
            Dim MyguidString As String
            Dim MyGuidString1 As String
            Dim MyGuidString2 As String
            Dim MyGuidString3 As String
            Dim DataLen As Integer
            Dim StringLen As Integer
            Dim i%
    
            On Error GoTo error_olemsg
    
            lResult = CoCreateGuid(lguid)
    
            If lResult = S_OK Then
    
               MyGuidString1 = Hex$(lguid.Data1)
               StringLen = Len(MyGuidString1)
               DataLen = Len(lguid.Data1)
               MyGuidString1 = LeadingZeros(2 * DataLen, StringLen) _
                  & MyGuidString1 'First 4 bytes (8 hex digits)
    
               MyGuidString2 = Hex$(lguid.Data2)
               StringLen = Len(MyGuidString2)
               DataLen = Len(lguid.Data2)
               MyGuidString2 = LeadingZeros(2 * DataLen, StringLen) _
                  & Trim$(MyGuidString2) 'Next 2 bytes (4 hex digits)
    
               MyGuidString3 = Hex$(lguid.Data3)
               StringLen = Len(MyGuidString3)
               DataLen = Len(lguid.Data3)
               MyGuidString3 = LeadingZeros(2 * DataLen, StringLen) _
                  & Trim$(MyGuidString3) 'Next 2 bytes (4 hex digits)
    
               GetGUID = _
                  MyGuidString1 & MyGuidString2 & MyGuidString3
    
               For i% = 0 To 7
                  MyguidString = MyguidString & _
                       Format$(Hex$(lguid.Data4(i%)), "00")
               Next i%
    
               'MyGuidString contains last 8 bytes of Guid (16 hex digits)
               GetGUID = GetGUID & MyguidString
    
            Else
               GetGUID = "00000000" ' return zeros if function unsuccessful
            End If
    
            Exit Function
    
          error_olemsg:
             MsgBox "Error " & Str(Err) & ": " & Error$(Err)
             GetGUID = "00000000"
             Exit Function
    
          End Function
    
          Function LeadingZeros(ExpectedLen As Integer, ActualLen As Integer) _
              As String
             LeadingZeros = String$(ExpectedLen - ActualLen, "0")
          End Function
     


  3. Add a Command Button to the form, and add the following code to the form:
    
          Private Sub Command1_Click()
               MsgBox GetGuid
          End Sub
     


  4. Press F5 to run the project, and click the Command Button.


RESULT: A GUID is generated and shown within a MessageBox.

Additional query words: kbdss kbDSupport kbVBp kbVBp400 kbVBp500 kbVBp600 kbActiveX

Keywords : kbnokeyword kbVBp400 kbVBp500 kbVBp600 kbGrpVB
Version :
Platform :
Issue type : kbhowto


Last Reviewed: January 5, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.