Article ID: 143432 - Last Review: November 21, 2006 - Revision: 5.1 How to gain access to member variables of the control derived class from its property page when you use an ActiveX controlThis article was previously published under Q143432 Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. On This PageSUMMARY When you are using an ActiveX control, you may experience situations in which you want to call member functions or to gain access to member variables of
the control derived class from its associated property page. You can do this by using the array of IDispatch pointers that represent the objects that are manipulated through the property page. (Each property page holds an array of IDispatch pointers.) This article explains in detail
how to do this. This article includes a code sample to illustrate this method.
MORE INFORMATION In an ActiveX control, property sheets let an end user
directly manipulate the control's properties. Property sheets display one or more
property pages that contain a collection of properties. These properties could
belong either to one particular control or to a collection of ActiveX controls.
Each ActiveX control property page is an in-process object. Each property page has its own CLSID that implements the interface IPropertyPage. The IPropertyPage::SetObjects member function is used to provide a property page with pointers to the objects (IUnknowns) that are manipulated by this particular page. (For more information about the SetObjects function, see the OLE Programmer's Reference, Vol. 1.) The MFC implementation for the IPropertyPage interface stores the object pointers as an array of IDispatch pointers. The pointers represent the controls that are affected by a particular property page. You can access this array by using COlePropertyPage::GetObjectArray. The property pages in MFC use this IDispatch array to apply the changes directly to the controls by creating a COleDispatchDriver-derived class, attaching the IDispatch array to this class, and invoking the SetProperty/GetProperty of COleDispatchDriver to convey the change to the control-derived class. An ActiveX control that is generated by using the ControlWizard creates a property page that you can use to manipulate the properties of one particular ActiveX control instead of manipulating a collection of controls. Therefore, you can access the control that is associated with a property page by obtaining the previously mentioned IDispatch array in the COlePropertyPage and then calling the static function CCmdTarget::FromIDispatch. The function returns a pointer to the CCmdTarget object that is associated with any one of the IDispatch pointers. The sample code section of this article illustrates this method. Note Calling CCmdTarget::FromIDispatch for an IDispatch pointer belonging to an ActiveX control will always return NULL in versions before MFC 4.x. For more information about this problem, click the following article number to view the article in the Microsoft Knowledge Base: 138414
(http://support.microsoft.com/kb/138414/
)
FromIDispatch returns NULL for OLE control
This is no longer a problem in versions MFC 4.x.
Sample codeImportant Note CCmdTarget::FromIDispatch may return NULL when the control is created with aggregation support. This behavior is noticeable in containers such as Microsoft Visual C++ 6.0 ActiveX Test Container, Microsoft Excel 97, Excel 2000, Microsoft FrontPage 98, and perhaps others. Therefore, the previous method will not work in those containers. A possible workaround is to make the control nonaggregatable by setting the following in the control constructor. 205670
(http://support.microsoft.com/kb/205670/
)
How to obtain access to an ActiveX control from its property page
APPLIES TO
| Article Translations
|
Back to the top
