Click here to Skip to main content
11,632,450 members (81,565 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC dialog
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)");
 
	// TODO: Add your command handler code here
	CFileDlg fd(TRUE, "*.abs", "", OFN_HIDEREADONLY, szFilter, GetForegroundWindow() ); // | OFN_OVERWRITEPROMPT
		//"Open Bin File(*.abs)|Default DataBase File(*.ddf)|SatCodex File(*.sdx)|Format Text File(*.txt)");
	
	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;
 
//Hangs inside this call
	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
// FileDlg.h : header file
//
#include "FileExportDialog.h"

/////////////////////////////////////////////////////////////////////////////
// CFileDlg dialog

class CFileDlg : public CFileExportDialog
{
	DECLARE_DYNAMIC(CFileDlg)
 
public:
	CFileDlg(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
		LPCTSTR lpszDefExt = NULL,
		LPCTSTR lpszFileName = NULL,
		DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		LPCTSTR lpszFilter = NULL,
		CWnd* pParentWnd = NULL);
 
protected:
	//{{AFX_MSG(CFileDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};
 
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_FILEDLG_H__8C927F99_3840_4A93_9E17_E153C675F9D4__INCLUDED_)

File FileDlg.cpp code (initialising dialog):
// FileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AliEditor.h"
#include "FileDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
/////////////////////////////////////////////////////////////////////////////
// CFileDlg

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)
	//{{AFX_MSG_MAP(CFileDlg)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()




Other initialization part in FileExportDialog.cpp file called by code above:
//****************************************************************************
// N O L D U S   I N F O R M A T I O N   T E C H N O L O G Y   B . V .
//****************************************************************************
// Filename:      CFileExportDialog.cpp
// Project:       EthoVision
// Module:        Visualization
// Programmer:    Anneke Sicherer-Roetman
// Version:       1.00
// Revision Date: 22-03-1999
//****************************************************************************
// Description:   Definition of class CFileExportDialog
//                See CFileExportDialog.h
//****************************************************************************
// Revision history:
// 22-03-1999 - First implementation
//****************************************************************************
// Bugs: ........
//****************************************************************************
// @doc
//****************************************************************************

#include "stdafx.h"
#include <dlgs.h>
#include "FileExportDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
// helper functions, defined at end
int Split(char *str,char *word[],int max,char c);
char *newdup(const char *str);
 
IMPLEMENT_DYNAMIC(CFileExportDialog, FILEEXPORTDIALOG_BASECLASS)
 
//****************************************************************************
// Function CFileExportDialog::CFileExportDialog
// @mfunc   constructor, creates dialog title and filter buffers
// @parm    const CString | &title      | dialog title
// @parm    const CString | &filter     | file filter<nl>
//          The filter MUST be of the form "Word File (*.doc)\|Text File (*.txt)"<nl>
//          .i.e one extension per format and the formats separated by pipes.
// @parm    const CString | &fileName   | initial filename (default "")<nl>
//          The initial filetype is determined from this filename if given.
// @xref    <c CFileExportDialog>
//****************************************************************************
// @prog 
// Anneke Sicherer-Roetman
// @revs 
// 22-03-1999 - First implementation
//****************************************************************************
// @todo 
//****************************************************************************
CFileExportDialog::CFileExportDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
		LPCTSTR lpszDefExt,
		LPCTSTR lpszFileName,
		DWORD dwFlags,
		LPCTSTR lpszFilter,
		CWnd* pParentWnd) :
		base(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd),
      // if filter is "" (not NULL), an extension is added automatically!
      m_pTitleBuffer(NULL),
      m_pFilterBuffer(NULL),
      m_pFileBuffer(NULL),
      m_CurrentFilterIndex(1)
{
  // set dialog title
 // m_pTitleBuffer = newdup(title);
 // m_ofn.lpstrTitle = m_pTitleBuffer;
  CString filter(lpszFilter);
  CString fileName(lpszFileName);
 
  // construct filter string for standard file open dialog
  char *tempBuffer = newdup(filter);
  char *word[40]; // maximum of 40 file types
  // split original filter string on pipes
  int n = Split(tempBuffer,word,20,'|');
  char **fptr = new char *[n]; // note: pointers into tempBuffer
  // create new filter string
  m_pFilterBuffer = new char[filter.GetLength()+n*8+1];
  m_pFilterBuffer[0]='\0';
  // fill new filter string
  for (int i = 0; i < n; ++i) {
    // copy format description and add pipe
    strcat(m_pFilterBuffer,word[i]);
    strcat(m_pFilterBuffer,"|");
    // extract filter from format description
    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));
    // add filter and add pipe
    strcat(m_pFilterBuffer,part2[0]);
    strcat(m_pFilterBuffer,"|");
    // remember filter for later usage
    fptr[i] = part2[0]+1;
  }
  // replace pipes by nul characters
  int l = strlen(m_pFilterBuffer);
  for (int i = 0; i < l; ++i)
    if (m_pFilterBuffer[i] == '|')
      m_pFilterBuffer[i] = '\0';
  // set filter string
  m_ofn.lpstrFilter = m_pFilterBuffer;
 
  // set default filename in dialog
  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);
  }
 
  // set filter index from default 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;
        }
    }
  }
 
  // free temporary buffers
  delete tempBuffer;
  delete [] fptr;
}
....




Header file of FileExportDialog.h:
//****************************************************************************
// N O L D U S   I N F O R M A T I O N   T E C H N O L O G Y   B . V .
//****************************************************************************
// Filename:      CFileExportDialog.h
// Project:       EthoVision
// Module:        Visualization
// Programmer:    Anneke Sicherer-Roetman
// Version:       1.00
// Revision Date: 22-03-1999
//****************************************************************************
// Description:   Declaration of class CFileExportDialog
//****************************************************************************
// Revision history:
// 22-03-1999 - First implementation
//****************************************************************************
// Bugs: ........
//****************************************************************************
// @doc
//****************************************************************************
#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

//****************************************************************************
// @class         CFileExportDialog |
//                File save dialog with automatic change of extension<nl>
//                This dialog changes the extension in the filename field
//                in concert with the chosen file type.<nl>
//                The initial filetype is determined from the default filename, if given.<nl>
//                Otherwise the first filetype in the filter string is used.
// @base          public | CFileDialog
//****************************************************************************
// @ex
// This shows how to call the export dialog from your application: |
//
// CFileExportDialog dialog("Export graphics file", 
//   "Windows Bitmap (*.bmp)|JPEG Compressed Image (*.jpg)","visualization.jpg");
//
// if (dialog.DoModal() == IDOK) {
//   CString filename = dialog.GetPathName();
//   int filetype = dialog.GetFilterIndex();
//   SaveFile(filename, filetype);
// }
//****************************************************************************
// @prog 
// Anneke Sicherer-Roetman
// @revs 
// 22-03-1999 - First implementation
//****************************************************************************
// @todo 
//****************************************************************************
#define FILEEXPORTDIALOG_BASECLASS CFileDialog
 
class CFileExportDialog : public FILEEXPORTDIALOG_BASECLASS
{
  typedef FILEEXPORTDIALOG_BASECLASS base;
 
	DECLARE_DYNAMIC(CFileExportDialog)
 
  // @access Public Member Functions and Variables
public:
 
  // @cmember
  // constructor, creates dialog title and filter buffers
  CFileExportDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
		LPCTSTR lpszDefExt = NULL,
		LPCTSTR lpszFileName = NULL,
		DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		LPCTSTR lpszFilter = NULL,
		CWnd* pParentWnd = NULL);
 
  // @cmember
  // destructor, destroys dialog title and filter buffers
  virtual ~CFileExportDialog();
 
  // @cmember,mfunc
  // return current filter index (useful if two file formats have same extension)
  int GetFilterIndex() const { return m_CurrentFilterIndex; }
 
  // @access Protected Member Functions and Variables
protected:
 
  // @cmember
  // called when user selects other filetype, changes extension
  virtual void OnTypeChange();
 
  //{{AFX_MSG(CFileExportDialog)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
 
  // @access Private Member Functions and Variables
private:
 
  char *m_pTitleBuffer;      // @cmember contains title string
  char *m_pFilterBuffer;     // @cmember contains filter string
  char *m_pFileBuffer;       // @cmember contains filename string
  int m_CurrentFilterIndex;  // @cmember current file type
};
 
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#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); // can still be a user hook

    // zero out the file buffer for consistent parsing later
    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));
 
    //  This is a special case for the file open/save dialog,
    //  which sometimes pumps while it is coming up but before it has
    //  disabled the main window.
    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();
 

// HERE CALLS **OPENFILENAME& CFileDialog::GetOFN()** method and then hangs
            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;
 
    // Second part of special case for file open/save dialog.
    if (bEnableParent)
        ::EnableWindow(m_ofn.hwndOwner, TRUE);
    if (::IsWindow(hWndFocus))
        ::SetFocus(hWndFocus);
 
    PostModal();
    return nResult ? nResult : IDCANCEL;
}
Posted 2-Oct-12 2:56am
Edited 3-Oct-12 3:11am
v5

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

DoModal is fine, trust me.
Please post your code where your doing the initialization and then calling DoModal.
  Permalink  
Comments
Tomazas77 at 2-Oct-12 10:24am
   
See updates with code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,498
1 Sergey Alexandrovich Kryukov 8,149
2 Mika Wendelius 6,260
3 F-ES Sitecore 2,321
4 Suvendu Shekhar Giri 1,995


Advertise | Privacy | Mobile
Web01 | 2.8.150728.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100