Solved

HI Problem With ActiveX control In WIN32 Applications

Posted on 2000-04-28
6
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

726 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