Convert Guid To String

Having been pampered by C#, where Guid to string is so trivial, I had to return to an older c++ project and achieve the same thing. Just how hard can it be ? I mean, there’s even a StringFromCLISD function. True, no example of how to use it on MSDN and as with so many API calls the returned information is always in exactly the wrong format  - in this case OleStr. But we can fix that can’t we:

LPOLESTR pOleStr;
 StringFromCLSID(filterClsid,&pOleStr);
 CString csClsid(pOleStr);
 CoTaskMemFree(pOleStr);

Wrong ! This blows up in release (but not debug) builds when function returns, suggesting it is the CString that is the real culprit. A highly informative “Critical Error” is generated and something about the rtl heap corrupt. Debug builds are fine though.

Perhaps this is a known issue of some kind, maybe I’ve done something wrong, but happily there is another function we can use, StringFromGUID2 !

const int GUID_STRING_LENGTH = 40;
OLECHAR szGuid[GUID_STRING_LENGTH]={0};
int nCount = ::StringFromGUID2(filterClsid, szGuid, GUID_STRING_LENGTH );
CString csClsid(szGuid);

Works in build and release. Good-o.

 

2 comments

  1. Dr A says:

    A little correction, the function in question is called StringFromGUID2, not StringFromCLSID2, as can be seen from the example code :)

  2. DukeNukem says:

    Wondered how long it’d take to spot the deliberate mistake I inserted to see if people were paying attention :-)

Leave a Reply