Does Delphi have anything built-in to generate UUIDs?
Asked
Active
Viewed 4.2k times
4 Answers
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.
-
That is a fast way to do, helped me a lot. – Frederiko Ribeiro Jun 05 '19 at 16:30
-
Be aware: the shortcut is dependent of the chosen keyboard layout in Delphi options (https://stackoverflow.com/a/45942091/2925238) – casiosmu Jul 28 '21 at 08:58
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.
-
-
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