Link to home
Start Free TrialLog in
Avatar of austin123
austin123

asked on

HI Problem With ActiveX control In WIN32 Applications

Hi all,
     I have the same problem as user sudhaom. When a dialog box with  an activex control is called it is not being shown.
How activex controls are used with WIN32 applications.

Avatar of WDB
WDB

The following outline describes the steps that need to be taken to create a Non-MFC C++ application that can contain ActiveX/COM controls and how to go about accessing those controls.

Add following lines to stdafx.h:
  #include <atlbase.h>
  extern CComModule _Module;
  #include <atlcom.h>
  #include <atlhost.h>

Add Following logic to main .cpp file:
At top:
  CComModule _Module;

At beginning of WinMain:
  _Module.Init(NULL,hInstance);
  AtlAxWinInit();

At the end of WinMain:
   _Module.Term()

#import the proper .dll,.ocx for the control you want to access and make sure to include the following options for the #import - raw_interfaces_only, raw_native_types, no_namespace, named_guids

// To Access An Object
1. Use the ATL CComPtr<> Template Class to Create An Instance Of The Interface.
      CComPtr<ISomething> pObject;
NOTE: ISomething is the interface we want access to. "Something" will be the actual name of the COM interface.

2. Use the GetDlgControl() API to obtain a handle to the control you have placed in your dialog and fill pObject with it's contents.
GetDlgControl(IDC_SOMETHING,IID_ISomething, (void **)&pObject);
NOTE: IDC_SOMETHING will be the control ID defined in your dialog, IID_ISomething will be the named guid of the interface we are obtaining.

3. Use the interface.
   pObject->SomeMethod1();
   pObject->SomeMethod2();
    ...


Here is an example of a Class that we could use to access an ACTIVEX/COM object from within a dialog. All classes that we create to do this should resemble this format:

#ifndef __NEWDLG_H_
#define __NEWDLG_H_

#include "resource.h"       // main symbols
#include <atlhost.h> // ALLOWS HOSTING OF COM/ACTIVEX CONTROLS IN DIALOG
#import "SomeDllOrOcx" raw_interfaces_only, raw_native_types, no_namespace, named_guids  

/////////////////////////////////////////////////////////////////////////////
// CNewDlg
class CNewDlg :
public CAxDialogImpl<CNewDlg> // USE THE ATL CAxDialogImpl Template Class
{
public:
CNewDlg() // Constructer()
{
}

~CNewDlg() // Destructer()
{
}

BEGIN_MSG_MAP(CNewDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_ID_HANDLER(IDOK, OnOK)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
      ...
      // CUSTOM CONTROL HANDLERS
COMMAND_HANDLER(IDC_MYBUTTON, BN_CLICKED, OnClickedMybutton)
      ...
END_MSG_MAP()

// Handler prototypes:
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);

//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

    LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 1;  // Let the system set the focus
}

LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
EndDialog(wID);
return 0;
}

LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
EndDialog(wID);
return 0;
}
public :

BEGIN_SINK_MAP(CNewDlg)
//Make sure the Event Handlers have __stdcall calling convention
END_SINK_MAP()

      // EXAMPLE OF ACCESSING OF A CONTROL
LRESULT OnClickedMybutton(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
CComPtr<ISomething> pObject;
GetDlgControl(IDC_SOMEID,IID_ISomething, (void **)&pObject);
pObject->SomeMethod1();
pObject->SomeMethod2();
pObject->SomeMethod3();
return 0;
}
};

#endif //__NEWDLG_H_
Sorry,
  I meant to originally post this as an answer.

The following outline describes the steps that need to be taken to create a Non-MFC C++ application that can contain ActiveX/COM controls and how to go about accessing those controls.

Add following lines to stdafx.h:
  #include <atlbase.h>
  extern CComModule _Module;
  #include <atlcom.h>
  #include <atlhost.h>

Add Following logic to main .cpp file:
At top:
  CComModule _Module;

At beginning of WinMain:
  _Module.Init(NULL,hInstance);
  AtlAxWinInit();

At the end of WinMain:
   _Module.Term()

#import the proper .dll,.ocx for the control you want to access and make sure to include the following options for the #import - raw_interfaces_only, raw_native_types, no_namespace, named_guids

// To Access An Object
1. Use the ATL CComPtr<> Template Class to Create An Instance Of The Interface.
      CComPtr<ISomething> pObject;
NOTE: ISomething is the interface we want access to. "Something" will be the actual name of the COM interface.

2. Use the GetDlgControl() API to obtain a handle to the control you have placed in your dialog and fill pObject with it's contents.
GetDlgControl(IDC_SOMETHING,IID_ISomething, (void **)&pObject);
NOTE: IDC_SOMETHING will be the control ID defined in your dialog, IID_ISomething will be the named guid of the interface we are obtaining.

3. Use the interface.
   pObject->SomeMethod1();
   pObject->SomeMethod2();
    ...


Here is an example of a Class that we could use to access an ACTIVEX/COM object from within a dialog. All classes that we create to do this should resemble this format:

#ifndef __NEWDLG_H_
#define __NEWDLG_H_

#include "resource.h"       // main symbols
#include <atlhost.h> // ALLOWS HOSTING OF COM/ACTIVEX CONTROLS IN DIALOG
#import "SomeDllOrOcx" raw_interfaces_only, raw_native_types, no_namespace, named_guids  

/////////////////////////////////////////////////////////////////////////////
// CNewDlg
class CNewDlg :
public CAxDialogImpl<CNewDlg> // USE THE ATL CAxDialogImpl Template Class
{
public:
CNewDlg() // Constructer()
{
}

~CNewDlg() // Destructer()
{
}

BEGIN_MSG_MAP(CNewDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_ID_HANDLER(IDOK, OnOK)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
      ...
      // CUSTOM CONTROL HANDLERS
COMMAND_HANDLER(IDC_MYBUTTON, BN_CLICKED, OnClickedMybutton)
      ...
END_MSG_MAP()

// Handler prototypes:
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);

//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

    LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 1;  // Let the system set the focus
}

LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
EndDialog(wID);
return 0;
}

LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
EndDialog(wID);
return 0;
}
public :

BEGIN_SINK_MAP(CNewDlg)
//Make sure the Event Handlers have __stdcall calling convention
END_SINK_MAP()

      // EXAMPLE OF ACCESSING OF A CONTROL
LRESULT OnClickedMybutton(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
CComPtr<ISomething> pObject;
GetDlgControl(IDC_SOMEID,IID_ISomething, (void **)&pObject);
pObject->SomeMethod1();
pObject->SomeMethod2();
pObject->SomeMethod3();
return 0;
}
};

#endif //__NEWDLG_H_
Why was this answer rejected? I have no problem with having my answer rejected but please comment as to why you have done so. The answer I have proposed is a clear and complete answer to your question, at least it appears that way to me.
ASKER CERTIFIED SOLUTION
Avatar of WDB
WDB

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
austin123 - Please post a comment and advise the Expert of your intentions.

darinw
Customer Service
austin123 - You there?

darinw
Customer Service