Create GUID via WinAPI

Share your advanced PureBasic knowledge/code with the community.
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

Create GUID via WinAPI

Post 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()
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Whats this for?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

Post 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
Last edited by javabean on Sun Jul 10, 2005 12:00 pm, edited 1 time in total.
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Thanks, that clears it up :)

-Anthony
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
timwil1963
New User
New User
Posts: 7
Joined: Sat Dec 28, 2019 4:26 pm

Re: Create GUID via WinAPI

Post by timwil1963 »

Much thanks for this
BarryG
Addict
Addict
Posts: 3293
Joined: Thu Apr 18, 2019 8:17 am

Re: Create GUID via WinAPI

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Create GUID via WinAPI

Post by mk-soft »

Random is not Safe...

Link for All OS: viewtopic.php?f=12&t=74015
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Create GUID via WinAPI

Post 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
Hygge
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Create GUID via WinAPI

Post 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
Post Reply