#include#include // Get the GUID data for a file void SetFileGUID(LPCTSTR szFilePath, const GUID* pGuid) { IShellItem* pItem = nullptr; HRESULT hr = SHCreateItemFromParsingName(szFilePath, nullptr, IID_PPV_ARGS(&pItem)); if (SUCCEEDED(hr)) { PROPVARIANT propVar; propVar.vt = VT_CLSID; propVar.puuid = (GUID *)pGuid; hr = pItem->SetProperty(PKEY_DataObjectGuid, propVar); pItem->Release(); } }
HRESULT SetObjectGUID(IUnknown* pObject, const GUID* pGuid) { IPersist* pPersist = nullptr; HRESULT hr = pObject->QueryInterface(IID_IPersist, (void**)&pPersist); if (SUCCEEDED(hr)) { IPersistPropertyBag* pPersistPropBag = nullptr; hr = pPersist->QueryInterface(IID_IPersistPropertyBag, (void**)&pPersistPropBag); if (SUCCEEDED(hr)) { PROPBAG2 propBag = { 0 }; propBag.pstrName = L"DataObjectGuid"; propBag.dwType = PROPBAG2_TYPE_GUID; VARIANT var; VariantInit(&var); var.vt = VT_CLSID; var.puuid = (GUID *)pGuid; hr = pPersistPropBag->Write(L"DataObjectGuid", &var, &propBag); pPersistPropBag->Release(); } pPersist->Release(); } return hr; }This example sets the GUID data for a COM object using the SetObjectGUID method. It queries the object for the IPersist and IPersistPropertyBag interfaces, and then uses the Write method of the IPersistPropertyBag interface to set the value of the "DataObjectGuid" property to the specified GUID. Package library: ole32.lib