Mirror

Create a (unique) GUID (Views: 100)


Problem/Question/Abstract:

I'm looking for a function to create a GUID. I don't want to create a COM object or something, I just need a sort of 'Session ID' for a special database procedure.

Answer:

uses
  ActiveX;

function GenerateKey: string;
var
  MyGUID: TGUID;
  MyWideChar: array[0..100] of WideChar;
begin
  {First, generate the GUID:}
  CoCreateGUID(MyGUID);
  {Now convert it to a wide-character string:}
  StringFromGUID2(MyGUID, MyWideChar, 39);
  {Now convert it to a Delphi string:}
  Result := WideCharToString(MyWideChar);
  {Get rid of the three dashes that StringFromGUID2() puts in the result string:}
  while Pos('-', Result) > 0 do
    Delete(Result, Pos('-', Result), 1);
  {Get rid of the left and right brackets in the string:}
  while Pos('{', Result) > 0 do
    Delete(Result, Pos('{', Result), 1);
  while Pos('}', Result) > 0 do
    Delete(Result, Pos('}', Result), 1);
end;

<< Back to main page