41

Does Delphi have anything built-in to generate UUIDs?

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
Cruachan
  • 15,733
  • 5
  • 59
  • 112

4 Answers4

74
program Guid;

{$APPTYPE CONSOLE}

uses
SysUtils;

var

Uid: TGuid;
Result: HResult;

begin
Result := CreateGuid(Uid);
if Result = S_OK then
   WriteLn(GuidToString(Uid));
end.

Under the covers CreateGuid() calls one of the various APIs, depending on the platform. For example on Windows, it nowadays calls UuidCreate.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
39

Also, if you need a GUID for an interface declaration, hit ctrl+shift+g in the code editor to insert a GUID at the caret.

mghie
  • 32,028
  • 6
  • 87
  • 129
Nat
  • 5,414
  • 26
  • 38
6

If you're using one of the latest version of Delphi, and include SysUtils, you can call TGuid.NewGuid to generate a new guid.

NewGuid is actually implemented in a helper class for TGuid (TGuidHelper), which is declared in SysUtils.

This function calls the CreateGUID method (also in SysUtils and already mentioned in the answer by Mitch Wheat), which is actually a cross platform function that calls different libraries depending on the platform it runs on.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
RBA
  • 12,337
  • 16
  • 79
  • 126
  • is this cross platform? – Mehmet Fide Jul 16 '18 at 21:15
  • 2
    @MehmetFide - you can check yourself the documentation http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.SysUtils.CreateGUID – RBA Jul 17 '18 at 10:30
  • This was what I was looking for, but it didn't fully work. Since it is a class helper for TGUID, actually calling `TGuidHelper.NewGuid` doesn't work. You need to call `TGuid.NewGuid`. And that only works if you explicitly include SysUtils, because TGuid is system, but the helper is in SysUtils. I hope you don't mind that I updated the answer with this information. – GolezTrol Sep 12 '18 at 18:20
6

Yes TGUID, see this example

program Guid;

{$APPTYPE CONSOLE}

uses
SysUtils;

begin
  WriteLn(TGUID.NewGuid.ToString());
end.
Simson
  • 3,373
  • 2
  • 24
  • 38
Jesse Lee
  • 174
  • 1
  • 6