Why does CFileDialog :: GetNextPathName not work when the file path is long?

Using the CFileDialog class, I select several files placed in a directory with a long path. This is normal when I select only one or two files; but when I select three files at a time, it returns only part of the third path to the file. (It looks like it's limited to 512 characters) How can I solve this?

+6
source share
2 answers

MFC uses a default buffer of size _MAX_PATH and therefore you see this behavior. Take a look at dlgfile.cpp to implement CFileDialog::CFileDialog , and you will see the settings for m_ofn.lpstrFile and m_ofn.nMaxFile .

You can specify a larger buffer if you want. Before calling DoModal you can either access the CFileDialog::m_pOFN element to get a pointer to OPENFILENAME , which CFileDialog will use, and update it directly or call CFileDialog::GetOFN to get a link to the structure and update it.

In any case, you will find this useful: http://msdn.microsoft.com/en-US/library/ms646839(v=vs.80).aspx

+5
source

Assuming your code looks something like this:

 CFileDialog dialog(...); dialog.DoModal(); 

Determine the maximum number of files that you want 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(); 
0
source

All Articles