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 TypePrivate Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) As
LongPublic 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
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.
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 TypePrivate Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) As
LongPublic 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
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.
Private Type GUIDType
D1 As Long
D2 As Integer
D3 As Integer
D4(8) As Byte
End Type
Private Declare Function WinCoCreateGuid Lib "OLE32.DLL" Alias "CoCreateGuid" (g As GUIDType) As Long
Public Function CreateGUIDString() As String
Dim g As GUIDType
Dim sBuf As String
Call WinCoCreateGuid(g)
sBuf = PadZeros(Hex$(g.D1), 8, True) & _
PadZeros(Hex$(g.D2), 4, True) & _
PadZeros(Hex$(g.D3), 4, True) & _
PadZeros(Hex$(g.D4(0)), 2) & _
PadZeros(Hex$(g.D4(1)), 2, True) & _
PadZeros(Hex$(g.D4(2)), 2) & _
PadZeros(Hex$(g.D4(3)), 2) & _
PadZeros(Hex$(g.D4(4)), 2) & _
PadZeros(Hex$(g.D4(5)), 2) & _
PadZeros(Hex$(g.D4(6)), 2) & _
PadZeros(Hex$(g.D4(7)), 2)
CreateGUIDString = sBuf
End FunctionPrivate Function PadZeros(ByVal sBit As String, _
ByVal iStrLen As Integer, Optional bHyphen _
As Boolean) As String
If iStrLen > Len(sBit) Then
sBit = Right$(String$((iStrLen - Len(sBit)), _
"0") & sBit, iStrLen)
End If
If bHyphen Then sBit = sBit & "-"
PadZeros = sBit
End Function
Private Sub Command1_Click()
List1.AddItem CreateGUIDString
End Sub