Search code examples
delphiuuidguiddelphi-5

How to generate a GUID version 1 in Delphi?


Is there a way to generate GUID v1 (time-based) in Delphi 5? I found this function...

unit ComObj;
function CreateClassID: string;

But I'm not sure if it generates a time-based GUID. Also I know about this...

SysUtils.CreateGUID(newGUID);

... which calls the Windows API CoCreateGUID that produces the GUID version 4 (random).


Solution

  • Similar to how CreateGUID is implemented - using UuidCreateSequential:

    uses
      Windows;
    
    function UuidCreateSequential(out guid: TGUID): Longint; stdcall; external 'rpcrt4.dll' name 'UuidCreateSequential';
    
    function CreateGUID_V1(out Guid: TGUID): HResult;
    begin
      Result := HResultFromWin32(UuidCreateSequential(Guid));
    end;
    

    I don't have Delphi 5 here so not sure if everything being used here was available 20 years ago.