Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDialog::Create fails for dialog with ActiveX control

I have a module that creates a modeless dialog containing an ActiveX control. This module was part of an MFC EXE application and the creation of the dialog worked fine. Recently, I moved the module out into an ATL/COM server and copied the dialog resource from the EXE into the COM server. When trying to create the modeless dialog using CDialog::Create() an error happens.

I debugged into CDialog::Create and noticed that it fails in ::CreateDialogIndirect() which returns NULL and GetLastError returns 0. I changed the "No Fail Create" flag to True in the dialog resource properties and I get more details into the error. The problem happens in the dialog's DoDataExchange() within the DDX_Control macro. This calls into the CDataExchange::PrepareCtrl() with the control's resource ID as follows:

HWND CDataExchange::PrepareCtrl(int nIDC)
{
   ASSERT(nIDC != 0);
   ASSERT(nIDC != -1); // not allowed
   HWND hWndCtrl;
   COleControlSite* pSite = NULL;
   m_pDlgWnd->GetDlgItem(nIDC, &hWndCtrl);
   if (hWndCtrl == NULL)
   {
      // Could be a windowless OCX
      pSite = m_pDlgWnd->GetOleControlSite(nIDC);
      if (pSite == NULL)
      {
         TRACE(traceAppMsg, 0, "Error: no data exchange control with ID 0x%04X.\n", nIDC);
         ASSERT(FALSE);
         AfxThrowNotSupportedException();
      }
   }
   m_idLastControl = nIDC;
   m_bEditLastControl = FALSE; // not an edit item by default

   return hWndCtrl;
}

The call to the function m_pDlgWnd->GetOleControlSite() fails for the resource ID passed. By the way, the resource ID is the control's ID.

Any suggestions on why this works inside the EXE and fails in the COM server?

like image 817
user1756114 Avatar asked Nov 08 '12 15:11

user1756114


1 Answers

I had exactly the same problem. In my case the problem turned out to be that I hadn't called AfxEnableControlContainer(). I added a call to that in my app's InitInstance member function and it fixed the problem.

like image 54
user2312514 Avatar answered Sep 24 '22 18:09

user2312514