Solved

HI Problem With ActiveX control In WIN32 Applications

Posted on 2000-04-28
6
274 Views
Last Modified: 2013-12-03
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.

0
Comment
Question by:austin123
  • 4
  • 2
6 Comments
 
LVL 2

Expert Comment

by:WDB
ID: 2759593
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_
0
 
LVL 2

Expert Comment

by:WDB
ID: 2759601
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_
0
 
LVL 2

Expert Comment

by:WDB
ID: 2931974
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.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 2

Accepted Solution

by:
WDB earned 50 total points
ID: 3319813
Once again, 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. I have reported this thread to EE.

0
 
LVL 3

Expert Comment

by:darinw
ID: 3321784
austin123 - Please post a comment and advise the Expert of your intentions.

darinw
Customer Service
0
 
LVL 3

Expert Comment

by:darinw
ID: 3354973
austin123 - You there?

darinw
Customer Service
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question