Hi,
I am having issue after migrated from VC++ 6 to VC2010. Application is compiled but when I click open to open File using overriden CDialog class dialog hangs after loading halfway. I am not good C++ developer so having difficulty to find last issue.
Immediate window returns only this error when hangs:
"First-chance exception at 0x006c69cc in AliEditor.exe: 0xC0000005: Access violation reading location 0x00000020."
Main Form code calling Open Dialog:
void CMainFrame::OnFileOpen()
{
static TCHAR BASED_CODE szFilter[] = _T("Open Bin File(*.abs)|")
_T("Default DataBase File(*.ddf)|")
_T("SatCodex File(*.sdx)|")
_T("Format Text File(*.txt)");
CFileDlg fd(TRUE, "*.abs", "", OFN_HIDEREADONLY, szFilter, GetForegroundWindow() );
const int c_cMaxFiles = 1;
const int c_cbBuffSize = (c_cMaxFiles * (MAX_PATH + 1)) + 1;
fd.GetOFN().lpstrFile = m_strFilePathName.GetBuffer(c_cbBuffSize);
fd.GetOFN().nMaxFile = c_cbBuffSize;
if(fd.DoModal() != IDOK) return;
......
}
File FileDlg.h header:
#if !defined(AFX_FILEDLG_H__8C927F99_3840_4A93_9E17_E153C675F9D4__INCLUDED_)
#define AFX_FILEDLG_H__8C927F99_3840_4A93_9E17_E153C675F9D4__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "FileExportDialog.h"
class CFileDlg : public CFileExportDialog
{
DECLARE_DYNAMIC(CFileDlg)
public:
CFileDlg(BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL);
protected:
DECLARE_MESSAGE_MAP()
};
#endif // !defined(AFX_FILEDLG_H__8C927F99_3840_4A93_9E17_E153C675F9D4__INCLUDED_)
File FileDlg.cpp code (initialising dialog):
#include "stdafx.h"
#include "AliEditor.h"
#include "FileDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNAMIC(CFileDlg, CFileDialog)
CFileDlg::CFileDlg(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
CFileExportDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
{
}
BEGIN_MESSAGE_MAP(CFileDlg, CFileExportDialog)
ON_WM_CLOSE()
END_MESSAGE_MAP()
Other initialization part in FileExportDialog.cpp file called by code above:
#include "stdafx.h"
#include <dlgs.h>
#include "FileExportDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int Split(char *str,char *word[],int max,char c);
char *newdup(const char *str);
IMPLEMENT_DYNAMIC(CFileExportDialog, FILEEXPORTDIALOG_BASECLASS)
CFileExportDialog::CFileExportDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt,
LPCTSTR lpszFileName,
DWORD dwFlags,
LPCTSTR lpszFilter,
CWnd* pParentWnd) :
base(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd),
m_pTitleBuffer(NULL),
m_pFilterBuffer(NULL),
m_pFileBuffer(NULL),
m_CurrentFilterIndex(1)
{
CString filter(lpszFilter);
CString fileName(lpszFileName);
char *tempBuffer = newdup(filter);
char *word[40]; int n = Split(tempBuffer,word,20,'|');
char **fptr = new char *[n]; m_pFilterBuffer = new char[filter.GetLength()+n*8+1];
m_pFilterBuffer[0]='\0';
for (int i = 0; i < n; ++i) {
strcat(m_pFilterBuffer,word[i]);
strcat(m_pFilterBuffer,"|");
char *part1[2];
int m = Split(word[i],part1,2,'(');
ASSERT(m == 2);
char *part2[2];
m = Split(part1[1],part2,2,')');
ASSERT((m == 1 || m == 2) && !strncmp(part2[0],"*.",2));
strcat(m_pFilterBuffer,part2[0]);
strcat(m_pFilterBuffer,"|");
fptr[i] = part2[0]+1;
}
int l = strlen(m_pFilterBuffer);
for (int i = 0; i < l; ++i)
if (m_pFilterBuffer[i] == '|')
m_pFilterBuffer[i] = '\0';
m_ofn.lpstrFilter = m_pFilterBuffer;
m_pFileBuffer = new char[256];
m_ofn.lpstrFile = m_pFileBuffer;
m_ofn.nMaxFile = 256;
if (fileName.IsEmpty()) {
sprintf(m_pFileBuffer, "*%s",fptr[0]);
} else {
strcpy(m_pFileBuffer, fileName);
}
m_ofn.nFilterIndex = m_CurrentFilterIndex = 1;
if (fileName.IsEmpty()) {
} else {
const char *ext = strchr(LPCSTR(fileName),'.');
if (ext) {
for (int i = 0; i < n; ++i)
if (!strcmp(ext, fptr[i])) {
m_ofn.nFilterIndex = m_CurrentFilterIndex = i + 1;
break;
}
}
}
delete tempBuffer;
delete [] fptr;
}
....
Header file of FileExportDialog.h:
#if !defined(AFX_NCFileExportDialog_H__BAABDC84_DDDA_11D2_A614_0060085FE616__INCLUDED_)
#define AFX_NCFileExportDialog_H__BAABDC84_DDDA_11D2_A614_0060085FE616__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define FILEEXPORTDIALOG_BASECLASS CFileDialog
class CFileExportDialog : public FILEEXPORTDIALOG_BASECLASS
{
typedef FILEEXPORTDIALOG_BASECLASS base;
DECLARE_DYNAMIC(CFileExportDialog)
public:
CFileExportDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL);
virtual ~CFileExportDialog();
int GetFilterIndex() const { return m_CurrentFilterIndex; }
protected:
virtual void OnTypeChange();
DECLARE_MESSAGE_MAP()
private:
char *m_pTitleBuffer; char *m_pFilterBuffer; char *m_pFileBuffer; int m_CurrentFilterIndex; };
#endif // !defined(AFX_NCFileExportDialog_H__BAABDC84_DDDA_11D2_A614_0060085FE616__INCLUDED_)
Then goes into DoModal() of "dlgfile.cpp" file and hangs here:
INT_PTR CFileDialog::DoModal()
{
ASSERT_VALID(this);
ASSERT(m_ofn.Flags & OFN_ENABLEHOOK);
ASSERT(m_ofn.lpfnHook != NULL);
ASSERT(AfxIsValidAddress(m_ofn.lpstrFile, m_ofn.nMaxFile));
DWORD nOffset = lstrlen(m_ofn.lpstrFile)+1;
ASSERT(nOffset <= m_ofn.nMaxFile);
memset(m_ofn.lpstrFile+nOffset, 0, (m_ofn.nMaxFile-nOffset)*sizeof(TCHAR));
HWND hWndFocus = ::GetFocus();
BOOL bEnableParent = FALSE;
m_ofn.hwndOwner = PreModal();
AfxUnhookWindowCreate();
if (m_ofn.hwndOwner != NULL && ::IsWindowEnabled(m_ofn.hwndOwner))
{
bEnableParent = TRUE;
::EnableWindow(m_ofn.hwndOwner, FALSE);
}
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
ASSERT(pThreadState->m_pAlternateWndInit == NULL);
if (m_bVistaStyle == TRUE)
{
AfxHookWindowCreate(this);
}
else if (m_ofn.Flags & OFN_EXPLORER)
pThreadState->m_pAlternateWndInit = this;
else
AfxHookWindowCreate(this);
INT_PTR nResult = 0;
if (m_bVistaStyle == TRUE)
{
ApplyOFNToShellDialog();
HRESULT hr = (static_cast<IFileDialog*>(m_pIFileDialog))->Show(m_ofn.hwndOwner);
nResult = (hr == S_OK) ? IDOK : IDCANCEL;
}
else if (m_bOpenFileDialog)
nResult = ::AfxCtxGetOpenFileName(&m_ofn);
else
nResult = ::AfxCtxGetSaveFileName(&m_ofn);
if (nResult)
ASSERT(pThreadState->m_pAlternateWndInit == NULL);
pThreadState->m_pAlternateWndInit = NULL;
if (bEnableParent)
::EnableWindow(m_ofn.hwndOwner, TRUE);
if (::IsWindow(hWndFocus))
::SetFocus(hWndFocus);
PostModal();
return nResult ? nResult : IDCANCEL;
}