Microsoft KB Archive/176790

= How To Use CoCreateGUID API to Generate a GUID with VB =

Article ID: 176790

Article Last Modified on 8/30/2004

-

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

-



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
 Add a standard module to a new Visual Basic project. Form1 is created by default.  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   Add a Command Button to the form, and add the following code to the form: Private Sub Command1_Click MsgBox GetGuid End Sub  Press F5 to run the project, and click the Command Button.

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

Keywords: kbhowto KB176790

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.