bool getOpenDirectory(char* out, int max_size, const char* starting_dir) { bool ret = false; IFileDialog* pfd; if (SUCCEEDED(CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)))) { if (starting_dir) { PIDLIST_ABSOLUTE pidl; WCHAR wstarting_dir[MAX_PATH]; WCHAR* wc = wstarting_dir; for (const char *c = starting_dir; *c && wc - wstarting_dir < MAX_PATH - 1; ++c, ++wc) { *wc = *c == '/' ? '\\' : *c; } *wc = 0; HRESULT hresult = ::SHParseDisplayName(wstarting_dir, 0, &pidl, SFGAO_FOLDER, 0); if (SUCCEEDED(hresult)) { IShellItem* psi; hresult = ::SHCreateShellItem(NULL, NULL, pidl, &psi); if (SUCCEEDED(hresult)) { pfd->SetFolder(psi); } ILFree(pidl); } } DWORD dwOptions; if (SUCCEEDED(pfd->GetOptions(&dwOptions))) { pfd->SetOptions(dwOptions | FOS_PICKFOLDERS); } if (SUCCEEDED(pfd->Show(NULL))) { IShellItem* psi; if (SUCCEEDED(pfd->GetResult(&psi))) { WCHAR* tmp; if (SUCCEEDED(psi->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &tmp))) { char* c = out; while (*tmp && c - out < max_size - 1) { *c = (char)*tmp; ++c; ++tmp; } *c = '\0'; ret = true; } psi->Release(); } } pfd->Release(); } return ret; }
bool ShowOpenDirectoryDialog(Path& path) { bool pathSelected = false; // check current OS version OSVERSIONINFO osvi; memset(&osvi, 0, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); IFileDialog *pfd; if (SUCCEEDED(CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)))) { // configure the dialog to Select Folders only DWORD dwOptions; if (SUCCEEDED(pfd->GetOptions(&dwOptions))) { pfd->SetOptions(dwOptions | FOS_PICKFOLDERS | FOS_DONTADDTORECENT); if (SUCCEEDED(pfd->Show(GetActiveWindow()))) { IShellItem *psi; if (SUCCEEDED(pfd->GetResult(&psi))) { LPWSTR lpwszName = NULL; if (SUCCEEDED(psi->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, (LPWSTR*) &lpwszName))) { // Add directory path to the result //ConvertToUnixPath(pathName); path = Path(lpwszName); pathSelected = true; ::CoTaskMemFree(lpwszName); } psi->Release(); } } } pfd->Release(); } return pathSelected; }
static void openFile(HWND hWnd) { HRESULT hr; IFileDialog *pFileDialog = nullptr; IShellItem *pShellItem = nullptr; PWSTR pFilePath = nullptr; DWORD dwFlags; /* Create FileOpenDialog */ hr = CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileDialog)); if (FAILED(hr)) goto FINISH; /* Get options */ hr = pFileDialog->GetOptions(&dwFlags); if (FAILED(hr)) goto FINISH; /* Get shell items only for file system items */ hr = pFileDialog->SetOptions(dwFlags | FOS_FORCEFILESYSTEM); if (FAILED(hr)) goto FINISH; /* Set file types */ COMDLG_FILTERSPEC fileTypes[] = { { L"All supported images", L"*.png;*.jpg;*.jpeg;*.psd" }, }; hr = pFileDialog->SetFileTypes(ARRAYSIZE(fileTypes), fileTypes); if (FAILED(hr)) goto FINISH; /* Show dialog */ hr = pFileDialog->Show(hWnd); if (FAILED(hr)) goto FINISH; hr = pFileDialog->GetResult(&pShellItem); if (FAILED(hr)) goto FINISH; hr = pShellItem->GetDisplayName(SIGDN_FILESYSPATH, &pFilePath); if (FAILED(hr)) goto FINISH; loadImage(hWnd, pFilePath); FINISH: if (pFilePath) CoTaskMemFree(pFilePath); if (pShellItem) pShellItem->Release(); if (pFileDialog) pFileDialog->Release(); }
// IObjectWithSelection IFACEMETHODIMP SetSelection(IShellItemArray *aArray) { if (!aArray) { return E_FAIL; } SetInterface(&mShellItemArray, aArray); DWORD count = 0; aArray->GetCount(&count); if (!count) { return E_FAIL; } #ifdef SHOW_CONSOLE Log(L"SetSelection param count: %d", count); for (DWORD idx = 0; idx < count; idx++) { IShellItem* item = nullptr; if (SUCCEEDED(aArray->GetItemAt(idx, &item))) { LPWSTR str = nullptr; if (FAILED(item->GetDisplayName(SIGDN_FILESYSPATH, &str))) { if (FAILED(item->GetDisplayName(SIGDN_URL, &str))) { Log(L"Failed to get a shell item array item."); item->Release(); continue; } } item->Release(); Log(L"SetSelection param: '%s'", str); CoTaskMemFree(str); } } #endif IShellItem* item = nullptr; if (FAILED(aArray->GetItemAt(0, &item))) { return E_FAIL; } bool isFileSystem = false; if (!SetTargetPath(item) || !mTarget.GetLength()) { Log(L"SetTargetPath failed."); return E_FAIL; } item->Release(); Log(L"SetSelection target: %s", mTarget); return S_OK; }
HRESULT ContextMenu::DisplayItems(IShellItemArray *psia, HWND hwndParent) { DWORD count; psia->GetCount(&count); for(DWORD i = 0; i < count; ++i) { IShellItem *psi; HRESULT hr = psia->GetItemAt(i, &psi); if (SUCCEEDED(hr)) { PWSTR pszDisplayName; hr = psi->GetDisplayName(SIGDN_NORMALDISPLAY, &pszDisplayName); if (SUCCEEDED(hr)) { MessageBoxW(hwndParent, pszDisplayName, pszDisplayName, MB_OK); CoTaskMemFree(pszDisplayName); } psi->Release(); } else { return hr; } } return S_OK; }
void ChangeDropImageType(DROPIMAGETYPE newType) { if (newType != _dropImageType) { _dropImageType = newType; if (_pdtobj != nullptr) { IShellItem *psi; HRESULT hr = CreateItemFromObject(_pdtobj, IID_PPV_ARGS(&psi)); if (SUCCEEDED(hr)) { PWSTR pszName; hr = psi->GetDisplayName(SIGDN_NORMALDISPLAY/*SIGDN_FILESYSPATH*/, &pszName); if (SUCCEEDED(hr)) { SetDropTip(_pdtobj, _dropImageType, _pszDropTipTemplate ? _pszDropTipTemplate : L"%1", pszName); CoTaskMemFree(pszName); } psi->Release(); } else { auto strName = GetDisplayFullNameFromObject(_pdtobj); if (!strName.IsEmpty()) { SetDropTip(_pdtobj, _dropImageType, _pszDropTipTemplate ? _pszDropTipTemplate : L"%1", strName); } } } } }
PWSTR LoadFile() { IFileOpenDialog *pFileOpen; PWSTR pszFilePath = NULL; // Create the FileOpenDialog object. HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); if (SUCCEEDED(hr)) { //IShellItem *psiDocuments = NULL; //hr = SHCreateItemInKnownFolder(FOLDERID_Documents, 0, NULL, IID_PPV_ARGS(&psiDocuments)); //if (SUCCEEDED(hr)) { // hr = pFileOpen->SetFolder(psiDocuments); // psiDocuments->Release(); //} // Show the Open dialog box. hr = pFileOpen->Show(NULL); // Get the file name from the dialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); pItem->Release(); } } pFileOpen->Release(); } return pszFilePath; }
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; }
char *commonItemDialog(HWND parent, REFCLSID clsid, REFIID iid, FILEOPENDIALOGOPTIONS optsadd) { IFileDialog *d = NULL; FILEOPENDIALOGOPTIONS opts; IShellItem *result = NULL; WCHAR *wname = NULL; char *name = NULL; HRESULT hr; hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, iid, (LPVOID *) (&d)); if (hr != S_OK) { logHRESULT(L"error creating common item dialog", hr); // always return NULL on error goto out; } hr = d->GetOptions(&opts); if (hr != S_OK) { logHRESULT(L"error getting current options", hr); goto out; } opts |= optsadd; // the other platforms don't check read-only; we won't either opts &= ~FOS_NOREADONLYRETURN; hr = d->SetOptions(opts); if (hr != S_OK) { logHRESULT(L"error setting options", hr); goto out; } hr = d->Show(parent); if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) // cancelled; return NULL like we have ready goto out; if (hr != S_OK) { logHRESULT(L"error showing dialog", hr); goto out; } hr = d->GetResult(&result); if (hr != S_OK) { logHRESULT(L"error getting dialog result", hr); goto out; } hr = result->GetDisplayName(SIGDN_FILESYSPATH, &wname); if (hr != S_OK) { logHRESULT(L"error getting filename", hr); goto out; } name = toUTF8(wname); out: if (wname != NULL) CoTaskMemFree(wname); if (result != NULL) result->Release(); if (d != NULL) d->Release(); return name; }
bool Gwen::Platform::FolderOpen( const String& Name, const String& StartPath, Gwen::Event::Handler* pHandler, Event::Handler::FunctionWithInformation fnCallback ) { IFileDialog *pfd = NULL; bool bSuccess = false; if ( CoCreateInstance( CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS( &pfd ) ) != S_OK ) return bSuccess; DWORD dwOptions; if ( pfd->GetOptions(&dwOptions) != S_OK ) { pfd->Release(); return bSuccess; } pfd->SetOptions( dwOptions | FOS_PICKFOLDERS ); // // TODO: SetDefaultFolder -> StartPath // if ( pfd->Show(NULL) == S_OK ) { IShellItem *psi; if ( pfd->GetResult(&psi) == S_OK ) { WCHAR* strOut = NULL; if ( psi->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &strOut ) != S_OK ) { return bSuccess; } // // GWEN callback - call it. // if ( pHandler && fnCallback ) { Gwen::Event::Information info; info.Control = NULL; info.ControlCaller = NULL; info.String = Gwen::Utility::UnicodeToString( strOut ); (pHandler->*fnCallback)( info ); } CoTaskMemFree( strOut ); psi->Release(); bSuccess = true; } } pfd->Release(); return bSuccess; }
void OnTakePhoto(HWND hwnd) { wchar_t filename[MAX_PATH]; // Get the path to the Documents folder. IShellItem *psi = NULL; PWSTR pszFolderPath = NULL; HRESULT hr = SHCreateItemInKnownFolder(FOLDERID_Documents, 0, NULL, IID_PPV_ARGS(&psi)); if (FAILED(hr)) { goto done; } hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath); if (FAILED(hr)) { goto done; } // Construct a file name based on the current time. SYSTEMTIME time; GetLocalTime(&time); hr = StringCchPrintf(filename, MAX_PATH, L"MyPhoto%04u_%02u%02u_%02u%02u%02u.jpg", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond); if (FAILED(hr)) { goto done; } LPTSTR path = PathCombine(PhotoFileName, pszFolderPath, filename); if (path == NULL) { hr = E_FAIL; goto done; } hr = g_pEngine->TakePhoto(path); if (FAILED(hr)) { goto done; } _SetStatusText(path); done: SafeRelease(&psi); CoTaskMemFree(pszFolderPath); if (FAILED(hr)) { ShowError(hwnd, IDS_ERR_PHOTO, hr); } UpdateUI(hwnd); }
HRESULT v_ExecuteLibCommand() { // Get the private and public save locations. IShellItem *psiPrivateSaveLoc; HRESULT hr = _plib->GetDefaultSaveFolder(DSFT_PRIVATE, IID_PPV_ARGS(&psiPrivateSaveLoc)); if (SUCCEEDED(hr)) { IShellItem *psiPublicSaveLoc; hr = _plib->GetDefaultSaveFolder(DSFT_PUBLIC, IID_PPV_ARGS(&psiPublicSaveLoc)); if (SUCCEEDED(hr)) { // Get the list of folders that match the specified filter. IShellItemArray *psiaFolders; hr = _plib->GetFolders(_lffFilter, IID_PPV_ARGS(&psiaFolders)); if (SUCCEEDED(hr)) { DWORD cFolders; hr = psiaFolders->GetCount(&cFolders); if (SUCCEEDED(hr)) { Output(L"Library contains %u folders:\n", cFolders); for (DWORD iFolder = 0; iFolder < cFolders; iFolder++) { IShellItem *psiFolder; if (SUCCEEDED(psiaFolders->GetItemAt(iFolder, &psiFolder))) { // Print each folder's name as an absolute path, suitable for parsing in the Shell Namespace (e.g SHParseDisplayName). // For file system folders (the typical case), this will be the file system path of the folder. PWSTR pszDisplay; if (SUCCEEDED(psiFolder->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &pszDisplay))) { PCWSTR pszPrefix = L" "; int iCompare; if (S_OK == psiPrivateSaveLoc->Compare(psiFolder, SICHINT_CANONICAL | SICHINT_TEST_FILESYSPATH_IF_NOT_EQUAL, &iCompare)) { pszPrefix = L"* "; } else if (S_OK == psiPublicSaveLoc->Compare(psiFolder, SICHINT_CANONICAL | SICHINT_TEST_FILESYSPATH_IF_NOT_EQUAL, &iCompare)) { pszPrefix = L"# "; } Output(L"%s%s\n", pszPrefix, pszDisplay); CoTaskMemFree(pszDisplay); } psiFolder->Release(); } } } psiaFolders->Release(); } psiPublicSaveLoc->Release(); } psiPrivateSaveLoc->Release(); } return hr; }
const bool Core::select_file() { //Open file dialog, straight from https://msdn.microsoft.com/en-us/library/windows/desktop/ff485843(v=vs.85).aspx HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); PWSTR pszFilePath = nullptr; if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; // Create the FileOpenDialog object. hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); if (SUCCEEDED(hr)) { //Boilerplate for only showing *.nes files in the dialog. See the MSDN docs more info. COMDLG_FILTERSPEC fileFilter; fileFilter.pszName = L"iNES"; fileFilter.pszSpec = L"*.nes"; pFileOpen->SetFileTypes(1, &fileFilter); pFileOpen->SetFileTypeIndex(1); pFileOpen->SetDefaultExtension(L"nes"); // Show the Open dialog box. hr = pFileOpen->Show(NULL); // Get the file name from the dialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); if (SUCCEEDED(hr)) { logmsg("Opening file"); } else { pszFilePath = nullptr; } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } if (pszFilePath == nullptr) { alert_error("Unable to open file! File must have the extension \".nes\""); return false; } //Convert wchar_t string to char string std::size_t i; wcstombs_s(&i, fileSelection, pszFilePath, MAX_PATH); return true; }
// Open an audio/video file. void OnFileOpen(HWND hwnd) { IFileOpenDialog *pFileOpen = NULL; IShellItem *pItem = NULL; PWSTR pszFilePath = NULL; // Create the FileOpenDialog object. HRESULT hr = CoCreateInstance(__uuidof(FileOpenDialog), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpen)); if (FAILED(hr)) { goto done; } // Show the Open dialog box. hr = pFileOpen->Show(NULL); if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) { // The user canceled the dialog. Do not treat as an error. hr = S_OK; goto done; } else if (FAILED(hr)) { goto done; } // Get the file name from the dialog box. hr = pFileOpen->GetResult(&pItem); if (FAILED(hr)) { goto done; } hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); if (FAILED(hr)) { goto done; } // Display the file name to the user. hr = g_pPlayer->OpenURL(pszFilePath); if (SUCCEEDED(hr)) { UpdateUI(hwnd, OpenPending); } done: if (FAILED(hr)) { NotifyError(hwnd, L"Could not open the file.", hr); UpdateUI(hwnd, Closed); } CoTaskMemFree(pszFilePath); SafeRelease(&pItem); SafeRelease(&pFileOpen); }
STDMETHODIMP COpenFileListener::OnFileOk(IFileDialog* pfd) { IShellItemArray *psiaResults; IFileDialogCustomize *pfdc; HRESULT hr; IFileOpenDialog *fod; FILEINFO fileinfoTmp = {0}; DWORD choice; fileinfoTmp.parentList = pFInfoList; hr = pfd->QueryInterface(IID_PPV_ARGS(&fod)); if(SUCCEEDED(hr)) { hr = fod->GetSelectedItems(&psiaResults); if (SUCCEEDED(hr)) { DWORD fileCount; IShellItem *isi; LPWSTR pwsz = NULL; psiaResults->GetCount(&fileCount); for(DWORD i=0;i<fileCount;i++) { psiaResults->GetItemAt(i,&isi); isi->GetDisplayName(SIGDN_FILESYSPATH,&pwsz); isi->Release(); fileinfoTmp.szFilename = pwsz; pFInfoList->fInfos.push_back(fileinfoTmp); CoTaskMemFree(pwsz); } psiaResults->Release(); } fod->Release(); } hr = pfd->QueryInterface(IID_PPV_ARGS(&pfdc)); if(SUCCEEDED(hr)) { hr = pfdc->GetSelectedControlItem(FDIALOG_OPENCHOICES,&choice); if(SUCCEEDED(hr)) { if(choice==FDIALOG_CHOICE_REPARENT) { pFInfoList->uiCmdOpts = CMD_REPARENT; } else if(choice==FDIALOG_CHOICE_ALLHASHES) { pFInfoList->uiCmdOpts = CMD_ALLHASHES; } else if(choice==FDIALOG_CHOICE_BSD) { pFInfoList->uiCmdOpts = CMD_FORCE_BSD; } } pfdc->Release(); } return S_OK; }
QString qt_win_CID_get_existing_directory(const QFileDialogArgs &args) { QString result; QDialog modal_widget; modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true); modal_widget.setParent(args.parent, Qt::Window); QApplicationPrivate::enterModal(&modal_widget); IFileOpenDialog *pfd = 0; HRESULT hr = CoCreateInstance(QT_CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, QT_IID_IFileOpenDialog, reinterpret_cast<void**>(&pfd)); if (SUCCEEDED(hr)) { qt_win_set_IFileDialogOptions(pfd, args.selection, args.directory, args.caption, QStringList(), QFileDialog::ExistingFile, args.options); // Set the FOS_PICKFOLDERS flag DWORD newOptions; hr = pfd->GetOptions(&newOptions); newOptions |= (FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM); if (SUCCEEDED(hr) && SUCCEEDED((hr = pfd->SetOptions(newOptions)))) { QWidget *parentWindow = args.parent; if (parentWindow) parentWindow = parentWindow->window(); else parentWindow = QApplication::activeWindow(); // Show the file dialog. hr = pfd->Show(parentWindow ? parentWindow->winId() : 0); if (SUCCEEDED(hr)) { // Retrieve the result IShellItem *psi = 0; hr = pfd->GetResult(&psi); if (SUCCEEDED(hr)) { // Retrieve the file name from shell item. wchar_t *pszPath; hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &pszPath); if (SUCCEEDED(hr)) { result = QString::fromWCharArray(pszPath); CoTaskMemFree(pszPath); } psi->Release(); // Free the current item. } } } } QApplicationPrivate::leaveModal(&modal_widget); qt_win_eatMouseMove(); if (pfd) pfd->Release(); return result; }
// You can get some information from IKnownFolder that you cannot get from // the KNOWNFOLDER_DEFINITION. void DumpKnownFolderInfo(IKnownFolder *pkf) { KNOWNFOLDERID kfid = GUID_NULL; HRESULT hr = pkf->GetId(&kfid); if (SUCCEEDED(hr)) { KNOWNFOLDER_DEFINITION kfd; ZeroMemory(&kfd, sizeof(kfd)); hr = pkf->GetFolderDefinition(&kfd); if (SUCCEEDED(hr)) { WCHAR szGuid[GUID_SIZE]; StringFromGUID2(kfid, szGuid, ARRAYSIZE(szGuid)); wprintf(L"IKnownFolder info for %s (%s)\n", kfd.pszName, szGuid); PWSTR pszPath = NULL; hr = pkf->GetPath(0, &pszPath); if (SUCCEEDED(hr)) { wprintf(L"\tCurrent Path : %s\n", pszPath); CoTaskMemFree(pszPath); } else { wprintf(L"\tERROR: IKnownFolder::GetPath() returned hr=0x%x\n", hr); } IShellItem *psi; hr = pkf->GetShellItem(0, IID_PPV_ARGS(&psi)); if (SUCCEEDED(hr)) { PWSTR psz; hr = psi->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &psz); if (SUCCEEDED(hr)) { wprintf(L"\tCurrent Location: %s\n", psz); CoTaskMemFree(psz); } psi->Release(); } else { wprintf(L"\tERROR: IKnownFolder::GetLocation() returned hr=0x%x\n", hr); } } else { wprintf(L"\tERROR: IKnownFolderManager::GetFolderDefinition() failed. hr=0x%x\n", hr); } } else { wprintf(L"\tERROR: IKnownFolder::GetId() failed. hr=0x%x\n", hr); } }
void OnFileOpen(HWND hwnd) { HRESULT hr = S_OK; IFileDialog *pDialog = NULL; IShellItem *pItem = NULL; LPWSTR pwszFilePath = NULL; // Create the FileOpenDialog object. hr = CoCreateInstance( __uuidof(FileOpenDialog), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDialog) ); if (FAILED(hr)) { goto done; } hr = pDialog->SetTitle(L"Select a File to Play"); if (FAILED(hr)) { goto done; } // Show the file-open dialog. hr = pDialog->Show(hwnd); if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) { // User cancelled. hr = S_OK; goto done; } if (FAILED(hr)) { goto done; } hr = pDialog->GetResult(&pItem); if (FAILED(hr)) { goto done; } hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pwszFilePath); if (FAILED(hr)) { goto done; } // Open the file and create bitmaps. hr = OpenVideoFile(hwnd, pwszFilePath); done: if (FAILED(hr)) { ShowErrorMessage(L"Cannot open file.", hr); } CoTaskMemFree(pwszFilePath); SafeRelease(&pItem); SafeRelease(&pDialog); }
void FileOpen::showDialog() { #ifdef _WIN32 path = NULL; HRESULT hResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hResult)) { //Create a instance to open the fileOpenDialog IFileOpenDialog *pFileOpen; hResult = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpen)); if (SUCCEEDED(hResult)) { //show the File Open hResult = pFileOpen->Show(NULL); //get the file name result if (SUCCEEDED(hResult)) { IShellItem *pItem; hResult = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hResult)) { PWSTR FilePath; hResult = pItem->GetDisplayName(SIGDN_FILESYSPATH, &FilePath); //get the file name and copy to "path" if (SUCCEEDED(hResult)) { int count = wcslen(FilePath); path = new char[2*count]; wcstombs(path, FilePath, count); path[count] = '\0'; CoTaskMemFree(FilePath); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } #else std::cout << "Sorry, this is not implemented on linux yet =(" << std::endl; return; #endif }
IFACEMETHODIMP OnSelectionChange(IFileDialog *fd) { IShellItem *si = NULL; fd->GetFolder(&si); scope_defer([&](){ si->Release(); }); PWSTR path = NULL; if (SUCCEEDED(si->GetDisplayName(SIGDN_FILESYSPATH, &path))) { *dir = path; ::CoTaskMemFree(path); } DBGW(L"OnSelectionChange(%s)\n", dir->s()); return S_OK; }
BOOL ReadFromFile() { IFileOpenDialog *pDlg; COMDLG_FILTERSPEC FileTypes[] = { { L"PS2 MemoryCard files", L"*.ps2" }, { L"All files", L"*.*" } }; HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDlg)); WCHAR cPath[MAX_PATH] = L""; GetCurrentDirectoryW(sizeof(cPath) / sizeof(cPath[0]), cPath); IShellItem *psiFolder, *psiParent; SHCreateItemFromParsingName(cPath, NULL, IID_PPV_ARGS(&psiFolder)); psiFolder->GetParent(&psiParent); //初期フォルダの指定 pDlg->SetFolder(psiFolder); //フィルターの指定 pDlg->SetFileTypes(_countof(FileTypes), FileTypes); //ダイアログ表示 hr = pDlg->Show(NULL); //ファイル名 LPOLESTR pwsz = NULL; if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pDlg->GetResult(&pItem); if (SUCCEEDED(hr)) { hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pwsz); if (SUCCEEDED(hr)) { HANDLE hFile; hFile = CreateFile(pwsz, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (hFile) { DWORD BytesRead; BOOL b = ReadFile(hFile, &(byteMemDat.Byte), sizeof(byteMemDat), &BytesRead, NULL); if (BytesRead) { CloseHandle(hFile); } } } } } UpdateDataList(&byteMemDat); return TRUE; }
void gameOfLife3D::MainWnd::OnSaveFileAs() { IFileSaveDialog* pFileSaveDialog = nullptr; HRESULT hr = CoCreateInstance(CLSID_FileSaveDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileSaveDialog)); if (SUCCEEDED(hr)) { hr = pFileSaveDialog->SetFileTypes(NUMBER_OF_DIALOG_FILTERS, DIALOG_FILTERS); } if (SUCCEEDED(hr)) { hr = pFileSaveDialog->Show(m_hwnd); } IShellItem *pResult = nullptr; if (SUCCEEDED(hr)) { hr = pFileSaveDialog->GetResult(&pResult); } WCHAR *pPath = nullptr; if (SUCCEEDED(hr)) { hr = pResult->GetDisplayName(SIGDN_FILESYSPATH, &pPath); } UINT fileTypeIndex = 0; if (SUCCEEDED(hr)) { hr = pFileSaveDialog->GetFileTypeIndex(&fileTypeIndex); } if (SUCCEEDED(hr)) { auto lifeFile = std::make_shared<gameOfLife3D::io::LifeFile>(); m_canvasPanel->GetLifeFile(lifeFile); std::wstring fileName(pPath); if (fileTypeIndex == 0) { std::wregex rx(L".*\\" L".l3d"); bool ret = std::regex_match(fileName.begin(), fileName.end(), rx); if (!ret) { fileName += L".l3d"; } lifeFile->Save(fileName, gameOfLife3D::io::LIFEFILE_FORMAT_LIFE3D100); } else { std::wregex rx(L".*\\" L".lif"); bool ret = std::regex_match(fileName.begin(), fileName.end(), rx); if (!ret) { fileName += L".lif"; } lifeFile->Save(fileName, gameOfLife3D::io::LIFEFILE_FORMAT_LIFE106); } } if (pPath != nullptr) { CoTaskMemFree(pPath); } SafeRelease(&pResult); SafeRelease(&pFileSaveDialog); }
string getSavePathWithDialog() { string path = ""; HRESULT hr = CoInitializeEx(NULL, COINITBASE_MULTITHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileSaveDialog *pFileSave; // Create the FileOpenDialog object. hr = CoCreateInstance(CLSID_FileSaveDialog, NULL, CLSCTX_ALL, IID_IFileSaveDialog, reinterpret_cast<void**>(&pFileSave)); if (SUCCEEDED(hr)) { // Set default extension hr = pFileSave->SetDefaultExtension(L"n3s"); if (SUCCEEDED(hr)) { // Show the Open dialog box. hr = pFileSave->Show(NULL); // Get the file name from the dialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileSave->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTR pszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file name to the user. if (SUCCEEDED(hr)) { //MessageBox(NULL, pszFilePath, L"File Path", MB_OK); char buffer[500]; wcstombs(buffer, pszFilePath, 500); path = buffer; CoTaskMemFree(pszFilePath); } pItem->Release(); } } pFileSave->Release(); } } CoUninitialize(); } return path; }
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) { switch (id) { case ID_FILE_OPEN: { IFileDialog *pFileDialog = NULL; HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileDialog)); if (SUCCEEDED(hr)) { COMDLG_FILTERSPEC filter[3]; filter[0].pszName = L"位图文件(*.bmp)"; filter[0].pszSpec = L"*.bmp"; filter[1].pszName = L"JPEG(*.jpg;*.jpeg)"; filter[1].pszSpec = L"*.jpg;*.jpeg"; filter[2].pszName = L"所有支持的文件(*.jpg;*.jpeg;*.bmp;*.png)"; filter[2].pszSpec = L"*.jpg;*.jpeg;*.bmp;*.png"; hr = pFileDialog->SetFileTypes(3, filter); hr = pFileDialog->SetFileTypeIndex(3); hr = pFileDialog->Show(hwnd); if (SUCCEEDED(hr)) { IShellItem *psi = NULL; hr = pFileDialog->GetResult(&psi); if (SUCCEEDED(hr)) { PWSTR pwszFilePath = NULL; hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &pwszFilePath); if (SUCCEEDED(hr)) { wchar_t * pext = wcsrchr(pwszFilePath, L'.'); if(pext != NULL && _wcsicmp(pext, L".bmp") == 0) SendMessage(hwnd, WM_USER_IMG, 0, (LPARAM)pwszFilePath); else SendMessage(hwnd, WM_USER_IMG, 1, (LPARAM)pwszFilePath); CoTaskMemFree(pwszFilePath); } psi->Release(); } } } pFileDialog->Release(); break; } case ID_FILE_EXIT: PostQuitMessage(0); break; default: FORWARD_WM_COMMAND(hwnd, id, hwndCtl, codeNotify, DefWindowProc); } }
void EditorScreen::LoadMap() { HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen = NULL; HRESULT hr = CoCreateInstance(__uuidof(FileOpenDialog), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpen)); if (SUCCEEDED(hr)) { hr = pFileOpen->SetDefaultExtension(L"xml"); hr = pFileOpen->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes); hr = pFileOpen->Show(NULL); if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTR pszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file name to the user. if (SUCCEEDED(hr)) { // Reset current Map information ResetMap(); std::string filePath = utf8_encode(pszFilePath); map->LoadMap(filePath); CoTaskMemFree(pszFilePath); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } }
HRESULT DisplayItem(IShellItemArray *psia, HWND hwnd) { // Get the first ShellItem and display its name IShellItem *psi; HRESULT hr = psia->GetItemAt(0, &psi); if (SUCCEEDED(hr)) { PWSTR pszDisplayName; hr = psi->GetDisplayName(SIGDN_NORMALDISPLAY, &pszDisplayName); if (SUCCEEDED(hr)) { MessageBox(hwnd, pszDisplayName, pszDisplayName, MB_OK); CoTaskMemFree(pszDisplayName); } psi->Release(); } return hr; }
extern "C" LXCWIN_API HRESULT fileSaveDialog(HWND hWnd, LPWSTR dialogTitle, LPWSTR *result) { *result = NULL; HRESULT hr = S_OK; CoInitialize(nullptr); IFileOpenDialog *pfd = NULL;// yes, *open*, since this dialog selects an existing parent dir, not a new file hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)); if (SUCCEEDED(hr)) { // set default folder to "My Documents" IShellItem *psiDocuments = NULL; hr = SHCreateItemInKnownFolder(FOLDERID_Documents, 0, NULL, IID_PPV_ARGS(&psiDocuments)); if (SUCCEEDED(hr)) { hr = pfd->SetDefaultFolder(psiDocuments); psiDocuments->Release(); } // dialog title pfd->SetTitle(dialogTitle); // ok button text pfd->SetOkButtonLabel(L"Choose target"); // only choose directories DWORD dwOptions; hr = pfd->GetOptions(&dwOptions); if (SUCCEEDED(hr)) { pfd->SetOptions(dwOptions | FOS_PICKFOLDERS); } // show the save dialog hr = pfd->Show(hWnd); if (SUCCEEDED(hr)) { IShellItem *psiaResult; hr = pfd->GetResult(&psiaResult); if (SUCCEEDED(hr)) { psiaResult->GetDisplayName(SIGDN_FILESYSPATH, &(*result)); psiaResult->Release(); } } pfd->Release(); } return hr; }
HRESULT v_ExecuteLibCommand() { // Attempt to resolve the folder. An IShellItem representing the updated target location is returned. IShellItem *psiResolved; HRESULT hr = _plib->ResolveFolder(_psiFolder, _dwTimeout, IID_PPV_ARGS(&psiResolved)); if (SUCCEEDED(hr)) { PWSTR pszResolvedPath; psiResolved->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &pszResolvedPath); Output(L"Resolved folder path %s to: %s\n", _pszFolderPath, pszResolvedPath); CoTaskMemFree(pszResolvedPath); psiResolved->Release(); } else { RuntimeError(L"Error %#08x resolving folder %s from the library.\n", hr, _pszFolderPath); } return hr; }
PWSTR openUserFileDiaglog() { PWSTR pszFilePath = NULL; HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen = NULL; // Create the FileOpenDialog object. hr = CoCreateInstance( CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, (void**)&pFileOpen); if (SUCCEEDED(hr)) { // Show the Open dialog box. hr = pFileOpen->Show(NULL); // Get the file name from the dialog box. if (SUCCEEDED(hr)) { IShellItem *pItem = NULL; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } return pszFilePath; }
extern "C" LXCWIN_API HRESULT fileOpenDialog(HWND hWnd, DWORD *count, LPWSTR **result) { *result = NULL; HRESULT hr = S_OK; CoInitialize(nullptr); IFileOpenDialog *pfd = NULL; hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)); if (SUCCEEDED(hr)) { // set default folder to "My Documents" IShellItem *psiDocuments = NULL; hr = SHCreateItemInKnownFolder(FOLDERID_Documents, 0, NULL, IID_PPV_ARGS(&psiDocuments)); if (SUCCEEDED(hr)) { hr = pfd->SetDefaultFolder(psiDocuments); psiDocuments->Release(); } // dialog title pfd->SetTitle(L"Select files to share"); // allow multiselect, restrict to real files DWORD dwOptions; hr = pfd->GetOptions(&dwOptions); if (SUCCEEDED(hr)) { // ideally, allow selecting folders as well as files, but IFileDialog does not support this :( pfd->SetOptions(dwOptions | FOS_ALLOWMULTISELECT | FOS_FORCEFILESYSTEM); // | FOS_PICKFOLDERS } // do not limit to certain file types // show the open file dialog hr = pfd->Show(hWnd); if (SUCCEEDED(hr)) { IShellItemArray *psiaResults; hr = pfd->GetResults(&psiaResults); if (SUCCEEDED(hr)) { hr = psiaResults->GetCount(count); if (SUCCEEDED(hr)) { *result = (LPWSTR*)calloc(*count, sizeof(LPWSTR)); if (*result != NULL) { for (DWORD i = 0; i < *count; i++) { IShellItem *resultItem = NULL; hr = psiaResults->GetItemAt(i, &resultItem); if (SUCCEEDED(hr)) { resultItem->GetDisplayName(SIGDN_FILESYSPATH, &((*result)[i])); resultItem->Release(); } } // paths now contains selected files } } psiaResults->Release(); } } pfd->Release(); } return hr; }