0

C++/MFC, Windows 11. Visual Studio 2022 17.4.3.

I'm using CFileDialog to allow user to chose a file. By creating a new class derived from CFileDialog, I am being notified whenever the user changes directories (folders).

I implemented this so I could control the filter applied to the list of files in the directory. However, I have been unsuccessful in this. Even if I don't change m_ofn, I get an error.

Here is some sample code:

// Caller
#include "Browsing_test.h"
P brTest(true, NULL, NULL, 0, fileTypes);
brTest.BrowseTest();
// Browsing_test.h
class P : CFileDialog
{
public:
    P(BOOL bOpenFileDialog,
        LPCTSTR lpszDefExt = NULL,
        LPCTSTR lpszFileName = NULL,
        DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        LPCTSTR lpszFilter = NULL,
        CWnd *pParentWnd = NULL,
        DWORD dwSize = 0,
        BOOL bVistaStyle = TRUE) : CFileDialog(bOpenFileDialog,
            lpszDefExt,
            lpszFileName,
            dwFlags,
            lpszFilter,
            pParentWnd,
            dwSize,
            bVistaStyle) {};

    int BrowseTest(void);
#include "stdafx.h"
#include "Browsing_test.h"

int P::BrowseTest(void)
{
    int resultDebug = (int)DoModal();
    return resultDebug;
}

void P::OnFolderChange()
{
    auto s = GetOFN();       // for modifying m_ofn member of the base class,
                             // but not used in this sample code
    // Add modificatons to m_ofn here
    ApplyOFNToShellDialog(); // Gets assert on updating flags
}

Running this code gives an error in dlgfile.cpp (Microsoft code) at line:

hr = (static_cast<IFileDialog*>(m_pIFileDialog))->SetOptions(dwFlags);

which returns hr = E_UNEXPECTED Catastrophic failure. The value of dwFlags was hex 40.

Cosmin
  • 21,216
  • 5
  • 45
  • 60
Woody20
  • 791
  • 11
  • 30
  • Lousy error code, not unusual in COM. It is trying to tell you that you can't modify the options while the dialog is active. Technically you could close the dialog and display it again, but that won't be pretty. – Hans Passant Jan 11 '23 at 11:40
  • @HansPassant: This sounds like the explanation, and if so, there is no simple way to do what I am looking for. – Woody20 Jan 11 '23 at 19:42

0 Answers0