How To Use CoCreateGUID API to Generate a GUID with VB (176790)



The information in this article applies to:

  • Microsoft Visual Basic Control Creation Edition for Windows 5.0
  • Microsoft Visual Basic Learning Edition for Windows 5.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0

This article was previously published under Q176790

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 Windows 95, Windows 98, Windows Me, Windows NT and Windows 2000. 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:
    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
    					
  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.

Modification Type:MinorLast Reviewed:8/30/2004
Keywords:kbhowto KB176790