dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: (COMCTL32.DLL): 0xC0000005: Access Violation.

  1. #1
    Join Date
    Apr 2007
    Posts
    23

    (COMCTL32.DLL): 0xC0000005: Access Violation.

    CPopupWnd is derived from CWnd

    CPopupWnd::CPopupWnd(int x, int y, CWnd* pParent)
    {
    try
    {
    LPCTSTR lpszWndName = AfxRegisterWndClass(
    CS_DBLCLKS,
    AfxGetApp()->LoadCursor(IDC_ARROW),
    CreateSolidBrush(GetSysColor(COLOR_BTNFACE)));

    CreateEx(
    0,
    lpszWndName,
    NULL,
    WS_CHILD | WS_VISIBLE | WS_POPUP | WS_BORDER,
    x,y,
    326,168,
    *pParent,
    NULL);

    SetOwner(pParent);
    }
    catch (CResourceException* pEx)
    {
    pEx->Delete();
    }
    }

    CPopupWnd::~CPopupWnd()
    {
    }


    BEGIN_MESSAGE_MAP(CPopupWnd, CWnd)
    //{{AFX_MSG_MAP(CPopupWnd)
    ON_WM_KILLFOCUS()
    ON_WM_CREATE()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()


    /////////////////////////////////////////////////////////////////////////////
    // CPopupWnd message handlers

    void CPopupWnd::PostNcDestroy()
    {
    // TODO: Add your specialized code here and/or call the base class
    delete this;

    CWnd::PostNcDestroy();
    }

    void CPopupWnd::OnKillFocus(CWnd* pNewWnd)
    {
    CWnd::OnKillFocus(pNewWnd);

    // TODO: Add your message handler code here
    DestroyWindow();
    }

    int CPopupWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CWnd::OnCreate(lpCreateStruct) == -1)
    return -1;

    // TODO: Add your specialized creation code here
    RECT rc;
    GetClientRect(&rc);
    //m_wndMonthCtrl is a member variable of type CMonthCalCtrl
    m_wndMonthCtrl.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, rc, this, 2008);

    return 0;
    }

    and use like this:
    CRect rcBtn;
    m_btnPopup.GetClientRect(rcBtn);
    m_btnPopup.ClientToScreen(rcBtn);
    new CPopupWnd(rcBtn.left, rcBtn.bottom, this);

    when I click the year of CMonthCalCtrl, it causes this access violation, why?
    Last edited by skyskeleton; December 12th, 2007 at 08:57 AM.

  2. #2
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: (COMCTL32.DLL): 0xC0000005: Access Violation.

    m_wndMonthCtrl.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, rc, this, 2008);

    From MSDN:

    Code:
    CMonthCalCtrl::Create
    BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
    
    BOOL Create( DWORD dwStyle, const POINT& pt, CWnd* pParentWnd, UINT nID );
    
    Return Value
    
    Nonzero if initialization was successful; otherwise 0.
    
    Parameters
    
    dwStyle
    
    Specifies the combination of Windows styles applied to the month calendar control. SeeMonth Calendar Control Styles in the Platform SDK for more information about the styles.
    
    rect
    
    A reference to aRECT structure. Contains the position and size of the month calendar control.
    
    pt
    
    A reference to aPOINT structure that identifies the location of the month calendar control. 
    
    pParentWnd
    
    A pointer to a CWnd object that is the parent window of the month calendar control. It must not be NULL.
    
    nID
    
    Specifies the month calendar controlís control ID.
    Are you sure that 2008 is the ID of the control??

  3. #3
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917

    Re: (COMCTL32.DLL): 0xC0000005: Access Violation.

    Quote Originally Posted by juanpast
    Are you sure that 2008 is the ID of the control??
    Of course it is, since it was created with this ID (2008).
    It would be better to get into a habit of using #define to define ID and pass it instead using value.
    In case ID has to be changed and is referenced in multiple places, changing it may prove error prone. Defining macro requires change only in one place.

    skyskeleton:
    Firstly, I would strongly suggest changing your constructor.
    Calling Create in the constructor of the MFC object is not a good idea.
    You do not check if CreateEx fails. However, even if you did and it fails you are not able to prevent object from continuing its instantiation, resulting in possible memory leaks and/or other unexpected behavior.
    Instantiation of the object may fail also for other reason and you can have a runaway window.
    After successful instantiation of the object, you can call member function that in turn calls CreateEx.

    Anyway, looking at your code I do not see any reason for having problem you described.

    Could you post your project?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  4. #4
    Join Date
    Nov 2007
    Posts
    613

    Re: (COMCTL32.DLL): 0xC0000005: Access Violation.

    WS_CHILD cannot be used with WS_POPUP style.

  5. #5
    Join Date
    Apr 2007
    Posts
    23

    Re: (COMCTL32.DLL): 0xC0000005: Access Violation.

    Quote Originally Posted by JohnCz
    skyskeleton:
    Firstly, I would strongly suggest changing your constructor.
    Calling Create in the constructor of the MFC object is not a good idea.
    You do not check if CreateEx fails. However, even if you did and it fails you are not able to prevent object from continuing its instantiation, resulting in possible memory leaks and/or other unexpected behavior.
    Instantiation of the object may fail also for other reason and you can have a runaway window.
    After successful instantiation of the object, you can call member function that in turn calls CreateEx.

    Anyway, looking at your code I do not see any reason for having problem you described.

    Could you post your project?
    Firstly,thank you for your reply.I create a new project according to your suggestion,but the exception already exist.The new project is in attachment.
    Attached Files Attached Files

  6. #6
    Join Date
    Apr 2007
    Posts
    23

    Re: (COMCTL32.DLL): 0xC0000005: Access Violation.

    Quote Originally Posted by srelu
    WS_CHILD cannot be used with WS_POPUP style.
    Thank you for your reply.

  7. #7
    Join Date
    Apr 2007
    Posts
    23

    Re: (COMCTL32.DLL): 0xC0000005: Access Violation.

    Quote Originally Posted by juanpast
    m_wndMonthCtrl.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, rc, this, 2008);

    Are you sure that 2008 is the ID of the control??
    Thank you for you reply.The control id is 2008.

  8. #8
    Join Date
    Nov 2007
    Posts
    613

    Re: (COMCTL32.DLL): 0xC0000005: Access Violation.

    Replace WS_POPUP with WS_CHILD. It will work.

  9. #9
    Join Date
    Apr 2007
    Posts
    23

    Re: (COMCTL32.DLL): 0xC0000005: Access Violation.

    Quote Originally Posted by srelu
    Replace WS_POPUP with WS_CHILD. It will work.
    Thank you for your reply.But if replace WS_POPUP with WS_CHILD,the window is invisible

  10. #10
    Join Date
    Nov 2007
    Posts
    613

    Re: (COMCTL32.DLL): 0xC0000005: Access Violation.

    I tried your application, noticed the bug, applied this change and it worked well.

    I noticed one problem, the control's placement in the window was changed. Instead of being centered it was in the right bottom cormer of the dialog. Maybe, because of a different resolution of your video card in your dialog is placed completely outside the dialog box and that makes it invisible. Try to change the coordinates of the control. (For the begining try 0,0.)
    Last edited by srelu; December 13th, 2007 at 04:27 AM.

  11. #11
    Join Date
    Apr 2007
    Posts
    23

    Re: (COMCTL32.DLL): 0xC0000005: Access Violation.

    Quote Originally Posted by srelu
    I tried your application, noticed the bug, applied this change and it worked well.

    I noticed one problem, the control's placement in the window was changed. Instead of being centered it was in the right bottom cormer of the dialog. Maybe, because of a different resolution of your video card in your dialog is placed completely outside the dialog box and that makes it invisible. Try to change the coordinates of the control. (For the begining try 0,0.)
    Sure enough as you said , thank you very much!
    According to your suggestion,I changed my code like this:
    void CPopupWindowDlg::OnPopupwnd()
    {
    // TODO: Add your control notification handler code here

    RECT rcBtn;
    m_btnPopup.GetWindowRect(&rcBtn);
    ScreenToClient(&rcBtn);
    m_pWndPopup = new CPopupWnd();
    BOOL bRet = m_pWndPopup->CreateWnd(rcBtn.left, rcBtn.bottom, this);
    }
    then it is visiable and works well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)