How To Use CoCreateGUID API to Generate a GUID with VB

Article translations Article translations
Article ID: 176790 - View products that this article applies to.
This article was previously published under Q176790
Expand all | Collapse all

On This Page

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.

Properties

Article ID: 176790 - Last Review: August 30, 2004 - Revision: 2.2
APPLIES TO
  • Microsoft Visual Basic 5.0 Control Creation Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
Keywords: 
kbhowto KB176790

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com