Skip to content

Instantly share code, notes, and snippets.

@jNizM
Last active May 16, 2019 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jNizM/f8c4f51fd1331d4f2def to your computer and use it in GitHub Desktop.
Save jNizM/f8c4f51fd1331d4f2def to your computer and use it in GitHub Desktop.
Class Globally unique identifier (GUID) / Universally unique identifier (UUID)
#NoEnv
; Test GUID
MsgBox % Unique.Global.Create() ; ==> {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
test_guid1 := Unique.Global.Create()
test_guid2 := Unique.Global.Create()
MsgBox % Unique.Global.IsEqualGUID(test_guid1, test_guid1) ; ==> 1
MsgBox % Unique.Global.IsEqualGUID(test_guid1, test_guid2) ; ==> 0
; Test UUID
MsgBox % Unique.Universal.Create() ; ==> xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
MsgBox % Unique.Universal.Create(1) ; ==> {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
MsgBox % Unique.Universal.Create(1, 1) ; ==> {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
MsgBox % Unique.Universal.CreateNil() ; ==> 00000000-0000-0000-0000-000000000000
MsgBox % Unique.Universal.CreateNil(1) ; ==> {00000000-0000-0000-0000-000000000000}
MsgBox % Unique.Universal.CreateSequential() ; ==> xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
MsgBox % Unique.Universal.CreateSequential(1) ; ==> {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
MsgBox % Unique.Universal.CreateSequential(1, 1) ; ==> {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
test_uuid1 := Unique.Universal.Create()
test_uuid2 := Unique.Universal.Create()
MsgBox % Unique.Universal.Equal(test_uuid1, test_uuid1) ; ==> 1
MsgBox % Unique.Universal.Equal(test_uuid1, test_uuid2) ; ==> 0
MsgBox % Unique.Universal.Compare(test_uuid1, test_uuid1) ; ==> 0
MsgBox % Unique.Universal.Compare(test_uuid1, test_uuid2) ; ==> 1 | -1
;MsgBox % Unique.Universal.Hash(test_uuid1)
;test_uuidn := Unique.Universal.CreateNil()
;MsgBox % Unique.Universal.IsNil(test_uuidn)
test_uuid_old := "{550E8400-E29B-11D4-A716-446655440000}"
Unique.Universal.FromString(test_uuid_old, test_uuid_tmp)
Unique.Universal.ToString(test_uuid_tmp, test_uuid_new)
MsgBox % "GUID old:`t" test_uuid_old "`n`nGUID new:`t" test_uuid_new
class Unique
{
; ==========================================================================================================
; Globally unique identifier (GUID)
; https://en.wikipedia.org/wiki/Globally_unique_identifier
; ==========================================================================================================
class Global
{
; ------------------------------------------------------------------------------------------------------
; Function ......: CoCreateGuid
; Description ...: Creates a GUID, a unique 128-bit integer used for CLSIDs and interface identifiers.
; DLL ...........: ole32.dll
; Return ........: S_OK - The GUID was successfully created.
; Links .........: http://msdn.microsoft.com/en-us/library/ms688568.aspx
; ------------------------------------------------------------------------------------------------------
Create()
{
VarSetCapacity(foo_guid, 16, 0)
if !(DllCall("ole32.dll\CoCreateGuid", "Ptr", &foo_guid))
this.StringFromGUID2(&foo_guid, fin_guid)
return fin_guid
}
; ------------------------------------------------------------------------------------------------------
; Function ......: StringFromGUID2
; Description ...: Converts a globally unique identifier (GUID) into a string of printable characters.
; DLL ...........: ole32.dll
; Return ........: If the function succeeds, the return value is the number of characters in the
; returned string, including the null terminator.
; If the buffer is too small to contain the string, the return value is 0.
; Links .........: http://msdn.microsoft.com/en-us/library/ms683893.aspx
; ------------------------------------------------------------------------------------------------------
StringFromGUID2(byref foo_guid, byref fin_guid)
{
VarSetCapacity(tmp_guid, 38 * 2 + 1)
DllCall("ole32.dll\StringFromGUID2", "Ptr", foo_guid, "Ptr", &tmp_guid, "Int", 38 + 1)
fin_guid := StrGet(&tmp_guid, "UTF-16")
return true
}
; ------------------------------------------------------------------------------------------------------
; Function ......: IsEqualGUID
; Description ...: Determines whether two GUIDs are equal.
; DLL ...........: ole32.dll
; Return ........: If the values are equal, the return value is TRUE.
; Otherwise, the return value is FALSE.
; Links .........: http://msdn.microsoft.com/en-us/library/ms680575.aspx
; ------------------------------------------------------------------------------------------------------
IsEqualGUID(guid1, guid2)
{
return DllCall("ole32.dll\IsEqualGUID", "Ptr", &guid1, "Ptr", &guid2)
}
}
; ==========================================================================================================
; Universally unique identifier (UUID)
; https://en.wikipedia.org/wiki/Universally_unique_identifier
; ==========================================================================================================
class Universal
{
;static RPC_S_UUID_LOCAL_ONLY := 0x0720 ; 1824
;static RPC_S_UUID_NO_ADDRESS := 0x06CB ; 1739
;static RPC_S_OUT_OF_MEMORY := 0x000E ; 14
;static RPC_S_INVALID_STRING_UUID := 0x06A9 ; 1705
; ------------------------------------------------------------------------------------------------------
; Function ......: UuidCreate
; Description ...: The UuidCreate function creates a new UUID.
; DLL ...........: rpcrt4.dll
; Return ........: RPC_S_OK - The call succeeded.
; RPC_S_UUID_LOCAL_ONLY - The UUID is guaranteed to be unique to this computer only.
; RPC_S_UUID_NO_ADDRESS - Cannot get Ethernet or token-ring hardware address.
; Links .........: http://msdn.microsoft.com/en-us/library/aa379205.aspx
; ------------------------------------------------------------------------------------------------------
Create(brackets := 0, case := 0)
{
VarSetCapacity(foo_uuid, 16, 0)
if !(DllCall("rpcrt4.dll\UuidCreate", "Ptr", &foo_uuid))
this.ToString(foo_uuid, fin_uuid, brackets, case)
return fin_uuid
}
; ------------------------------------------------------------------------------------------------------
; Function ......: UuidCreateNil
; Description ...: The UuidCreateNil function creates a nil-valued UUID.
; DLL ...........: rpcrt4.dll
; Return ........: RPC_S_OK - The call succeeded.
; Links .........: http://msdn.microsoft.com/en-us/library/aa379262.aspx
; ------------------------------------------------------------------------------------------------------
CreateNil(brackets := 0)
{
VarSetCapacity(foo_uuid, 16, 0)
if !(DllCall("rpcrt4.dll\UuidCreateNil", "Ptr", &foo_uuid))
this.ToString(foo_uuid, fin_uuid, brackets, 0)
return fin_uuid
}
; ------------------------------------------------------------------------------------------------------
; Function ......: UuidCreateSequential
; Description ...: The UuidCreateSequential function creates a new UUID.
; DLL ...........: rpcrt4.dll
; Return ........: RPC_S_OK - The call succeeded.
; RPC_S_UUID_LOCAL_ONLY - The UUID is guaranteed to be unique to this computer only.
; RPC_S_UUID_NO_ADDRESS - Cannot get Ethernet or token-ring hardware address.
; Links .........: http://msdn.microsoft.com/en-us/library/aa379322.aspx
; ------------------------------------------------------------------------------------------------------
CreateSequential(brackets := 0, case := 0)
{
VarSetCapacity(foo_uuid, 16)
if !(DllCall("rpcrt4.dll\UuidCreateSequential", "Ptr", &foo_uuid))
this.ToString(foo_uuid, fin_uuid, brackets, case, case)
return fin_uuid
}
; ------------------------------------------------------------------------------------------------------
; Function ......: UuidToString
; Description ...: The UuidToString function converts a UUID to a string.
; DLL ...........: rpcrt4.dll
; Return ........: RPC_S_OK - The call succeeded.
; RPC_S_OUT_OF_MEMORY - The system is out of memory.
; Links .........: http://msdn.microsoft.com/en-us/library/aa379352.aspx
; ------------------------------------------------------------------------------------------------------
ToString(byref foo_uuid, byref fin_uuid, brackets := 0, case := 0)
{
tmp_uuid := ""
if !(DllCall("rpcrt4.dll\UuidToString", "Ptr", &foo_uuid, "UIntP", tmp_uuid))
{
fin_uuid := DllCall("MulDiv", "Ptr", tmp_uuid, "Int", 1, "Int", 1, "Str")
DllCall("rpcrt4.dll\RpcStringFree", "UIntP", tmp_uuid)
case ? DllCall("user32.dll\CharUpper", "Ptr", &fin_uuid) : fin_uuid
fin_uuid := brackets ? "{" fin_uuid "}" : fin_uuid
return true
}
return false
}
; ------------------------------------------------------------------------------------------------------
; Function ......: UuidFromString
; Description ...: The UuidFromString function converts a string to a UUID.
; DLL ...........: rpcrt4.dll
; Return ........: RPC_S_OK - The call succeeded.
; RPC_S_INVALID_STRING_UUID - The string UUID is invalid.
; Links .........: http://msdn.microsoft.com/en-us/library/aa379336.aspx
; ------------------------------------------------------------------------------------------------------
FromString(fin_uuid, byref foo_uuid)
{
if (RegExMatch(fin_uuid, "^\{.*\}$"))
fin_uuid := SubStr(fin_uuid, 2, -1)
VarSetCapacity(foo_uuid, 16, 0)
if !DllCall("rpcrt4.dll\UuidFromString", "Ptr", &fin_uuid, "Ptr", &foo_uuid)
return true
return false
}
; ------------------------------------------------------------------------------------------------------
; Function ......: UuidHash
; Description ...: The UuidHash function generate a hash value for a specified UUID.
; DLL ...........: rpcrt4.dll
; Return ........:
; Links .........: http://msdn.microsoft.com/en-us/library/aa379336.aspx
; ------------------------------------------------------------------------------------------------------
Hash(uuid)
{
static RPC_S_OK := 0
return DllCall("rpcrt4.dll\UuidHash", "Ptr", &uuid, "Ptr", &RPC_S_OK)
}
; ------------------------------------------------------------------------------------------------------
; Function ......: UuidEqual
; Description ...: The UuidEqual function compare two UUIDs and determine whether they are equal.
; DLL ...........: rpcrt4.dll
; Return ........: TRUE - The Uuid1 parameter is equal to the Uuid2 parameter.
; FALSE - The Uuid1 parameter is not equal to the Uuid2 parameter.
; Links .........: http://msdn.microsoft.com/en-us/library/aa379329.aspx
; ------------------------------------------------------------------------------------------------------
Equal(uuid1, uuid2)
{
static RPC_S_OK := 0
return DllCall("rpcrt4.dll\UuidEqual", "Ptr", &uuid1, "Ptr", &uuid2, "Ptr", &RPC_S_OK)
}
; ------------------------------------------------------------------------------------------------------
; Function ......: UuidCompare
; Description ...: The UuidCompare function compare two UUIDs and determine their order
; DLL ...........: rpcrt4.dll
; Return ........: –1 - The Uuid1 parameter is less than the Uuid2 parameter.
; 0 - The Uuid1 parameter is equal to the Uuid2 parameter.
; 1 - The Uuid1 parameter is greater than the Uuid2 parameter.
; Links .........: http://msdn.microsoft.com/en-us/library/aa379201(v=vs.85).aspx
; ------------------------------------------------------------------------------------------------------
Compare(uuid1, uuid2)
{
static RPC_S_OK := 0
return DllCall("rpcrt4.dll\UuidCompare", "Ptr", &uuid1, "Ptr", &uuid2, "Ptr", &RPC_S_OK)
}
; ------------------------------------------------------------------------------------------------------
; Function ......: UuidIsNil
; Description ...: The UuidIsNil function determine whether the specified UUID is a nil-valued UUID.
; DLL ...........: rpcrt4.dll
; Return ........: TRUE - The Uuid parameter is a nil-valued UUID.
; FALSE - The Uuid parameter is not a nil-valued UUID.
; Links .........: http://msdn.microsoft.com/en-us/library/aa379347.aspx
; ------------------------------------------------------------------------------------------------------
IsNil(uuid)
{
static RPC_S_OK := 0
return DllCall("rpcrt4.dll\UuidIsNil", "Ptr", &uuid, "Ptr", &RPC_S_OK)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment