//初始化Com接口
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
//创建pOpenFileDlg
IFileDialog* pOpenFileDlg = NULL;
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pOpenFileDlg));
if (SUCCEEDED(hr))
{
//显示打开对话框
hr = pOpenFileDlg->Show(NULL);
//从对话框中获取的文件路径抽象到IShellItem对象中
if (SUCCEEDED(hr))
{
IShellItem* pItem = NULL;
hr = pOpenFileDlg->GetResult(&pItem);
//从抽象接口中获取名字
if (SUCCEEDED(hr))
{
PWSTR pszFilePath = NULL;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
//获取成功则 pszFilePath就是对应的路径 ,下方写对应的代码即可。
if (SUCCEEDED(hr))
{
m_edtFilePath = pszFilePath;
UpdateData(FALSE);
//释放抽象对象内存
CoTaskMemFree(pszFilePath);
}
pItem->Release();
}
}
pOpenFileDlg->Release();
}
}
CoUninitialize();