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;
}
Ejemplo n.º 2
2
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;
}
Ejemplo n.º 3
1
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();
}
void PickContainer()
{
    IFileDialog *pfd;
    if (SUCCEEDED(CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd))))
    {
        DWORD dwOptions;
        if (SUCCEEDED(pfd->GetOptions(&dwOptions)))
        {
            pfd->SetOptions(dwOptions | FOS_PICKFOLDERS);
        }

        if (SUCCEEDED(pfd->Show(NULL)))
        {
            IShellItem *psi;
            if (SUCCEEDED(pfd->GetResult(&psi)))
            {
                PWSTR pszPath;
                if (SUCCEEDED(GetIDListName(psi, &pszPath)))
                {
                    MessageBox(NULL, pszPath, L"Selected Container", MB_OK);
                    CoTaskMemFree(pszPath);
                }
                psi->Release();
            }
        }
        pfd->Release();
    }
}
Ejemplo n.º 5
0
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;
}
Ejemplo n.º 6
0
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;
}
Ejemplo n.º 7
0
	bool getOpenDirectory(char* out, int max_size)
	{
		bool ret = false;
		IFileDialog *pfd;
		if (SUCCEEDED(CoCreateInstance(
				CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd))))
		{
			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;
	}
Ejemplo n.º 8
0
string get_directory(string dname, string caption)
{
//NOTE: This uses the Windows Vista or later file chooser, which is different than the one used by GM8 and lower
//because I could not find out which one it uses, since IFileDialog is used by both wxWidgets and QtFramework
//and there doesn't appear to be a standard file picker for XP or lower in the Windows API except SHBrowseForFolder that is
//used by Game Maker for get_directory_alt
	IFileDialog* fileDialog;
	CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, 
		IID_PPV_ARGS(&fileDialog));

	DWORD options;
	fileDialog->GetOptions(&options);

	options &= ~FOS_FILEMUSTEXIST;  
	options &= ~FOS_PATHMUSTEXIST;
	fileDialog->SetOptions(options | FOS_PICKFOLDERS);
	//TODO: Set default directory to dname
	fileDialog->SetTitle(std::wstring(caption.begin(), caption.end()).c_str());
	
	fileDialog->Show(enigma::hWnd);
	
	string res = "";
	IShellItem *psi;
	
	if (SUCCEEDED(fileDialog->GetResult(&psi))) {
		LPWSTR wideres;
		psi->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &wideres);
		psi->Release();
		
		//TODO: F**k Microsoft with a wooden spoon
		std::wstring wtf = wideres;
		res = string(wtf.begin(), wtf.end());
	}
	
	return res;
}
Ejemplo n.º 9
0
/*
	ANT_CANVAS::LOAD_FILE()
	-----------------------
*/
long long ANT_canvas::load_file(void)
{
long long lines;
IFileDialog *pfd = NULL;
IShellItem *psiResult;
DWORD dwFlags;
PWSTR pszFilePath = NULL;
char chosen_filter[1024];
char chosen_filename[MAX_PATH];
OPENFILENAME parameters;

/*
	Create the Open Dialg object IFileDialog
*/
if ((CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)) == S_OK))
	{
	/*
		we're on VISTA / Windows 7 or later
	*/
	const COMDLG_FILTERSPEC c_rgSaveTypes[] =
	{
	{L"C/C++ files",		L"*.c;*.cxx;*.cpp;*.h;*.hpp.*.hxx;*.mak;makefile"},
	{L"All Documents (*.*)",		L"*.*"}
	};

	pfd->GetOptions(&dwFlags);
	pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);					// we want to see a file
	pfd->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes);
	pfd->SetFileTypeIndex(1);										// first item in the list is the default
	pfd->SetDefaultExtension(L"doc;docx");
	if (pfd->Show(NULL) == S_OK)
		{
		if (pfd->GetResult(&psiResult) == S_OK)						// get the result object if the user clicks "Open"
			{
			psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);

			lines = file->read_file((char *)pszFilePath);

			CoTaskMemFree(pszFilePath);
			psiResult->Release();
			}
		}
	pfd->Release();

	return lines;
	}
else
	{
	/*
		we're on something prior to VISTA (NT through to XP) so use the old-style common control
	*/
	memset(chosen_filename, 0, sizeof(chosen_filename));

	parameters.lStructSize = sizeof(parameters);
	parameters.hwndOwner = window;
	parameters.hInstance = hInstance;
	parameters.lpstrFilter = L"C/C++ files\0*.c;*.cxx;*.cpp;*.h;*.hpp.*.hxx\0\0\0";
	parameters.lpstrCustomFilter = (LPWSTR)chosen_filter;
	parameters.nMaxCustFilter = sizeof(chosen_filter) - 1;
	parameters.nFilterIndex = 1;
	parameters.lpstrFile = (LPWSTR)chosen_filename;
	parameters.nMaxFile = sizeof(chosen_filename) - 1;
	parameters.lpstrFileTitle = NULL;
	parameters.nMaxFileTitle = 0;
	parameters.lpstrInitialDir = NULL;
	parameters.lpstrTitle = L"Open...";
	parameters.Flags = OFN_LONGNAMES;
	parameters.nFileOffset = 0;
	parameters.nFileExtension = 0;
	parameters.lpstrDefExt = NULL;
	parameters.lCustData = 0;
	parameters.lpfnHook = NULL;
	parameters.lpTemplateName = 0;
	#if (_WIN32_WINNT >= 0x0500)
		parameters.pvReserved = NULL;
		parameters.dwReserved = 0;
		parameters.FlagsEx = 0;
	#endif

	if ((GetOpenFileNameW(&parameters)) != 0)
		return file->read_file((char *)parameters.lpstrFile);
	}

return 0;
}
Ejemplo n.º 10
0
	void openBrowseDialogCore(FileDialogType type, const Path& defaultPath, const String& filterList,
		Vector<Path>& paths, AsyncOp& returnValue)
	{
		// Init COM library.
		CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);

		IFileDialog* fileDialog = nullptr;

		UINT32 dialogType = ((UINT32)type & (UINT32)FileDialogType::TypeMask);
		bool isOpenDialog = dialogType == (UINT32)FileDialogType::OpenFile || dialogType == (UINT32)FileDialogType::OpenFolder;

		// Create dialog
		IID classId = isOpenDialog ? CLSID_FileOpenDialog : CLSID_FileSaveDialog;
		CoCreateInstance(classId, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&fileDialog));

		addFiltersToDialog(fileDialog, filterList);
		setDefaultPath(fileDialog, defaultPath);

		// Apply multiselect flags
		bool isMultiselect = false;
		if (isOpenDialog)
		{
			if (dialogType == (UINT32)FileDialogType::OpenFile)
			{
				if (((UINT32)type & (UINT32)FileDialogType::Multiselect) != 0)
				{
					DWORD dwFlags;
					fileDialog->GetOptions(&dwFlags);
					fileDialog->SetOptions(dwFlags | FOS_ALLOWMULTISELECT);

					isMultiselect = true;
				}
			}
			else
			{
				DWORD dwFlags;
				fileDialog->GetOptions(&dwFlags);
				fileDialog->SetOptions(dwFlags | FOS_PICKFOLDERS);
			}
		}

		// Show the dialog
		bool finalResult = false;

		// Need to enable all windows, otherwise when the browse dialog closes the active window will become some 
		// background window
		Win32Window::_enableAllWindows();

		if (SUCCEEDED(fileDialog->Show(nullptr)))
		{
			if (isMultiselect)
			{
				// Get file names
				IFileOpenDialog* fileOpenDialog;
				fileDialog->QueryInterface(IID_IFileOpenDialog, (void**)&fileOpenDialog);
				
				IShellItemArray* shellItems = nullptr;
				fileOpenDialog->GetResults(&shellItems);

				getPaths(shellItems, paths);
				shellItems->Release();
				fileOpenDialog->Release();
			}
			else
			{
				// Get the file name
				IShellItem* shellItem = nullptr;
				fileDialog->GetResult(&shellItem);

				LPWSTR filePath = nullptr;
				shellItem->GetDisplayName(SIGDN_FILESYSPATH, &filePath);

				paths.push_back((Path)UTF8::fromWide(WString(filePath)));
				CoTaskMemFree(filePath);

				shellItem->Release();
			}

			finalResult = true;
		}

		// Restore modal window state (before we enabled all windows)
		Win32Window::_restoreModalWindows();

		CoUninitialize();

		returnValue._completeOperation(finalResult);
	}
Ejemplo n.º 11
0
//-----------------------------------------------------------------------------
bool VistaFileSelector::runModalInternal ()
{
	fileDialog = 0;
	HRESULT hr = -1;
	if (style == kSelectSaveFile)
	{
		hr = CoCreateInstance (CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IFileDialog, &fileDialog));
		if (defaultSaveName)
		{
			fileDialog->SetFileName (UTF8StringHelper (defaultSaveName));
		}
	}
	else
	{
		hr = CoCreateInstance (CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IFileDialog, &fileDialog));
		if (SUCCEEDED (hr))
		{
			if (style == kSelectDirectory)
			{
				DWORD dwOptions = 0;
				hr = fileDialog->GetOptions (&dwOptions);
				if (SUCCEEDED (hr))
					hr = fileDialog->SetOptions (dwOptions | FOS_PICKFOLDERS);
				if (FAILED (hr))
				{
					fileDialog->Release ();
					fileDialog = 0;
					return false;
				}
			}
			if (allowMultiFileSelection)
			{
				DWORD dwOptions = 0;
				hr = fileDialog->GetOptions (&dwOptions);
				if (SUCCEEDED (hr))
					hr = fileDialog->SetOptions (dwOptions | FOS_ALLOWMULTISELECT);
				if (FAILED (hr))
				{
					fileDialog->Release ();
					fileDialog = 0;
					return false;
				}
			}
		}
	}
	if (FAILED (hr))
	{
		fileDialog = 0;
		return false;
	}

	if (title)
		hr = fileDialog->SetTitle (UTF8StringHelper (title));

	DWORD numExtensions = 0;
	DWORD defaultFileTypeIndex = 0;
	COMDLG_FILTERSPEC* filters = buildExtensionFilter (extensions, defaultExtension, numExtensions, defaultFileTypeIndex);
	if (filters)
	{
		fileDialog->SetFileTypes (numExtensions, filters);
		if (defaultFileTypeIndex)
			fileDialog->SetFileTypeIndex (defaultFileTypeIndex);
	}
	if (initialPath && _SHCreateItemFromParsingName)
	{
		IShellItem* shellItem;
		hr = _SHCreateItemFromParsingName (UTF8StringHelper (initialPath), 0, IID_PPV_ARG (IShellItem, &shellItem));
		if (SUCCEEDED (hr))
		{
			fileDialog->SetFolder (shellItem);
			shellItem->Release ();
		}
	}
	Win32Frame* win32Frame = frame->getPlatformFrame () ? dynamic_cast<Win32Frame*> (frame->getPlatformFrame ()) : 0;
	hr = fileDialog->Show (win32Frame ? win32Frame->getPlatformWindow () : 0);
	if (SUCCEEDED (hr))
	{
		if (allowMultiFileSelection)
		{
			IFileOpenDialog* openFileDialog = 0;
			hr = fileDialog->QueryInterface (IID_PPV_ARG(IFileOpenDialog, &openFileDialog));
			if (SUCCEEDED (hr))
			{
				IShellItemArray* items;
				hr = openFileDialog->GetResults (&items);
				if (SUCCEEDED (hr))
				{
					DWORD count;
					hr = items->GetCount (&count);
					for (DWORD i = 0; i < count; i++)
					{
						IShellItem* item;
						hr = items->GetItemAt (i, &item);
						if (SUCCEEDED (hr))
						{
							LPWSTR filesysPath = 0;
							hr = item->GetDisplayName (SIGDN_FILESYSPATH, &filesysPath);
							if (SUCCEEDED (hr))
							{
								UTF8StringHelper str (filesysPath);
								UTF8StringBuffer resultPath = String::newWithString (str);
								result.push_back (resultPath);
							}
							item->Release ();
						}
					}
					items->Release ();
				}
				openFileDialog->Release ();
			}
		}
		else
		{
			IShellItem *item;
			hr = fileDialog->GetResult (&item);
			if (SUCCEEDED (hr))
			{
				LPWSTR filesysPath = 0;
				hr = item->GetDisplayName (SIGDN_FILESYSPATH, &filesysPath);
				if (SUCCEEDED (hr))
				{
					UTF8StringHelper str (filesysPath);
					UTF8StringBuffer resultPath = String::newWithString (str);
					result.push_back (resultPath);
				}
				item->Release ();
			}
		}
	}
	fileDialog->Release ();
	fileDialog = 0;
	freeExtensionFilter (filters);
	return SUCCEEDED (hr);
}
Ejemplo n.º 12
0
HRESULT BasicFileOpen(PWSTR* filePath)
{
	// CoCreate the File Open Dialog object.
	IFileDialog *pfd = NULL;
	HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, 
		NULL, 
		CLSCTX_INPROC_SERVER, 
		IID_PPV_ARGS(&pfd));
	if (SUCCEEDED(hr))
	{
		// Create an event handling object, and hook it up to the dialog.
		IFileDialogEvents *pfde = NULL;
		//DWORD dwCookie;
		// Set the options on the dialog.
		DWORD dwFlags;

		// Before setting, always get the options first in order 
		// not to override existing options.
		hr = pfd->GetOptions(&dwFlags);
		if (SUCCEEDED(hr))
		{
			// In this case, get shell items only for file system items.
			hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
			if (SUCCEEDED(hr))
			{
				static const COMDLG_FILTERSPEC c_rgSaveTypes[] =
				{
					{L"Executable Files(*.exe)",       L"*.exe"},
				};
				// Set the file types to display only. 
				// Notice that this is a 1-based array.
				hr = pfd->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes);
				if (SUCCEEDED(hr))
				{
					// Set the selected file type index to Word Docs for this example.
					hr = pfd->SetFileTypeIndex(1);
					if (SUCCEEDED(hr))
					{
						// Set the default extension to be ".doc" file.
						hr = pfd->SetDefaultExtension(L"exe");
						if (SUCCEEDED(hr))
						{
							// Show the dialog
							hr = pfd->Show(g_hwnd);
							if (SUCCEEDED(hr))
							{
								// Obtain the result once the user clicks 
								// the 'Open' button.
								// The result is an IShellItem object.
								IShellItem *psiResult;
								hr = pfd->GetResult(&psiResult);
								if (SUCCEEDED(hr))
								{
									// We are just going to print out the 
									// name of the file for sample sake.
									PWSTR pszFilePath = NULL;
									hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, 
										&pszFilePath);
									if (SUCCEEDED(hr))
									{
										*filePath=pszFilePath;
										
									}
									psiResult->Release();
								}
							}
						}
					}
				}
			}
		}
		pfd->Release();
	}
	else
	{
		hr=E_OUTOFMEMORY;
		OPENFILENAME ofn;
		LPWSTR pszFile=(LPWSTR)CoTaskMemAlloc(260*2);
		if(pszFile!=NULL)
		{
			memset1(&ofn,0,sizeof(ofn));
			ofn.lStructSize = sizeof(ofn);
			ofn.hwndOwner = g_hwnd;
			ofn.lpstrFile = pszFile;
			// Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
			// use the contents of szFile to initialize itself.
			ofn.lpstrFile[0] = L'\0';
			ofn.nMaxFile = 260;
			ofn.lpstrFilter = L"Executable Files(*.exe)\0*.exe\0";
			ofn.nFilterIndex = 1;
			ofn.lpstrFileTitle = NULL;
			ofn.nMaxFileTitle = 0;
			ofn.lpstrInitialDir = NULL;
			ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

			if (GetOpenFileName(&ofn)==TRUE)
			{
				*filePath=pszFile;
				hr=0;
			}
		}

	}
	return hr;
}
Ejemplo n.º 13
0
String ShowOpenDirectoryDialog()
{
	String ret;
	bool pathSelected = false;

	// check current OS version
	OSVERSIONINFO osvi;
	memset(&osvi, 0, sizeof(OSVERSIONINFO));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	
	if (GetVersionEx(&osvi) && (osvi.dwMajorVersion >= 6))
	{
		// for Vista or later, use the MSDN-preferred implementation of the Open File dialog in pick folders mode
		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);
							ret = lpwszName;
							pathSelected = true;

							::CoTaskMemFree(lpwszName);
						}

						psi->Release();
					}
				}
			}

			pfd->Release();
		}
	}
	else
	{
		// for XP, use the old-styled SHBrowseForFolder() implementation
		BROWSEINFO bi = {0};
		bi.hwndOwner = GetActiveWindow();
		bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX;

		LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
		if (pidl != 0)
		{
			TCHAR szFile[MAX_PATH];
			szFile[0] = 0;

			if (SHGetPathFromIDList(pidl, szFile))
			{
				// Add directory path to the result
				//ConvertToUnixPath(pathName);
				ret = szFile;
				pathSelected = true;
			}

			IMalloc* pMalloc = NULL;
			SHGetMalloc(&pMalloc);
			if (pMalloc)
			{
				pMalloc->Free(pidl);
				pMalloc->Release();
			}
		}
	}

	return ret;
}