Microsoft KB Archive/205670

= How to obtain access to an ActiveX control from its property page =

Article ID: 205670

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q205670

<div class="notice_section">

Note Microsoft Visual C++ .NET (2002) and Microsoft Visual C++ .NET (2003) support both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

<div class="summary_section">

SUMMARY
When you use an ActiveX control, you may have to call member functions or gain access to member variables of the control class from its associated property pages. This article explains to do this and provides a code sample as illustration.

<div class="moreinformation_section">

MORE INFORMATION
A property sheet in an ActiveX control allows a user to manipulate the properties of a control by displaying one or more property pages. The properties can belong to one control or to a collection of ActiveX controls of the same type. The code sample demonstrates how to access the properties of a control or a group of selected controls from the property page of a control. It also demonstrates how to get direct access to the control instance. <ol> Generate a new MFC ActiveX control and specify the project name as MyTest. Accept all of the defaults for the control when prompted.</li>  Add a new member function to the control. Name the function SayHello, with return type void. Add the following code to it: void CMyTestCtrl::SayHello {   AfxMessageBox("Hello"); //Add this statement. }                   </li> Create a property:

In Visual C++ .NET <ol style="list-style-type: lower-alpha;"> Open the Class View window and then open MyTestLib (this is the node that represents the IDL file for the project).</li> Right-click the subnode for the default dispinterface (_DMyTest), Add, and then click Add Property.</li> In the Add Property Wizard, set the property type to LONG and the property name to ControlPointer. For Implementation, click to select Get/Set methods.</li> Click Finish to generate the property.</li></ol>

In earlier versions of Visual C++ <ol style="list-style-type: lower-alpha;"> Click the Automation tab in the MFC ClassWizard dialog box, and then add a new property for the control of type LONG.</li> Set the External name field to ControlPointer, select Get/Set methods for Implementation, and then accept the rest of the settings.</li></ol> </li>  Use the following code for the generated ControlPointer property methods: long CMyTestCtrl::GetControlPointer {   return reinterpret_cast (this); }

void CMyTestCtrl::SetControlPointer(long /*nNewValue*/) {   SetNotSupported; }                   </li> <li>Build the project to make the type library before proceeding.</li> <li>Generate automation wrappers for the control:

In Visual C++ .NET <ol style="list-style-type: lower-alpha;"> <li>On the Project menu, click Add Class Project.</li> <li>In the Add Class dialog box, click to select MFC Class from Typelib, and then click OK.</li> <li>In the Add Class From Typelib Wizard, locate Add class from, and then click to select File.</li> <li>Find the .tlb file that was generated for the control. The file is located in the Debug directory and is named MyTest.tlb. Use the selection arrows to move only the _DMyTest interface into the Generated classes pane. Note You must now change the Generated Classes class from the default CDMyTest to _DMyTest. This also changes the generated class names for the wrappers. The wizard prepends a 'C' to the generated wrapper classes, which may cause problems when you use some type libraries. Changing names is necessary for this type library to make the code match between Visual C++ .NET and earlier versions.</li> <li>Click Finish to generate the wrapper header files.</li></ol>

In earlier versions of Visual C++ <ol style="list-style-type: lower-alpha;"> <li>Click the Automation tab in the Class Wizard, click Add class, and then click From a type library.</li> <li>Find the .tlb file that was generated for the control. It is located in the Debug directory and is named MyTest.tlb.</li> <li>Select only the _DMyTest interface and click OK. The event interface is not needed.</li></ol> </li> <li>Using the resource editor, add a button to the property page. Change the caption to Access all selected controls.</li> <li> Add the control's header file after the other includes in the property page source file: #include "MyTestCtl.h"                   </li> <li>Add a button click handler for the button by using the Class Wizard.</li> <li> Add the following code in the button click handler function: void CMyTestPropPage::OnButton1 {       // Add this code: ULONG uNumControls;

// Get the array of IDispatchs stored in the property page. LPDISPATCH *lpDispatchControls = GetObjectArray(&uNumControls); for (ULONG i = 0; i < uNumControls; i++) {         CMyTestCtrl *pMyCtrl = GetControl(lpDispatchControls, i); if (pMyCtrl) pMyCtrl->SayHello; }   }                    </li> <li> Add the following global code method before the button click handler function: CMyTestCtrl* GetControl(LPDISPATCH *lpDispatchControls, ULONG iControlIndex) {       CMyTestCtrl *pMyCtrl = NULL;

// Get the CCmdTarget object associated. pMyCtrl = (CMyTestCtrl*) CCmdTarget::FromIDispatch(lpDispatchControls[iControlIndex]);

if (!pMyCtrl) // Above failed. Container must have aggregated the control. {          long ControlPointer;

_DMyTest control(lpDispatchControls[iControlIndex]);

// GetObjectArray docs state must not release pointer. control.m_bAutoRelease = FALSE; ControlPointer = control.GetControlPointer;

pMyCtrl = reinterpret_cast<CMyTestCtrl*>(ControlPointer); }

return pMyCtrl; }                   </li> <li>Build the project.</li> <li>Test the project in Visual C++ ActiveX Test Container. You see the message box when you click the Access all selected controls button on the property page.</li></ol>

<div class="references_section">