Share this page 

Get an UUID (Universally Unique Identifiers)Tag(s): WinAPI/Registry


Thanks to O.L.Dansereau for the tip!

UUID means Universally Unique Identifier, see this Wikipedia article for more details.

[structure definition]
TYPE s_uuid FROM structure
 unsignedlong  data1
 unsignedinteger  data2
 unsignedinteger  data3
 unsignedinteger  data4[4]
END TYPE


[local function declaration]
FUNCTION long uuidCreate(ref s_uuid astr_uuid) LIBRARY "Rpcrt4.dll"  &
  ALIAS FOR "UuidCreate"

[powerscript function string of_hex(unsignedlong aul_decimal)]
String ls_hex
Character lch_hex[0 TO 15] = &
 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', &
  'c', 'd', 'e', 'f'}

// Check parameters
IF IsNull(aul_decimal) THEN
 SetNull(ls_hex)
 RETURN ls_hex
END IF
 
DO
 ls_hex = lch_hex[Mod(aul_decimal, 16)] + ls_hex
 aul_decimal /= 16
LOOP UNTIL aul_decimal = 0
 
RETURN ls_hex

 
[powerscript]
long  ll_rc
s_uuid lstr_uuid
string ls_guid = ""

constant long RPC_S_OK = 0
constant long RPC_S_UUID_LOCAL_ONLY = 1824
constant long RPC_S_UUID_NO_ADDRESS = 1739

ll_rc = uuidCreate(lstr_uuid)
//  returns 
//   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/token-ring hardware address for this computer.
IF ll_rc <> RPC_S_OK THEN
    setNull(ls_GUID)
    // MessageBox("", "uuid create not ok ?!?")
ELSE
    ls_GUID = right("00000000" + of_hex(lstr_uuid.data1), 8)
    ls_GUID += "-" + right("0000" + of_hex(lstr_uuid.data2), 4)
    ls_GUID += "-" + right("0000" + of_hex(lstr_uuid.data3), 4)
    ls_GUID += "-" + right("0000" + of_hex(lstr_uuid.data4[1]), 4)
    ls_GUID += "-" + right("0000" + of_hex(lstr_uuid.data4[2]), 4) &
          + right("0000" + of_hex(lstr_uuid.data4[3]), 4) &
          + right("0000" + of_hex(lstr_uuid.data4[4]), 4)
    ls_GUID = upper(ls_GUID)
    // MessageBox("", ls_guid)
    // output example : 00003B93-2641-477A-C99E-A2FFEBEB214A
END IF