You are currently offline, waiting for your internet to reconnect

How To Use CoCreateGUID API to Generate a GUID with VB

This article was previously published under Q176790
SUMMARY
As a programmer, you may need to generate GUIDs (Globally UniqueIdentifiers) for various purposes. This article describes how to generate aGUID in Visual Basic using the CoCreateGuid API.

NOTE: The code in this article is not intended and cannot be used to createor change a GUID automatically generated by Visual Basic for custom ActiveXcomponents. GUIDs automatically generated by Visual Basic cannot bealtered.
MORE INFORMATION
The code below can be used to create a GUID in Visual Basic. The code callsthe 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 fourparts that represent the individual parts separated by dashes that youwould see when viewing a CLSID or GUID in the system registry. This codesimply returns a GUID; however, it can be modified to add the dashes ifdesired:

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 GUIDData1 As LongData2 As IntegerData3 As IntegerData4(7) As ByteEnd TypePrivate Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) AsLongPublic Function GetGUID() As String'(c) 2000 Gus MolinaDim udtGUID As GUIDIf (CoCreateGuid(udtGUID) = 0) ThenGetGUID = _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 IfEnd 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: 08/30/2004 22:09:00 - Revision: 2.2

  • 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
  • kbhowto KB176790
Feedback