By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Customizing File Open dialog box

P: 79
Hello,

I need to customize a standard File Open dialog box so that when DoModal is called, the dialog box opens to a certain directory and already has preselected a file.


Expand|Select|Wrap|Line Numbers
  1. FileDlg.GetOFN().lpstrInitialDir = _T("<some directory>");
  2.  
  3.  
  4. FileDlg.GetOFN().lpstrFile = _T("masterDefault.txt");
The first line works when by itself. As soon as I add the second line, the program crashes when FileDlg.DoModal() is called. I don't get any compilation errors.

What am I doing wrong?
Aug 15 '07 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. FileDlg.GetOFN().lpstrFile = _T("masterDefault.txt");
OPENFILENAME::lpstrFile is defined as
Pointer to a buffer that contains a filename used to initialize the File Name edit control. The first character of this buffer must be NULL if initialization is not necessary. When the GetOpenFileName or GetSaveFileName function returns successfully, this buffer contains the drive designator, path, filename, and extension of the selected file.

If the OFN_ALLOWMULTISELECT flag is set and the user selects multiple files, the buffer contains the current directory followed by the filenames of the selected files. For Explorer-style dialog boxes, the directory and filename strings are NULL separated, with an extra NULL character after the last filename. For old-style dialog boxes, the strings are space separated and the function uses short filenames for filenames with spaces. You can use the FindFirstFile function to convert between long and short filenames.

If the buffer is too small, the function returns FALSE and the CommDlgExtendedError function returns FNERR_BUFFERTOOSMALL. In this case, the first two bytes of the lpstrFile buffer contain the required size, in bytes or characters.
The implication of this is that lpstrFile must point to a buffer that the FileDlg can write to (and it's size must be in the OPENFILENAME::nMaxFile to prevent buffer overruns). You have not supplied a pointer to a buffer but a pointer to a literal.

I suggest you read up on the OPENFILEDIALOG structure.
Aug 16 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.