I'm trying to open the default file properties dialog but unable to get it working property for files in different directories.
I need to obtain IContextMenu*
interface pointer for files in different directories (or even drives).
I expect to see "Various folders" in the properties dialog.
This is my naive approach:
int main() {
CoInitialize(NULL);
LPOLESTR pszFile = OLESTR("c:\\Windows\\notepad.exe");
LPOLESTR pszFile2 = OLESTR("c:\\Windows\\System32\\notepad.exe");
LPITEMIDLIST pidl;
LPITEMIDLIST pidl2;
LPCITEMIDLIST pidlItem;
LPCITEMIDLIST pidlItem2;
HRESULT hr;
IShellFolder* pFolder;
IContextMenu* pContextMenu;
CMINVOKECOMMANDINFO cmi;
hr = SHGetDesktopFolder(&pFolder);
if (FAILED(hr)) return 0;
hr = pFolder->ParseDisplayName(HWND_DESKTOP, NULL, pszFile, NULL, &pidl, NULL);
hr = pFolder->ParseDisplayName(HWND_DESKTOP, NULL, pszFile2, NULL, &pidl2, NULL);
pFolder->Release();
if (FAILED(hr)) return 0;
hr = SHBindToParent(pidl, IID_IShellFolder, (void **)&pFolder, &pidlItem);
if (FAILED(hr)) {
SHFree(pidl);
return 0;
}
//pFolder->Release();
hr = SHBindToParent(pidl2, IID_IShellFolder, (void **)&pFolder, &pidlItem2);
if (FAILED(hr)) {
SHFree(pidl2);
return 0;
}
LPCITEMIDLIST list[] = {pidlItem, pidlItem2};
hr = pFolder->GetUIObjectOf(HWND_DESKTOP, 2, (LPCITEMIDLIST *)list, IID_IContextMenu, NULL, (void **)&pContextMenu);
pFolder->Release();
if (SUCCEEDED(hr)) {
ZeroMemory(&cmi, sizeof(cmi));
cmi.cbSize = sizeof(cmi);
cmi.lpVerb = "properties";
cmi.nShow = SW_SHOWNORMAL;
hr = pContextMenu->InvokeCommand(&cmi);
MessageBox(0, _T("Dummy message box"), 0, 0);
Sleep(10000); // Give the system time to show the dialog before exiting
pContextMenu->Release();
}
SHFree(pidl);
return 0;
}
Also I tried this, but it also not working at all. Also I tried to use desktop IShellFolder as parent of my PIDLs, but it also not working.
Any ideas?
IShellFolder::GetUIObjectOf()
only works with single-level PIDLs that are relative to theIShellFolder
being queried. This is clearly stated in theGetUIObjectOf()
documentation:You are converting the 2 absolute PIDLs into relative PIDLs of their respective folders, but then you are using the
IShellFolder
of theWindows\System32
folder to retrieve theIContextMenu
for both files. That will not work for the relative PIDL belonging to theWindows\
folder, since theIShellFolder
of theWindows\System32
folder only knows about files in theWindows\System32
folder.Various online examples show the
IContextMenu
being queried from theIShellFolder
of the desktop when multiple files are involved. That approach only works when the files are in the same parent folder (such as demonstrated in Raymond Chen's example). Things get more complicated when the files are in different folders.You also have a few memory leaks in your code.
The correct way to handle this situation is to use the
SHMultiFileProperties()
function:Use the
IShellFolder
of the desktop to get anIDataObject
for the absolute PIDLs (this is one time whereGetUIObjectOf()
is allowed to violate the "Each item identifier list must contain exactly one SHITEMID structure" rule) and then pass that toSHMultiFileProperties()
. For example: