Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase number of characters in filename field of GetOpenFileName file selection dialog

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.

like image 586
Kevin Day Avatar asked Dec 11 '08 23:12

Kevin Day


2 Answers

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.)

like image 20
Frank Krueger Avatar answered Sep 20 '22 09:09

Frank Krueger


Turns out that the edit control (At least in my development environment) is a combo box, so EM_SETLIMITTEXT isn't appropriate.

Instead, I tracked 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.

like image 107
Kevin Day Avatar answered Sep 23 '22 09:09

Kevin Day