1

I inherited some code that is clearly based on this discussion:

cfiledialog and file extension

The code reads:

#define FILE_DIALOG_NAME cmb13

// CStarMaskDlg dialog
/* ------------------------------------------------------------------- */

class CSaveMaskDlg : public CFileDialog
{
    DECLARE_DYNAMIC(CSaveMaskDlg)

    // 
    // 9 December 2022 David C. Partridge
    // 
    // I added OFN_EXPLORER to the open flags as per the note in the docs for CFileDialog::SetControlText
    // that say:
    //    To use this method, you must create the dialog box with the OFN_EXPLORER style.
    //    Otherwise, the function will fail with an assertion.
    //
public :
    CSaveMaskDlg(bool bOpenFileDialog, // true for FileOpen, false for FileSaveAs
        LPCTSTR lpszDefExt = nullptr,
        LPCTSTR lpszFileName = nullptr,
        DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER, // Add OFN_EXPLORER - see CFileDialog
        LPCTSTR lpszFilter = nullptr,
        CWnd* pParentWnd = nullptr):CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
    {
    };
    virtual ~CSaveMaskDlg()
    {
    };

protected:
    virtual void OnTypeChange()
    {
        CFileDialog::OnTypeChange();
        CString         strFileName = GetFileTitle();

        if (strFileName.GetLength())
        {
            if (m_ofn.nFilterIndex == 1)
                strFileName += ".tif";
            else
                strFileName += ".fits";
            SetControlText(FILE_DIALOG_NAME, strFileName);
        };
    };
};
IMPLEMENT_DYNAMIC(CSaveMaskDlg, CFileDialog)

/* ------------------------------------------------------------------- */

IMPLEMENT_DYNAMIC(CStarMaskDlg, CDialog)

The problem is that whenever SetControlText is invoked (at least on W10 and W11), an InvalidArgument exception is thrown.

As you can see I tried to fix the problem but so far it is beating me.

If this code is wrong, then please put me out of my misery and show me how it should go.

Thanks a lot David

  • Invalid argument, means one of the parameters is incorrect. Given CFileDialogSetControlText only takes two arguments, and we can clearly see you are passing a string to the LPCSTR arg, that would seem to leave the first argument (nID) as the culprit. What is cmb13? Does it map to a control ID value for a control on your file dialog? – Ed Dore Dec 12 '22 at 14:41
  • Yes I knew that - it's discussed in the link I provided. It seems that things have changed sufficiently since then that this won't work. I deleted the code in question. – David Partridge Dec 13 '22 at 17:44
  • I programmed a lot of MFC over the last decade, and I had never seen that `SetControlText` function ever. I don't know how to answer to why it fails, but if you want an alternative approach: `GetDlgItem(FILE_DIALOG_NAME)->SetWindowText(strFileName);` – sergiol Mar 19 '23 at 20:00

0 Answers0