FIX: ATL Doesn't Call InitCommonControls in ActiveX Control (191355)



The information in this article applies to:

  • The Microsoft Active Template Library (ATL) 2.0, when used with:
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • The Microsoft Active Template Library (ATL) 2.1, when used with:
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 5.0

This article was previously published under Q191355

SYMPTOMS

An ATL ActiveX control based on one of the common controls may not appear in an ActiveX control container.

CAUSE

The ATL Object Wizard doesn't add a call to InitCommonControls() when you insert an ActiveX control based on a common control. The call to InitCommonControls() is required if you use any of the common controls in your ATL ActiveX control.

NOTE: If you didn't select "Support MFC" in the ATL Object Wizard when creating your project, then you must link with Comctl32.lib in order to call InitCommonControls(). Otherwise, an LNK2001 linker error may occur when calling InitCommonControls().

Among the common controls, the richedit control is an exception. Instead of calling InitCommonControls(), you need to do a LoadLibrary() on Riched32.dll.

RESOLUTION

This problem has been fixed in Visual C++ 6.0. The ATL Object Wizard adds a call to InitCommonControls() for you. For richedit controls, you have to call LoadLibrary() and FreeLibrary() on Riched32.dll. If you're using Visual C++ 5.0 or earlier, just add the call to InitCommonControls() before calling Create() for the common control. The Wizard usually calls Create() in OnCreate():
   LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam,
                    BOOL& bHandled)
   {
      RECT rc;
      GetWindowRect(&rc);
      rc.right -= rc.left;
      rc.bottom -= rc.top;
      rc.top = rc.left = 0;
      ::InitCommonControls();                // ADD THIS LINE
      m_ctlSysListView32.Create(m_hWnd, rc);
      return 0;
   }
				
For the richedit control, you'll need to load Riched32.dll and unload it:
   BEGIN_MSG_MAP(CRichEdit6)
      MESSAGE_HANDLER(WM_DESTROY, OnDestroy) // Add this line.
      // ...
   END_MSG_MAP()

   HINSTANCE m_hLibRichEdit;                 // Add this line.

   LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam,
                    BOOL& bHandled)
   {
      RECT rc;
      GetWindowRect(&rc);
      rc.right -= rc.left;
      rc.bottom -= rc.top;
      rc.top = rc.left = 0;
      m_hLibRichEdit =
         LoadLibrary(_T("RICHED32.DLL"));    // Add this line.
      m_ctlRichEdit.Create(m_hWnd, rc);
      return 0;
   }

   // Add this function.
   LRESULT OnDestroy(UINT, WPARAM, LPARAM, BOOL&)
   {
      m_ctlRichEdit.DestroyWindow();
      FreeLibrary(m_hLibRichEdit);
      return 0;
   }
				

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been corrected in Microsoft Visual C++, 6.0.

MORE INFORMATION

Containers such as ActiveX Control Test Container in MFC 4.2 already call InitCommonControls() so this problem does not occur.

Modification Type:MajorLast Reviewed:12/10/2003
Keywords:kbATL300fix kbBug kbCtrlCreate kbfix kbVC600fix kbwizard KB191355