Good day. I made a class for OpenFileDialog to work:

#include <Windows.h> enum DialogResult { OK, Cancel }; class IFileDialog { public: TCHAR FileName[MAX_PATH]; LPTSTR Title; virtual DialogResult Show() = 0; }; class COpenFileDialog : public IFileDialog { public: LPTSTR Filter; LPTSTR DefExt; COpenFileDialog(HWND hWnd); DialogResult Show(); private: OPENFILENAME m_ofn; }; COpenFileDialog::COpenFileDialog(HWND hWnd) { //FileName[0] = '\0'; ZeroMemory(&m_ofn, sizeof(m_ofn)); m_ofn.lStructSize = sizeof(m_ofn); m_ofn.hwndOwner = hWnd; m_ofn.lpstrFilter = Filter; m_ofn.nMaxFile = MAX_PATH; m_ofn.lpstrFile = FileName; m_ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; m_ofn.lpstrDefExt = DefExt; } DialogResult COpenFileDialog::Show() { return GetOpenFileName(&m_ofn) ? DialogResult::OK : DialogResult::Cancel; } 

Then I try to call:

 COpenFileDialog hDialog = COpenFileDialog(hMainWindow); hDialog.Filter = "M&B Scenes\0*.sco\0"; hDialog.DefExt = "sco"; hDialog.Title = ""; if(hDialog.Show() == DialogResult::OK) { //TODO } 

But the program crashes on hDialog.Show() , or rather when you call GetOpenFileName(&m_ofn) :

Unhandled exception at 0x000007FDFC5D21A3 (SHCore.dll) in ScoEditor.exe: 0xC0000005: Access violation reading location 0x000000E600000000.

How can this be fixed?

    2 answers 2

    Maybe it's in the pointers?

     LPTSTR Title; LPTSTR Filter; LPTSTR DefExt; 

    After all, in fact, you did not initialize them, but simply try to assign a string. Logically, you can not do that. It is necessary to allocate memory for these pointers (well, and then remove it, respectively).

     LPTSTR pBuffer; // TCHAR* pBuffer = new TCHAR[MAX_PATH]; ... delete[] pBuffer; 

    Try hard to specify these parameters when initializing the structure:

     m_ofn.lpstrFilter = Filter; m_ofn.lpstrDefExt = DefExt; m_ofn.lpstrFileTitle = Title; 

    MSDN link - click

      Somewhere during the initialization of the m_ofn structure m_ofn is an error, you are not transmitting what you need, or something is not declared.