views:

714

answers:

3

Our app allows multiple files to be selected in a file selection dialog which is shown via the GetOpenFileName function (this question also applies to folks using CFileDialog, etc...)

There appears to be a limit to the number of characters that can be typed into the filename field (259 seems to be the magic number - not sure why).

We have tried changing the following members of the OPENFILENAME structure:

lpstrFile - point to our own buffer, sized at 4K bytes nMaxFile - set to the size of lpstrFile (we are compiling ANSI, so this is effectively 4000

But these values appear to not increase the input width of the filename field in the dialog.

I am going to experiment with sending a EM_SETLIMITTEXT message to the control, but wanted to know if anyone else has a solution.

EDIT - solved this myself: solution I can't accept out my own answer, but here it is for posterity. If anyone else has a better solution, please post it - or feel free to mod up my solution so future searchers will find it at the top.

+1  A: 

From Naming a File or Directory on MSDN:

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters.

Even if you could coerce longer strings out of the dialog, you may run into trouble down the line when using APIs that have been coded against MAX_PATH.

The docs go on to say:

The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function. To specify an extended-length path, use the "\\?\" prefix. For example, "\\?\D:\<very long path>". (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

Frank Krueger
Note that I am retrieving multiple paths from the dialog - each path could be as long as MAX_PATH, but the dialog could return 10 or 15 of them.
Kevin Day
A: 

I believe this is a hard limit that cannot be bypassed. The only time it should matter is when you want to select more than one file, since the limit is enough for the maximum file name length.

I have added an "All Files" button to these dialogs for opening all of the files in a folder; that's the only workaround I have found.

Mark Ransom
ahhh - never say 'never' in the world of win32 :-)
Kevin Day
Ordinarily I wouldn't say "never", but I spent a LOT of time on this.
Mark Ransom
Hopefully my solution above will work for you then!
Kevin Day
Very cool, thanks! The job where I wrestled with this is long gone, but I'll keep it in mind for next time.
Mark Ransom
+3  A: 

not sure how I should close out a question that I answered for myself... I'm going to try posting this with the answer:

Turns out that the edit control (At least in my development environment) is a combo box, so EM_SETLIMITTEXT isn't appropriate. Instead, I track down the combo box using GetDlgCtrl on the parent of the file open dialog (I do this in the OnInitDialog handler), cast it to CComboBox*, then call LimitText() to set the limit. This could also be done by sending a CB_LIMITTEXT message to the control for those of you who are not working with CFileDialog. The appropriate value here is most likely the OPENFIILENAME.nMaxFile value that is passed in.

Kevin Day