// Function for obtaining an interface pointer to a COM shell object // // path: absolute path to the folder comprising the object // riid: the interface to return, IID_IDropTarget // pObject: the address to receive the interface pointer, caller must Release() bool UIObjectFromPath (wchar_t* path, const GUID riid, void** pObject) { // naked drive must have trailing backslash if (path [wcslen (path) - 1] == L':') wcscat (path, L"\\"); HRESULT hr = 0; IMalloc* pMalloc = 0; hr = ::SHGetMalloc (&pMalloc); if (hr != NOERROR) return false; IShellFolder* psfDesktop = 0; hr = ::SHGetDesktopFolder(&psfDesktop); if (hr != NOERROR) { pMalloc->Release(); return false; } // get absolute pidl ITEMIDLIST* pidl = 0; ULONG chEaten = 0; ULONG attributes = 0; hr = psfDesktop->ParseDisplayName (NULL, NULL, (wchar_t*)path, &chEaten, &pidl, &attributes); if (hr != NOERROR) { psfDesktop->Release(); pMalloc->Release (); return false; } // get the object *pObject = NULL; hr = psfDesktop->GetUIObjectOf (NULL, 1, (const ITEMIDLIST**)&pidl, riid, NULL, pObject); // cleanup if (pidl) pMalloc->Free (pidl); if (psfDesktop) psfDesktop->Release(); pMalloc->Release (); return SUCCEEDED(hr); }