正如 Raymond Chen 建议的那样,将路径替换为 PIDL (SHELLEXECUTEINFO::lpIDList) 会使属性对话框在通过 ShellExecuteEx() 调用时正确显示 Windows 7 下的大小和日期字段。
ShellExecuteEx() 的 Windows 7 实现似乎有问题,因为较新版本的操作系统不存在 SHELLEXCUTEINFO::lpFile 的问题。
还有另一种可能的解决方案,涉及创建IContextMenu 的实例并调用IContextMenu::InvokeCommand() 方法。我想这就是ShellExecuteEx() 在幕后所做的。向下滚动到 Solution 2 示例代码。
解决方案 1 - 使用带有 ShellExecuteEx 的 PIDL
#include <atlcom.h> // CComHeapPtr
#include <shlobj.h> // SHParseDisplayName()
#include <shellapi.h> // ShellExecuteEx()
// CComHeapPtr is a smart pointer that automatically calls CoTaskMemFree() when
// the current scope ends.
CComHeapPtr<ITEMIDLIST> pidl;
SFGAOF sfgao = 0;
// Convert the path into a PIDL.
HRESULT hr = ::SHParseDisplayName( L"D:\\Test.txt", nullptr, &pidl, 0, &sfgao );
if( SUCCEEDED( hr ) )
{
// Show the properties dialog of the file.
SHELLEXECUTEINFO info{ sizeof(info) };
info.hwnd = GetSafeHwnd();
info.nShow = SW_SHOWNORMAL;
info.fMask = SEE_MASK_INVOKEIDLIST;
info.lpIDList = pidl;
info.lpVerb = L"properties";
if( ! ::ShellExecuteEx( &info ) )
{
// Make sure you don't put ANY code before the call to ::GetLastError()
// otherwise the last error value might be invalidated!
DWORD err = ::GetLastError();
// TODO: Do your error handling here.
}
}
else
{
// TODO: Do your error handling here
}
当从简单的基于对话框的 MFC 应用程序的按钮单击处理程序调用时,此代码在 Win 7 和 Win 10(其他版本未测试)下都适用于我。
如果您将info.hwnd 设置为NULL,它也适用于控制台应用程序(只需从示例代码中删除行info.hwnd = GetSafeHwnd();,因为它已经用0 初始化)。在SHELLEXECUTEINFO 参考中指出hwnd 成员是可选的。
不要忘记在应用程序启动时强制调用 CoInitialize() 或 CoInitializeEx() 并在关闭时强制调用 CoUninitialize() 以正确初始化和取消初始化 COM。
注意事项:
CComHeapPtr 是一个包含在 ATL 中的智能指针,它会在作用域结束时自动调用 CoTaskMemFree()。它是一个所有权转移指针,其语义类似于已弃用的std::auto_ptr。也就是说,当你将一个CComHeapPtr对象赋值给另一个对象,或者使用带有CComHeapPtr参数的构造函数时,原来的对象会变成一个NULL指针。
CComHeapPtr<ITEMIDLIST> pidl2( pidl1 ); // pidl1 allocated somewhere before
// Now pidl1 can't be used anymore to access the ITEMIDLIST object.
// It has transferred ownership to pidl2!
我仍在使用它,因为它可以开箱即用,并且可以与 COM API 配合使用。
解决方案 2 - 使用 IContextMenu
以下代码需要 Windows Vista 或更高版本,因为我使用的是“现代”IShellItem API。
我将代码包装到一个函数ShowPropertiesDialog() 中,该函数接受一个窗口句柄和一个文件系统路径。如果发生任何错误,该函数将抛出一个std::system_error 异常。
#include <atlcom.h>
#include <string>
#include <system_error>
/// Show the shell properties dialog for the given filesystem object.
/// \exception Throws std::system_error in case of any error.
void ShowPropertiesDialog( HWND hwnd, const std::wstring& path )
{
using std::system_error;
using std::system_category;
if( path.empty() )
throw system_error( std::make_error_code( std::errc::invalid_argument ),
"Invalid empty path" );
// SHCreateItemFromParsingName() returns only a generic error (E_FAIL) if
// the path is incorrect. We can do better:
if( ::GetFileAttributesW( path.c_str() ) == INVALID_FILE_ATTRIBUTES )
{
// Make sure you don't put ANY code before the call to ::GetLastError()
// otherwise the last error value might be invalidated!
DWORD err = ::GetLastError();
throw system_error( static_cast<int>( err ), system_category(), "Invalid path" );
}
// Create an IShellItem from the path.
// IShellItem basically is a wrapper for an IShellFolder and a child PIDL, simplifying many tasks.
CComPtr<IShellItem> pItem;
HRESULT hr = ::SHCreateItemFromParsingName( path.c_str(), nullptr, IID_PPV_ARGS( &pItem ) );
if( FAILED( hr ) )
throw system_error( hr, system_category(), "Could not get IShellItem object" );
// Bind to the IContextMenu of the item.
CComPtr<IContextMenu> pContextMenu;
hr = pItem->BindToHandler( nullptr, BHID_SFUIObject, IID_PPV_ARGS( &pContextMenu ) );
if( FAILED( hr ) )
throw system_error( hr, system_category(), "Could not get IContextMenu object" );
// Finally invoke the "properties" verb of the context menu.
CMINVOKECOMMANDINFO cmd{ sizeof(cmd) };
cmd.lpVerb = "properties";
cmd.hwnd = hwnd;
cmd.nShow = SW_SHOWNORMAL;
hr = pContextMenu->InvokeCommand( &cmd );
if( FAILED( hr ) )
throw system_error( hr, system_category(),
"Could not invoke the \"properties\" verb from the context menu" );
}
下面我展示了一个如何从 CDialog 派生类的按钮处理程序中使用ShowPropertiesDialog() 的示例。其实ShowPropertiesDialog() 是独立于 MFC 的,因为它只需要一个窗口句柄,但是 OP 提到他想在 MFC 应用程序中使用代码。
#include <sstream>
#include <codecvt>
// Convert a multi-byte (ANSI) string returned from std::system_error::what()
// to Unicode (UTF-16).
std::wstring MultiByteToWString( const std::string& s )
{
std::wstring_convert< std::codecvt< wchar_t, char, std::mbstate_t >> conv;
try { return conv.from_bytes( s ); }
catch( std::range_error& ) { return {}; }
}
// A button click handler.
void CMyDialog::OnPropertiesButtonClicked()
{
std::wstring path( L"c:\\temp\\test.txt" );
// The code also works for the following paths:
//std::wstring path( L"c:\\temp" );
//std::wstring path( L"C:\\" );
//std::wstring path( L"\\\\127.0.0.1\\share" );
//std::wstring path( L"\\\\127.0.0.1\\share\\test.txt" );
try
{
ShowPropertiesDialog( GetSafeHwnd(), path );
}
catch( std::system_error& e )
{
std::wostringstream msg;
msg << L"Could not open the properties dialog for:\n" << path << L"\n\n"
<< MultiByteToWString( e.what() ) << L"\n"
<< L"Error code: " << e.code();
AfxMessageBox( msg.str().c_str(), MB_ICONERROR );
}
}