Create Your Own GUID

Well Visual Basic 5.0 comes with a program that create them for you, but what a pain. Wouldn’t it be nice to just do it via code? Well now you can with the sample code below.


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
Private Declare Function StringFromGUID2 Lib "ole32.dll" 
(rguid As Any, ByVal lpstrClsId As Long, ByVal cbMax As Long) As Long


Public Function GetGUID() As String
Dim pudtGUID As GUID
Dim pstrGUID As String
Dim pbytGUID() As Byte
Dim plngRet As Long
Dim plngLen As Long
    plngLen = 40
    pbytGUID = String(plngLen, 0)
    CoCreateGuid pudtGUID
    plngRet = StringFromGUID2(pudtGUID, VarPtr(pbytGUID(0)), plngLen)
    pstrGUID = pbytGUID
    If (Asc(Mid$(pstrGUID, plngRet, 1)) = 0) Then plngRet = plngRet - 1
    GetGUID = Left(pstrGUID, plngRet)
End Function


Tip Submitted By: David McCarter/Woody Pewitt