UsingGUIDs (C++)

From RAD Studio Code Examples
Jump to: navigation, search


This example demonstrates the usage of some GUID-related routines, along with the type itself.


__fastcall TForm2::TForm2(TComponent* Owner)
	: TForm(Owner)
	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 the Format function to
	   obtain the same output as GUIDToString
	Memo1->Lines->Add(Format(String("GUID using formatting is: ") +
		 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]))));

	/* Autogenerate a random GUID at run time. */
	if (CreateGUID(myGuid1) != 0)
		Memo1->Lines->Add("Creating GUID failed!");
		Memo1->Lines->Add("The generated guid is: " + GUIDToString(myGuid1));

	/* Generating second random GUID */

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