vbAccelerator - Contents of code file: GUIDGenerator.cls

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "GUIDGenerator"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'Author:    Dion Wiggins
'Purpose:   A simple component that generates a unique GUID value.
'           A GUID is a 16-byte field used as a unique identifier.
'           A GUID, also knows as a CLSID and IID a 16-byte identifier that is
 guaranteed to be unique.
'           This allows developers across the world can independently develop
 and deploy their software
'           without fear of accidental collision with software written by
 others.
'           This same GUID value can be used not only for software components,
 but for unique keys in
'           databases and any other application that requires a unique value.
'           COM frequently must map GUIDs (IIDs and CLSIDs) to some arbitrarily
 large set of other values.
'
'Copyright: Copyright (C) 1999, Control-Shift Inc. All Rights Reserved.
'Notes:     You are free to use and modify this code, but not to distribute
 modified versions
'           of the ActiveX DLL with the same filename and/or ProgIds.
'           If you have made changes which you think are beneficial, or have
 bug reports, then you
'           can email me (dionwiggins@hotmail.com) and I will do my utmost to
 get the a new version
'           published.
'           You can freely distribute this code, but you must distribute them
 it in its original state
'           and particularly keep the disclaimer information. Notification
 would be greatly appreciated!
'           You can freely distribute any compiled code or any products you
 build using the code.
'           If you wish to distribute the source code files by any other means
 (i.e. if you want to
'           include it on a CD or any other software media) then the EXPRESS
 PERMISSION of the
'           Control-Shift Inc. is REQUIRED.
Option Explicit

Private Type GUID
     Data1 As Long
     Data2 As Long
     Data3 As Long
     Data4(8) 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

'Author:    Dion Wiggins
'Purpose:   Creates a GUID
'Notes:
'Inputs:
'   - strRemoveChars    The characters to remove from the GUID (usually the {}-
 characters)
'History
'Date           Author          Description
'1 Jun 1999     Dion Wiggins    Created
Public Function CreateGUID( _
    Optional strRemoveChars As String = "{}-") As String
Dim udtGUID As GUID
Dim strGUID As String
Dim bytGUID() As Byte
Dim lngLen As Long
Dim lngRetVal As Long
Dim lngPos As Long

'Initialize
lngLen = 40
bytGUID = String(lngLen, 0)

'Create the GUID
CoCreateGuid udtGUID

'Convert the structure into a displayable string
lngRetVal = StringFromGUID2(udtGUID, VarPtr(bytGUID(0)), lngLen)
strGUID = bytGUID
If (Asc(Mid$(strGUID, lngRetVal, 1)) = 0) Then
    lngRetVal = lngRetVal - 1
End If

'Trim the trailing characters
strGUID = Left$(strGUID, lngRetVal)

'Remove the unwanted characters
For lngPos = 1 To Len(strRemoveChars)
    strGUID = Replace(strGUID, Mid(strRemoveChars, lngPos, 1), "")
Next

CreateGUID = strGUID
End Function