Tuesday, January 1, 2008


If you are converting a GUID to a String in C++ you can use this Windows API:

TCHAR szString[41];
::StringFromGUID2(guid, szString, 41);

Note that C++ adds brackets around the GUID so it appears like this:


However, if you are doing a ToString() in C# you don't get the ending brackets, for example:

String output = Guid.NewGuid().ToString();

Will return:


C# will parse a string into a GUID regardless of the brackets.  For example, both these get you the same GUID:

System.Guid guid = new System.Guid("c200e360-38c5-11ce-ae62-08002b2b79ef");

System.Guid guid2 = new System.Guid("{c200e360-38c5-11ce-ae62-08002b2b79ef}");

The equivalent function in C++ is:


However, CLSIDFromtString will not accept a GUID without brackets.  Note here that CLSID, IID and GUID are the same in C++.

If you are going between C++ and C# and you are passing GUIDs as string you need to include the brackets.  It is easier to have C# add the brackets then C++, since it requires a separate string.  Here is how you format your C# string:

String output = String.Format("{{{0}}}",Guid.NewGuid());

Note that you need to use double brackets to represent one bracket on each side.

No comments:

Post a Comment