I am working with Win32 API in C and have a need to convert a GUID structure into LPCSTR. I am relatively new to Win32 and didn't find much information around this type of conversion.
I did manage to convert GUID to OLECHAR using StringFromGUID2 function (see code fragment below) but stuck on further conversion to LPSCSTR. I am not too sure I am heading in the correct direction with OLECHAR but at the moment it seems logical thing to do.
GUID guid;
OLECHAR wszGuid[40] = {0};
OLECHAR szGuid[40]={0};
LPCSTR lpcGuid;
CoCreateGuid(&guid);
StringFromGUID2(&guid, wszGuid, _countof(wszGuid));
The OS does not support formatting a GUID as an Ansi string directly. You can format it as a Unicode string first and then convert it to Ansi afterwards:
GUID guid = {0};
wchar_t szGuidW[40] = {0};
char szGuidA[40] = {0};
CoCreateGuid(&guid);
StringFromGUID2(&guid, szGuidW, 40);
WideCharToMultiByte(CP_ACP, 0, szGuidW, -1, szGuidA, 40, NULL, NULL);
Or you can use sprintf()
or similar function to format the Ansi string manually:
GUID guid = {0};
char szGuid[40]={0};
CoCreateGuid(&guid);
sprintf(szGuid, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With