我一直在摸索如何做到这一点。基本上,我的应用程序需要使用winapi找出c ++中Windows中活动文件浏览器(即前台的文件浏览器)的目录路径。
代替这个:
TCHAR* getWindowDir(){
TCHAR* windowTitle = new TCHAR[MAX_PATH];
HWND windowHandle = GetForegroundWindow();
GetWindowText(windowHandle,windowTitle,MAX_PATH);
return windowTitle;
}
Run Code Online (Sandbox Code Playgroud)
显然返回窗口标题,我希望它返回活动目录。
创建一个实例,IShellWindows并使用该实例枚举当前打开的所有资源管理器窗口。使用各种相关接口,您可以从中PIDL枚举的每个项目中以a的形式获取窗口句柄和当前文件夹IShellWindows。如果窗口句柄等于的结果GetForegroundWindow(),则将PIDL转换为路径。
在下面的内容中,我将提供一些代码来获取有关所有资源管理器窗口的信息。它部分基于Raymond Chen的代码,但是使用智能指针来减少易碎和简洁的代码。我还通过异常添加了错误处理。
首先,必需的包括一些实用程序代码:
#include <Windows.h>
#include <shlobj.h>
#include <atlcomcli.h> // for COM smart pointers
#include <vector>
#include <system_error>
#include <memory>
// Throw a std::system_error if the HRESULT indicates failure.
template< typename T >
void ThrowIfFailed( HRESULT hr, T&& msg )
{
if( FAILED( hr ) )
throw std::system_error{ hr, std::system_category(), std::forward<T>( msg ) };
}
// Deleter for a PIDL allocated by the shell.
struct CoTaskMemDeleter
{
void operator()( ITEMIDLIST* pidl ) const { ::CoTaskMemFree( pidl ); }
};
// A smart pointer for PIDLs.
using UniquePidlPtr = std::unique_ptr< ITEMIDLIST, CoTaskMemDeleter >;
Run Code Online (Sandbox Code Playgroud)
现在,我们定义一个函数GetCurrentExplorerFolders()以返回有关所有当前打开的资源管理器窗口的信息,包括窗口句柄和PIDL当前文件夹的信息。
// Return value of GetCurrentExplorerFolders()
struct ExplorerFolderInfo
{
HWND hwnd = nullptr; // window handle of explorer
UniquePidlPtr pidl; // PIDL that points to current folder
};
// Get information about all currently open explorer windows.
// Throws std::system_error exception to report errors.
std::vector< ExplorerFolderInfo > GetCurrentExplorerFolders()
{
CComPtr< IShellWindows > pshWindows;
ThrowIfFailed(
pshWindows.CoCreateInstance( CLSID_ShellWindows ),
"Could not create instance of IShellWindows" );
long count = 0;
ThrowIfFailed(
pshWindows->get_Count( &count ),
"Could not get number of shell windows" );
std::vector< ExplorerFolderInfo > result;
result.reserve( count );
for( long i = 0; i < count; ++i )
{
ExplorerFolderInfo info;
CComVariant vi{ i };
CComPtr< IDispatch > pDisp;
ThrowIfFailed(
pshWindows->Item( vi, &pDisp ),
"Could not get item from IShellWindows" );
if( ! pDisp )
// Skip - this shell window was registered with a NULL IDispatch
continue;
CComQIPtr< IWebBrowserApp > pApp{ pDisp };
if( ! pApp )
// This window doesn't implement IWebBrowserApp
continue;
// Get the window handle.
pApp->get_HWND( reinterpret_cast<SHANDLE_PTR*>( &info.hwnd ) );
CComQIPtr< IServiceProvider > psp{ pApp };
if( ! psp )
// This window doesn't implement IServiceProvider
continue;
CComPtr< IShellBrowser > pBrowser;
if( FAILED( psp->QueryService( SID_STopLevelBrowser, &pBrowser ) ) )
// This window doesn't provide IShellBrowser
continue;
CComPtr< IShellView > pShellView;
if( FAILED( pBrowser->QueryActiveShellView( &pShellView ) ) )
// For some reason there is no active shell view
continue;
CComQIPtr< IFolderView > pFolderView{ pShellView };
if( ! pFolderView )
// The shell view doesn't implement IFolderView
continue;
// Get the interface from which we can finally query the PIDL of
// the current folder.
CComPtr< IPersistFolder2 > pFolder;
if( FAILED( pFolderView->GetFolder( IID_IPersistFolder2, (void**) &pFolder ) ) )
continue;
LPITEMIDLIST pidl = nullptr;
if( SUCCEEDED( pFolder->GetCurFolder( &pidl ) ) )
{
// Take ownership of the PIDL via std::unique_ptr.
info.pidl = UniquePidlPtr{ pidl };
result.push_back( std::move( info ) );
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
该示例显示了如何调用GetCurrentExplorerFolders(),转换PIDL为path并捕获异常。
int main()
{
::CoInitialize( nullptr );
try
{
std::wcout << L"Currently open explorer windows:\n";
for( const auto& info : GetCurrentExplorerFolders() )
{
wchar_t path[ 32767 ];
if( ::SHGetPathFromIDListEx( info.pidl.get(), path, ARRAYSIZE(path), 0 ) )
std::wcout << L"hwnd: 0x" << std::hex << info.hwnd << L", path: " << path << L"\n";
}
}
catch( std::system_error& e )
{
std::cout << "ERROR: " << e.what() << "\nError code: " << e.code() << "\n";
}
::CoUninitialize();
}
Run Code Online (Sandbox Code Playgroud)
可能的输出:
#include <Windows.h>
#include <shlobj.h>
#include <atlcomcli.h> // for COM smart pointers
#include <vector>
#include <system_error>
#include <memory>
// Throw a std::system_error if the HRESULT indicates failure.
template< typename T >
void ThrowIfFailed( HRESULT hr, T&& msg )
{
if( FAILED( hr ) )
throw std::system_error{ hr, std::system_category(), std::forward<T>( msg ) };
}
// Deleter for a PIDL allocated by the shell.
struct CoTaskMemDeleter
{
void operator()( ITEMIDLIST* pidl ) const { ::CoTaskMemFree( pidl ); }
};
// A smart pointer for PIDLs.
using UniquePidlPtr = std::unique_ptr< ITEMIDLIST, CoTaskMemDeleter >;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1539 次 |
| 最近记录: |