Page 1 of 1

Create GUID via WinAPI

Posted: Sat Jul 09, 2005 1:49 pm
by javabean
Code updated for 5.20+

Code: Select all

; Uses aXends 'COMLIB' userlib!
;
; If  'COMLIB' is not available use the following
; Uni2Ansi-Procedure ((c) aXend):
;
 Procedure.s Uni2Ansi(*Unicode)
   size.l = WideCharToMultiByte_(#CP_ACP, 0, *Unicode, -1, #Null, #Null, #Null, #Null)
   ansi.s=Space(size)
   WideCharToMultiByte_(#CP_ACP, 0, *Unicode, -1, @ansi, size, #Null, #Null)
   ProcedureReturn ansi
 EndProcedure


Procedure.s GUIDcreate()
  g.GUID
  If CoCreateGuid_(@g) = #S_OK
    unicodeGUID$ = Space(78)
    GUIDLen = StringFromGUID2_(g, @unicodeGUID$, Len(unicodeGUID$))
    
    CompilerIf #PB_Compiler_Unicode
      ansiGUID$ = unicodeGUID$
    CompilerElse
      ansiGUID$ = Left(Uni2Ansi(@unicodeGUID$), GUIDLen-1)
    CompilerEndIf
  EndIf
ProcedureReturn ansiGUID$
EndProcedure

Debug GUIDcreate()

Posted: Sat Jul 09, 2005 3:00 pm
by DoubleDutch
Whats this for?

Posted: Sun Jul 10, 2005 11:28 am
by javabean
GUID stands for Globally Unique Identifier. It's the Microsoft version of an UUID (Universally Unique Identifier). The GUID is a 128bit number given in the following format:

Code: Select all

{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

e.g. {E217B7A4-66C9-4E00-A962-78A20C1B484F}
Because of the 128 bits and the way the number is generated it is not very likely to find the same number twice on earth (therefore 'Globally Unique').
Windows uses GUIDs mostly in the context of COM objects as class- or interface identifier (e.g CLSID). But GUIDs can also be used as primary keys in databases, etc.

Just a few links:

http://www.webopedia.com/TERM/G/GUID.html
http://en.wikipedia.org/wiki/UUID

Posted: Sun Jul 10, 2005 11:45 am
by DoubleDutch
Thanks, that clears it up :)

-Anthony

Re: Create GUID via WinAPI

Posted: Sun Jan 26, 2020 10:59 pm
by timwil1963
Much thanks for this

Re: Create GUID via WinAPI

Posted: Mon Jan 27, 2020 1:33 am
by BarryG
Cross-platform and only two lines of code:

Code: Select all

For n=1 To 32 : t$+Hex(Random($F)) : Next
Debug Left(t$,8)+"-"+Mid(t$,9,4)+"-"+Mid(t$,13,4)+"-"+Mid(t$,17,4)+"-"+Mid(t$,21)
As a cross-platform procedure:

Code: Select all

Procedure.s CreateGUID()
  For n=1 To 32 : t$+Hex(Random($F)) : Next
  ProcedureReturn Left(t$,8)+"-"+Mid(t$,9,4)+"-"+Mid(t$,13,4)+"-"+Mid(t$,17,4)+"-"+Mid(t$,21)
EndProcedure

Debug CreateGUID() ; 2AD0672A-55EA-CF01-1B0C-3613AB9274D7

Re: Create GUID via WinAPI

Posted: Mon Jan 27, 2020 2:49 pm
by mk-soft
Random is not Safe...

Link for All OS: viewtopic.php?f=12&t=74015

Re: Create GUID via WinAPI

Posted: Mon Jan 27, 2020 3:14 pm
by Kiffi

Code: Select all

EnableExplicit

Procedure.s GetGUID()
  
  Protected *Buffer
  Protected GUID.s
  
  InitNetwork()
  
  *Buffer = ReceiveHTTPMemory("https://www.uuidgenerator.net/api/guid")
  
  If *Buffer
    GUID = PeekS(*Buffer, MemorySize(*Buffer), #PB_UTF8 | #PB_ByteLength)
    FreeMemory(*Buffer)
  Else
    Debug "Failed"
  EndIf
  
  ProcedureReturn GUID
  
EndProcedure

Debug GetGUID()
:P

Re: Create GUID via WinAPI

Posted: Mon Jan 27, 2020 3:15 pm
by Mijikai
^^
Another (safe) one (x64 only!):

Code: Select all

Procedure.s CreateGUID()
  Protected ps.i
  Protected gs.s
  If OpenCryptRandom()
    For ps = 0 To 35
      gs + Hex(CryptRandom($F))
    Next
    ps = @gs
    !mov rax,[p.v_ps]
    !mov word[rax + 0x10],0x2D
    !mov word[rax + 0x1A],0x2D
    !mov word[rax + 0x24],0x2D
    !mov word[rax + 0x2E],0x2D
    CloseCryptRandom()
  EndIf
  ProcedureReturn gs 
EndProcedure