Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a new GUID in VB. 1

Status
Not open for further replies.

JNeave

Programmer
Jan 28, 2002
125
GB
Hi,

I can already do this using
Code:
UUIDGEN.EXE -oNewGUID.txt
and readin that file into VB.

But for the sake of elegance, does anybody know how to create one in VB?

I can't declare CoCreateGUID in VB, probably because VB doesn't support C structures.. :(

Cheers,

James.
 
Yes, you can call CoCreateGuid:
Code:
Private Declare Function CoCreateGuid Lib "ole32" (id As Any) As Long
Private Sub Form_Load()
    MsgBox "Generated GUID: " + CreateGUID
End Sub
Public Function CreateGUID() As String
    Dim id(0 To 15) As Byte
    Dim Cnt As Long, GUID As String
    If CoCreateGuid(id(0)) = 0 Then
        For Cnt = 0 To 15
            CreateGUID = CreateGUID + IIf(id(Cnt) < 16, &quot;0&quot;, &quot;&quot;) + Hex$(id(Cnt))
        Next Cnt
        CreateGUID = Left$(CreateGUID, 8) + &quot;-&quot; + Mid$(CreateGUID, 9, 4) + &quot;-&quot; + Mid$(CreateGUID, 13, 4) + &quot;-&quot; + Mid$(CreateGUID, 17, 4) + &quot;-&quot; + Right$(CreateGUID, 12)
    Else
        MsgBox &quot;Error while creating GUID!&quot;
    End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top