Thursday 20 July 2017

How to create GUID at runtime

To create a GUID (Globally Unique Identifier) at runtime I have two different ways of doing this.

First Method
Add ComObj unit to the uses clause, if not already there, then to create the GUID as a string just do the following:
FThisID := CreateClassID;

Second Method
This method you need the SysUtils in the uses clause. The do something like the following:
var
  newGUID: TGUID;
begin
  SysUtils.CreateGUID(newGUID);
  FThisID := GUIDToString(newGUID); 
end;

3 comments:

  1. Yes Stefan, that is simpler, thanks I will use that in future.

    ReplyDelete
  2. Newer delphi version have record helpers which allow TGuid.Creat

    ReplyDelete