Solved

HI Problem With ActiveX control In WIN32 Applications

Posted on 2000-04-28
6
266 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Accepted Solution

by:
WDB earned 50 total points
Comment Utility
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
Comment Utility
austin123 - Please post a comment and advise the Expert of your intentions.

darinw
Customer Service
0
 
LVL 3

Expert Comment

by:darinw
Comment Utility
austin123 - You there?

darinw
Customer Service
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now