Using CFileDialog
class, I select multiple files placed in a directory with a long path. It's OK when I select only one or two files; but when I select three files at the same time it returns only a part of the third file path. (Looks like it's limited to 512 characters possibly) How can I resolve this?

- 239,200
- 50
- 490
- 574

- 2,755
- 2
- 33
- 60
-
Does it have to do with Window's `MAX_PATH`? – bames53 Feb 15 '13 at 21:30
-
It has to do with MFC using a buffer of size `_MAX_PATH` by default. It can be overriden. – Nik Bougalis Feb 15 '13 at 21:32
2 Answers
MFC uses a default buffer of size _MAX_PATH
and that's why you are seeing that behavior. Look at dlgfile.cpp
for the implementation of CFileDialog::CFileDialog
and you will see m_ofn.lpstrFile
and m_ofn.nMaxFile
being set.
You can specify a larger buffer if you want to. Before calling DoModal
you can either access the CFileDialog::m_pOFN
member to get a pointer to the OPENFILENAME
that the CFileDialog
will use and update it directly or call CFileDialog::GetOFN
to get a reference to the structure and update that.
Either way you will find this helpful: http://msdn.microsoft.com/en-US/library/ms646839(v=vs.80).aspx

- 10,495
- 1
- 21
- 37
-
1If you're going to go through the trouble of working around it, you might also want to consider using the [Common Item Dialogs](http://msdn.microsoft.com/en-US/library/bb776913.aspx) instead. The `GetOpenFileName` API was deprecated with the release of Vista, but is still supported for legacy applications. – Cody Gray - on strike Feb 15 '13 at 23:35
Assuming that your code looks something like this:
CFileDialog dialog(...);
dialog.DoModal();
Determine the maximum number of files that you wish to support, for example:
#define MAX_FILE_NAMES 256
Add this before calling DoModal
:
CString data;
dialog.m_pOFN->nMaxFile = (MAX_FILE_NAMES*(MAX_PATH+1))+1;
dialog.m_pOFN->lpstrFile = data.GetBuffer((MAX_FILE_NAMES*(MAX_PATH+1))+1);
Add this after calling DoModal
:
data.ReleaseBuffer();

- 29,648
- 10
- 62
- 114