Platform SDK: Active Directory, ADSI, and Directory Services |
The following C++ code fragment shows a function that returns a string representation of a GUID that can be used to bind to the object:
HRESULT GUIDtoBindableString (LPGUID pGUID, LPOLESTR *ppGUIDString) { HRESULT hr = E_FAIL; if (!pGUID) return E_INVALIDARG; //Build bindable GUID string LPOLESTR szDSGUID = new WCHAR [128]; DWORD dwLen = sizeof(*pGUID); LPBYTE lpByte = (LPBYTE) pGUID; //Copy a blank string to make it a zero length string. wcscpy( szDSGUID, L"" ); //Loop through to add each byte to the string. for( DWORD dwItem = 0L; dwItem < dwLen ; dwItem++ ) { //Append to szDSGUID, double-byte, byte at dwItem index. swprintf(szDSGUID + wcslen(szDSGUID), L"%02x", lpByte[dwItem]); if( wcslen( szDSGUID ) > 128 ) break; } //Allocate memory for string *ppGUIDString = (OLECHAR *)CoTaskMemAlloc (sizeof(OLECHAR)*(wcslen(szDSGUID)+1)); if (*ppGUIDString) wcscpy(*ppGUIDString, szDSGUID); else hr=E_FAIL; //Caller must free ppGUIDString using CoTaskMemFree. return hr; }