// Populates item. It is the responsibility of the caller to call item->Release() // if the return value indicates success HRESULT ComposerShellMenu::GetShellItem(IShellItem ** item, PBOOLEAN isFile) { HRESULT hr = E_FAIL; *item = NULL; if (m_Folder) { hr = SHCreateItemFromIDList(m_Folder, IID_PPV_ARGS(item)); } else if (m_Data) { IShellItemArray *itemArray = NULL; hr = SHCreateShellItemArrayFromDataObject(m_Data, IID_PPV_ARGS(&itemArray)); if (SUCCEEDED(hr)) { DWORD count = 0; hr = itemArray->GetCount(&count); if (SUCCEEDED(hr)) { if (1 == count) { hr = itemArray->GetItemAt(0, item); } else { hr = E_FAIL; } } itemArray->Release(); itemArray = NULL; } } if (SUCCEEDED(hr)) { hr = IsShellItemValid(*item, false, isFile); if (FAILED(hr)) { (*item)->Release(); *item = NULL; } } return hr; }
HRESULT ContextMenu::ShareWithBoxItems(IDataObject *dataObject, HWND hwndParent) { HRESULT hr = S_OK; if (dataObject == NULL) { return E_FAIL; } m_paths.clear(); #if (WINVER >= 0x0600) IShellItemArray *psia; hr = SHCreateShellItemArrayFromDataObject(dataObject, IID_PPV_ARGS(&psia)); if (SUCCEEDED(hr)) { //hr = DisplayItems(psia, hwndParent); DWORD count; psia->GetCount(&count); for(DWORD i = 0; i < count; ++i) { IShellItem *psi; hr = psia->GetItemAt(i, &psi); if (SUCCEEDED(hr)) { PWSTR pszDisplayName; hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &pszDisplayName); if (SUCCEEDED(hr)) { m_paths.push_back(wcsdup(pszDisplayName)); CoTaskMemFree(pszDisplayName); } psi->Release(); } else { break; } } psia->Release(); } #else STGMEDIUM medium; FORMATETC fe = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL}; if(SUCCEEDED(dataObject->GetData(&fe, &medium))) { // Get the count of files dropped. UINT uCount = DragQueryFile((HDROP)medium.hGlobal, (UINT)-1, NULL, 0); // Get the first file name from the CF_HDROP. for(UINT i = 0; i < uCount; ++i) { WCHAR fileName[MAX_PATH]; if(DragQueryFileW((HDROP)medium.hGlobal, i, fileName, MAX_PATH)) { m_paths.push_back(wcsdup(fileName)); } } ReleaseStgMedium(&medium); } #endif ShareWithBox(hwndParent); return hr; }