CFileDialog crashes in Platform SDK
-
I have installed Platform SDK Windows Server 2003 (I work in VS 6.0 on WinXP) and I have a problem with CFileDialog. This code
void CFileTestView::OnViewFile() { CFileDialog dlg(TRUE); dlg.DoModal(); }
worked just fine before defining _WIN32_WINNT, which I need to use different functions and constants. Now, when I define it#ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0501 // can be 0x0500 as well #endif
my code just crashes in ~CFileDialog (when dlg does out of scope and is destroyed). Since m_ofn member of CFileDialog depends on this _WIN32_WINT:typedef struct tagOFN { // ... #if (_WIN32_WINNT >= 0x0500) void * pvReserved; DWORD dwReserved; DWORD FlagsEx; #endif // (_WIN32_WINNT >= 0x0500) } OPENFILENAME
I rewrote the code this way:void CFileTestView::OnViewFile() { CFileDialog dlg(TRUE); dlg.m_ofn.lStructSize = sizeof(OPENFILENAME); // 88 bytes #if (_WIN32_WINNT >= 0x0500) dlg.m_ofn.pvReserved = NULL; dlg.m_ofn.dwReserved = 0; dlg.m_ofn.FlagsEx = 0; #endif dlg.DoModal(); // can also miss }
but I still get a first-chance exception in FileTest.Exe (KERNEL32.dll) 0xC0000005: Access Violation (the usual stuff). Now, I found out that if I declare dlg dinamically it doesn't crash:void CFileTestView::OnViewFile() { CFileDialog* dlg = new CFileDialog*(TRUE); dlg.m_ofn.lStructSize = sizeof(OPENFILENAME); // 88 bytes #if (_WIN32_WINNT >= 0x0500) dlg.m_ofn.pvReserved = NULL; dlg.m_ofn.dwReserved = 0; dlg.m_ofn.FlagsEx = 0; #endif delete dlg; // OK }
But If I derive CFileDialog to CMyFileDialog the program still crashes at delete:void CFileTestView::OnViewFile() { CMyFileDialog* dlg = new CMyFileDialog*(TRUE); dlg.m_ofn.lStructSize = sizeof(OPENFILENAME); // 88 bytes #if (_WIN32_WINNT >= 0x0500) dlg.m_ofn.pvReserved = NULL; dlg.m_ofn.dwReserved = 0; dlg.m_ofn.FlagsEx = 0; #endif delete dlg; // crash boom boom }
Any ideas? Thanks! -
I have installed Platform SDK Windows Server 2003 (I work in VS 6.0 on WinXP) and I have a problem with CFileDialog. This code
void CFileTestView::OnViewFile() { CFileDialog dlg(TRUE); dlg.DoModal(); }
worked just fine before defining _WIN32_WINNT, which I need to use different functions and constants. Now, when I define it#ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0501 // can be 0x0500 as well #endif
my code just crashes in ~CFileDialog (when dlg does out of scope and is destroyed). Since m_ofn member of CFileDialog depends on this _WIN32_WINT:typedef struct tagOFN { // ... #if (_WIN32_WINNT >= 0x0500) void * pvReserved; DWORD dwReserved; DWORD FlagsEx; #endif // (_WIN32_WINNT >= 0x0500) } OPENFILENAME
I rewrote the code this way:void CFileTestView::OnViewFile() { CFileDialog dlg(TRUE); dlg.m_ofn.lStructSize = sizeof(OPENFILENAME); // 88 bytes #if (_WIN32_WINNT >= 0x0500) dlg.m_ofn.pvReserved = NULL; dlg.m_ofn.dwReserved = 0; dlg.m_ofn.FlagsEx = 0; #endif dlg.DoModal(); // can also miss }
but I still get a first-chance exception in FileTest.Exe (KERNEL32.dll) 0xC0000005: Access Violation (the usual stuff). Now, I found out that if I declare dlg dinamically it doesn't crash:void CFileTestView::OnViewFile() { CFileDialog* dlg = new CFileDialog*(TRUE); dlg.m_ofn.lStructSize = sizeof(OPENFILENAME); // 88 bytes #if (_WIN32_WINNT >= 0x0500) dlg.m_ofn.pvReserved = NULL; dlg.m_ofn.dwReserved = 0; dlg.m_ofn.FlagsEx = 0; #endif delete dlg; // OK }
But If I derive CFileDialog to CMyFileDialog the program still crashes at delete:void CFileTestView::OnViewFile() { CMyFileDialog* dlg = new CMyFileDialog*(TRUE); dlg.m_ofn.lStructSize = sizeof(OPENFILENAME); // 88 bytes #if (_WIN32_WINNT >= 0x0500) dlg.m_ofn.pvReserved = NULL; dlg.m_ofn.dwReserved = 0; dlg.m_ofn.FlagsEx = 0; #endif delete dlg; // crash boom boom }
Any ideas? Thanks!Does the problem persist if you use
GetOpenFileName()
instead?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
I have installed Platform SDK Windows Server 2003 (I work in VS 6.0 on WinXP) and I have a problem with CFileDialog. This code
void CFileTestView::OnViewFile() { CFileDialog dlg(TRUE); dlg.DoModal(); }
worked just fine before defining _WIN32_WINNT, which I need to use different functions and constants. Now, when I define it#ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0501 // can be 0x0500 as well #endif
my code just crashes in ~CFileDialog (when dlg does out of scope and is destroyed). Since m_ofn member of CFileDialog depends on this _WIN32_WINT:typedef struct tagOFN { // ... #if (_WIN32_WINNT >= 0x0500) void * pvReserved; DWORD dwReserved; DWORD FlagsEx; #endif // (_WIN32_WINNT >= 0x0500) } OPENFILENAME
I rewrote the code this way:void CFileTestView::OnViewFile() { CFileDialog dlg(TRUE); dlg.m_ofn.lStructSize = sizeof(OPENFILENAME); // 88 bytes #if (_WIN32_WINNT >= 0x0500) dlg.m_ofn.pvReserved = NULL; dlg.m_ofn.dwReserved = 0; dlg.m_ofn.FlagsEx = 0; #endif dlg.DoModal(); // can also miss }
but I still get a first-chance exception in FileTest.Exe (KERNEL32.dll) 0xC0000005: Access Violation (the usual stuff). Now, I found out that if I declare dlg dinamically it doesn't crash:void CFileTestView::OnViewFile() { CFileDialog* dlg = new CFileDialog*(TRUE); dlg.m_ofn.lStructSize = sizeof(OPENFILENAME); // 88 bytes #if (_WIN32_WINNT >= 0x0500) dlg.m_ofn.pvReserved = NULL; dlg.m_ofn.dwReserved = 0; dlg.m_ofn.FlagsEx = 0; #endif delete dlg; // OK }
But If I derive CFileDialog to CMyFileDialog the program still crashes at delete:void CFileTestView::OnViewFile() { CMyFileDialog* dlg = new CMyFileDialog*(TRUE); dlg.m_ofn.lStructSize = sizeof(OPENFILENAME); // 88 bytes #if (_WIN32_WINNT >= 0x0500) dlg.m_ofn.pvReserved = NULL; dlg.m_ofn.dwReserved = 0; dlg.m_ofn.FlagsEx = 0; #endif delete dlg; // crash boom boom }
Any ideas? Thanks!Here's an idea... Look at the beginning of the class definition for CFileDialog... class CFileDialog : public CCommonDialog { DECLARE_DYNAMIC(CFileDialog) public: // Attributes OPENFILENAME m_ofn; // open file parameter block Whoa! There's an OPENFILENAME m_ofn member variable, and YOU DO NOT CONTROL IT'S SIZE! You would have to rebuild your MFC DLL or rebuild the static MFC library to which you are linking, using the NEW size. Anyway, that is why you get the crash - you write to data that overwrites something else! If you really need these new fields, you are going to have to 'swipe' the CFileDialog source code and make your own using the new data structure. Otherwise, just use OpenFileName as David Crow suggests.
-
Here's an idea... Look at the beginning of the class definition for CFileDialog... class CFileDialog : public CCommonDialog { DECLARE_DYNAMIC(CFileDialog) public: // Attributes OPENFILENAME m_ofn; // open file parameter block Whoa! There's an OPENFILENAME m_ofn member variable, and YOU DO NOT CONTROL IT'S SIZE! You would have to rebuild your MFC DLL or rebuild the static MFC library to which you are linking, using the NEW size. Anyway, that is why you get the crash - you write to data that overwrites something else! If you really need these new fields, you are going to have to 'swipe' the CFileDialog source code and make your own using the new data structure. Otherwise, just use OpenFileName as David Crow suggests.
First, my project is too large and I don't want to go and replace everything with OpenFileName(). Second, when I have to define _WIN32_WINNT to 0x0501 because I need it to use other functions that are define like
#if (_WIN32_WINNT >= 0x0500) //here is the function #endif
so OPENFILENAME will have the extra three data members. If Platform SDK provides me this (actually) new structure doesn't it also provide the necessary lib? Because of my settings it should try to link first to SDK libs and only after (if not found) to VS6.0 old libs.