一下代码获取当前点击的资源管理器的路径但是win11不兼容因为win11可以多个tab展示如何调整代码c++char getExplorerPath CoInitializeNULL; CComPtr IShellWindows pshWindows; if FAILED pshWindowsCoCreateInstance CLSID_ShellWindows
可以通过遍历所有打开的资源管理器窗口,并获取其选中的文件路径来实现多tab展示的情况下获取当前点击的资源管理器路径。具体实现可以参考以下代码:
char* getExplorerPath()
{
CoInitialize(NULL);
CComPtr<IShellWindows> pshWindows;
if (FAILED(pshWindows.CoCreateInstance(CLSID_ShellWindows)))
{
return nullptr;
}
long count = 0;
if (FAILED(pshWindows->get_Count(&count)))
{
return nullptr;
}
for (long i = 0; i < count; ++i)
{
CComVariant vi{ i };
CComPtr<IDispatch> pDisp;
if (FAILED(pshWindows->Item(vi, &pDisp)))
{
continue;
}
if (!pDisp)
{
continue;
}
CComQIPtr<IWebBrowserApp> pApp{ pDisp };
if (!pApp)
{
continue;
}
// Get the window handle.
HWND hwnd;
HWND currWindow = GetForegroundWindow();
pApp->get_HWND(reinterpret_cast<SHANDLE_PTR*>(&hwnd));
if (hwnd != currWindow)
{
continue;
}
CComQIPtr<IServiceProvider> psp{ pApp };
if (!psp)
{
continue;
}
CComPtr<IShellBrowser> pBrowser;
if (FAILED(psp->QueryService(SID_STopLevelBrowser, &pBrowser)))
{
continue;
}
CComPtr<IShellView> pShellView;
if (FAILED(pBrowser->QueryActiveShellView(&pShellView)))
{
continue;
}
CComQIPtr<IFolderView> pFolderView{ pShellView };
if (!pFolderView)
{
continue;
}
CComPtr<IPersistFolder2> pFolder;
if (FAILED(pFolderView->GetFolder(IID_IPersistFolder2, (void**)&pFolder)))
{
continue;
}
LPITEMIDLIST pidl = nullptr;
if (FAILED(pFolder->GetCurFolder(&pidl)))
{
continue;
}
TCHAR path[MAX_PATH];
if (!SHGetPathFromIDList(pidl, path))
{
continue;
}
CoTaskMemFree(pidl);
return _strdup(CStringA(path));
}
// 遍历所有打开的资源管理器窗口
HWND hwnd = FindWindowEx(NULL, NULL, L"CabinetWClass", NULL);
while (hwnd)
{
CComPtr<IShellWindows> pshWindows;
if (FAILED(pshWindows.CoCreateInstance(CLSID_ShellWindows)))
{
return nullptr;
}
long count = 0;
if (FAILED(pshWindows->get_Count(&count)))
{
return nullptr;
}
for (long i = 0; i < count; ++i)
{
CComVariant vi{ i };
CComPtr<IDispatch> pDisp;
if (FAILED(pshWindows->Item(vi, &pDisp)))
{
continue;
}
if (!pDisp)
{
continue;
}
CComQIPtr<IWebBrowserApp> pApp{ pDisp };
if (!pApp)
{
continue;
}
// Get the window handle.
HWND hwndExplorer;
pApp->get_HWND(reinterpret_cast<SHANDLE_PTR*>(&hwndExplorer));
if (hwndExplorer != hwnd)
{
continue;
}
CComQIPtr<IServiceProvider> psp{ pApp };
if (!psp)
{
continue;
}
CComPtr<IShellBrowser> pBrowser;
if (FAILED(psp->QueryService(SID_STopLevelBrowser, &pBrowser)))
{
continue;
}
CComPtr<IShellView> pShellView;
if (FAILED(pBrowser->QueryActiveShellView(&pShellView)))
{
continue;
}
CComQIPtr<IFolderView> pFolderView{ pShellView };
if (!pFolderView)
{
continue;
}
// 获取选中的文件路径
CComPtr<IShellItemArray> pItems;
if (FAILED(pFolderView->GetSelectedItems(&pItems)))
{
continue;
}
DWORD countItems = 0;
if (FAILED(pItems->GetCount(&countItems)))
{
continue;
}
if (countItems == 0)
{
continue;
}
CComPtr<IShellItem> pItem;
if (FAILED(pItems->GetItemAt(0, &pItem)))
{
continue;
}
PWSTR pszFilePath = NULL;
if (FAILED(pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath)))
{
continue;
}
char* szFilePath = new char[MAX_PATH];
WideCharToMultiByte(CP_ACP, 0, pszFilePath, -1, szFilePath, MAX_PATH, NULL, NULL);
CoTaskMemFree(pszFilePath);
return szFilePath;
}
hwnd = FindWindowEx(NULL, hwnd, L"CabinetWClass", NULL);
}
CoUninitialize();
return nullptr;
}
``
原文地址: https://www.cveoy.top/t/topic/hfHj 著作权归作者所有。请勿转载和采集!