Wednesday, 1 December 2010

A simple GUID generator function for Delphi

Here is a little code sample, that I run into the other day. The actual URL is here, but since the original site is now closed, I thought that I could save this posting on my blog as well.

If anybody running CodeGear RAD Studio 2007 ever wishes to play with GUID's then this function is a real must have, All credit goes to the original poster.


function GUIDString: String;


uses ActiveX;
function GUIDString: String;
  Guid: TGUID;
  wsGuid: WideString;
  Result := '';
  { create the 128 bit integer }
  if CoCreateGuid(Guid) = S_OK then begin
    { create a buffer of suitable size }
    SetLength(wsGuid, 80);
    { convert the 128 bit integer into a Unicode string }
    if StringFromGUID2(Guid, POleStr(wsGuid), 80) > 0 then
      { convert the Unicode to a String for output }
      Result := OleStrToString(PWideChar(wsGuid));
    end;  { if CoCreateGuid ... }