std::vector<std::wstring> APlayerWindow::showOpenFile() { HRESULT hr = S_OK; std::vector<std::wstring> filePaths; IFileOpenDialog *fileDlg = NULL; hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fileDlg)); if (FAILED(hr)) return filePaths; ON_SCOPE_EXIT([&] { fileDlg->Release(); }); IKnownFolderManager *pkfm = NULL; hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm)); if (FAILED(hr)) return filePaths; ON_SCOPE_EXIT([&] { pkfm->Release(); }); IKnownFolder *pKnownFolder = NULL; hr = pkfm->GetFolder(FOLDERID_PublicMusic, &pKnownFolder); if (FAILED(hr)) return filePaths; ON_SCOPE_EXIT([&] { pKnownFolder->Release(); }); IShellItem *psi = NULL; hr = pKnownFolder->GetShellItem(0, IID_PPV_ARGS(&psi)); if (FAILED(hr)) return filePaths; ON_SCOPE_EXIT([&] { psi->Release(); }); hr = fileDlg->AddPlace(psi, FDAP_BOTTOM); COMDLG_FILTERSPEC rgSpec[] = { { L"ÒôÀÖÎļþ", SupportType } }; fileDlg->SetFileTypes(1, rgSpec); DWORD dwOptions; fileDlg->GetOptions(&dwOptions); fileDlg->SetOptions(dwOptions | FOS_ALLOWMULTISELECT); hr = fileDlg->Show(NULL); if (SUCCEEDED(hr)) { IShellItemArray *pRets; hr = fileDlg->GetResults(&pRets); if (SUCCEEDED(hr)) { DWORD count; pRets->GetCount(&count); for (DWORD i = 0; i < count; i++) { IShellItem *pRet; LPWSTR nameBuffer; pRets->GetItemAt(i, &pRet); pRet->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &nameBuffer); filePaths.push_back(std::wstring(nameBuffer)); pRet->Release(); CoTaskMemFree(nameBuffer); } pRets->Release(); } } return filePaths; }
// Processes a single argument which identifies the library to operate on; passes any remaining arguments to the derived class. HRESULT v_ProcessArguments(PCWSTR *ppszArgs, int cArgs) { PCWSTR pszLibPath = CONSUME_NEXT_ARG(ppszArgs, cArgs); HRESULT hr = pszLibPath ? S_OK : E_INVALIDARG; if (SUCCEEDED(hr)) { if (_fCreate) { // When creating a new library, interpret the argument as the file system path to save the library to. WCHAR szAbsPath[MAX_PATH]; hr = SHStrDupW(_wfullpath(szAbsPath, pszLibPath, ARRAYSIZE(szAbsPath)), &_pszSavePath); } else { // Check for the 'FOLDERID_' prefix, which indicates that the argument should be interpreted as a KNOWNFOLDERID. const WCHAR szPrefix[] = L"FOLDERID_"; const UINT cchPrefix = ARRAYSIZE(szPrefix) - 1; if (StrCmpNCW(pszLibPath, szPrefix, cchPrefix) == 0) { IKnownFolderManager *pkfm; hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm)); if (SUCCEEDED(hr)) { // KNOWNFOLDERIDs are GUIDs, but they have a corresponding canonical name which is a string. // By convention, the canonical name is the same as the name of the KNOWNFOLDERID #define. // That is, FOLDERID_DocumentsLibrary => "DocumentsLibrary". So, skip the prefix and pass // the remainder to GetFolderByName to retrieve the known folder. IKnownFolder *pkf; hr = pkfm->GetFolderByName(pszLibPath + cchPrefix, &pkf); if (SUCCEEDED(hr)) { hr = pkf->GetShellItem(KF_FLAG_INIT, IID_PPV_ARGS(&_psiLibrary)); pkf->Release(); } pkfm->Release(); } } else { // Default - interpret the argument as a file system path, and create a shell item for it. WCHAR szAbsPath[MAX_PATH]; hr = SHCreateItemFromParsingName(_wfullpath(szAbsPath, pszLibPath, ARRAYSIZE(szAbsPath)), NULL, IID_PPV_ARGS(&_psiLibrary)); } } } else { ParseError(L"Missing library path argument.\n"); } if (SUCCEEDED(hr)) { // Allow derived command to process any remaining arguments. hr = v_ProcessLibArguments(ppszArgs, cArgs); } return hr; }