HOWTO: Use CoCreateGUID API to Generate a GUID with VB

Last reviewed: November 17, 1997
Article ID: Q176790
The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.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 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. Note that calling GetGUID from code will now return a GUID.
Keywords          : vb432 VB4WIN vb5all vb5howto
Version           : WINDOWS:4.0,5.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.