RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.GUIDToString Function

Converts a class identifier GUID to a string.

Pascal
function GUIDToString(const GUID: TGUID): string;
C++
AnsiString GUIDToString(const TGUID GUID);

GUIDToString converts a GUID into a string of printable characters. Each GUID always converts to a unique string. 

If GUIDToString succeeds it returns a string for the requested GUID. If GUIDToString fails, it raises EConvertError.  

C++ Examples: 

 

/*
The following example manufactures a GUID and displays it in
a dialog box after translating it to a string.
*/
#include <ActiveX.hpp>
#include <ComObj.hpp>
#include <Sysutils.hpp>

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TGUID g;
    OleCheck(CoCreateGuid(&g));
    ShowMessage(Sysutils::GUIDToString(g));
}
//
//This example demonstrates the usage of some GUID related routines along with
//the type itself.
//
void __fastcall TForm2::FormCreate(TObject *Sender)
{
    TGUID myGuid0, myGuid1;

    /* Create a new GUID from the string representation */
    myGuid0 = StringToGUID("{00020400-0000-0000-C000-000000000046}");
    Memo1->Lines->Add("The GUID is: " + GUIDToString(myGuid0));

    /*
    Accessing GUID's internal fields. Using Format function to
    obtain the same output as GUIDToString
    */
    Memo1->Lines->Add(Format(String("GUID using formatting is: ") +
               "{%0.8X-%0.4X-%0.4X-%0.2X%0.2X-%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X}",
         ARRAYOFCONST((myGuid0.Data1, myGuid0.Data2, myGuid0.Data3,
         myGuid0.Data4[0], myGuid0.Data4[1], myGuid0.Data4[2], myGuid0.Data4[3],
         myGuid0.Data4[4], myGuid0.Data4[5], myGuid0.Data4[6], myGuid0.Data4[7]))));

    /* Auto-generate a random GUID at runtime */
    if (CreateGUID(myGuid1) != 0)
        Memo1->Lines->Add("Creating GUID failed!");
    else
        Memo1->Lines->Add("The generated guid is: " + GUIDToString(myGuid1));

    /* Generating second random GUID */
    CreateGUID(myGuid0);

    /* Testing if 2 guids are equal */
    if (IsEqualGUID(myGuid0, myGuid1))
       Memo1->Lines->Add("This cannot happen! CreateGUID guarantees that 2 " +
                String("randomly generated GUIDs cannot be equal!"));
}

 

Delphi Examples: 

{
The following example manufactures a GUID and displays it in
a dialog box after translating it to a string.
} 
uses ActiveX, ComObj;
procedure TForm1.Button1Click(Sender: TObject);
var
  G: TGUID;
begin
  OleCheck(CoCreateGuid(G));
  ShowMessage(SysUtils.GUIDToString(G));
end;
{
This example demonstrates the usage of some GUID related routines along with
the type itself.
}
procedure TForm2.FormCreate(Sender: TObject);
var
  MyGuid0, MyGuid1 : TGUID;

begin
  { Create a new GUID from the string representation }
  MyGuid0 := StringToGUID('{00020400-0000-0000-C000-000000000046}');
  Memo1.Lines.Add('The GUID is: ' + GUIDToString(MyGuid0));

  {
  Accessing GUID's internal fields. Using Format function to obtain the
  same output as GUIDToString
  }
  Memo1.Lines.Add(Format('GUID using formatting is: ' +
       '{%0.8X-%0.4X-%0.4X-%0.2X%0.2X-%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X}',
       [MyGuid0.D1, MyGuid0.D2, MyGuid0.D3,
       MyGuid0.D4[0], MyGuid0.D4[1], MyGuid0.D4[2], MyGuid0.D4[3],
       MyGuid0.D4[4], MyGuid0.D4[5], MyGuid0.D4[6], MyGuid0.D4[7]]));

  { Auto-generate a random GUID at runtime }
  if CreateGUID(MyGuid1) <> 0 then
     Memo1.Lines.Add('Creating GUID failed!')
  else
     Memo1.Lines.Add('The generated guid is: ' + GUIDToString(MyGuid1));

  { Generating second random GUID }
  CreateGUID(MyGuid0);

  { Testing if 2 guids are equal }
  if IsEqualGUID(MyGuid0, MyGuid1) then
     Memo1.Lines.Add('This cannot happen! CreateGUID guarantees that ' +
                     '2 randomly generated GUIDs cannot be equal!');
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!