Link to home
Start Free TrialLog in
Avatar of sternocera
sternocera

asked on

MFC/C++: a hwnd related assertion failure is caused when I copy an existing dialog resource for another CDialog class

Hello,

I've created a new CDialog derived class, which is similar to an existing one. I copied the existing Dialog resource for my new CDialog derived class, and added new objects for the dialog's widgets, which don't necessarily have the same name as the old objects.

The dialog executes, until I attempt to call any of the object's member functions, which causes various assertion/ensure failures:

This occurs when I attempt to call a CEdit's CWnd::SetWindowTextA(), for example:

void CWnd::SetWindowText(LPCTSTR lpszString)
{
      ENSURE(this);
      ENSURE(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL)); // this is the offending line

      if (m_pCtrlSite == NULL)
            ::SetWindowText(m_hWnd, lpszString);
      else
            m_pCtrlSite->SetWindowText(lpszString);
}

I'm unsure what has gone wrong. Please help!

Regards,
Sternocera
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi sternocera,

hard to say from that code fragment ...

You should first ensure that your code doesn't use any control-ID which isn't present in the copies resource.

Is there a call to SetWindowText in your dialog's class? Or is this called from another MFC function?

Maybe you could post some more code, especially the functions which call this above given code (from call-stack)?


regards,

ZOPPO
Appears to be you have instantiated your dialog, but have not created, could you show the lines where you first use the object?
Avatar of sternocera
sternocera

ASKER

Zoppo,

Here is the offending code:
void CEditExistingProduct::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      DDX_Control(pDX, IDC_SKU, SKUEdit); // IDC_SKU definitely exists in my dialog resource
...
      
}
...
BOOL CEditExistingProduct::OnInitDialog()
{
      CString sku_is; sku_is.Format("%i", Sku);
      //SKUEdit.SetWindowTextA(sku_is); // I have these commented out, so there is no assertion failure now
      //SKUEdit.EnableWindow(FALSE);
      
      return TRUE;
}

Incidentally, I'm sorry I didn't give you a fair share of the points that time, Zoppo. It was a stupid oversight on my part.

Thanks,
Sternocera


jaime_olivares,

This is how the CDialog derived object gets instantiated:

void CProductSearch::OnDoubleClickedRow(NMHDR* pNMHDR, LRESULT* pResult)
{
      int current = ProductReport.GetFirstSelectedItem();
      CString sku = ProductReport.GetItemText(current,1);
      int SkuInt = atoi(sku.GetBuffer(0));
      sku.ReleaseBuffer();
      
      CEditExistingProduct MyEdit;
      MyEdit.ChangeSku(SkuInt);
      MyEdit.DoModal();

}

Thanks,
Sternocera
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Zoppo,

Including CDialog::OnInitDialog() in my function overload is something that I know I should have done, but forgot to.

Now that I have, no more assertion failures. Silly me.

Thanks a lot for your time,
Sternocera


No problem, you're welcome ...