如何访问Winform ActiveX控件在MFC没有CLR [英] How to Access Winform ActiveX Control in MFC No CLR
问题描述
我的专案包含四个项目
- C#DLL通过Interop公开COM接口
- 一个WPF控件,其中包含1中的一个暴露类的实例
- 一个Winform ActiveX,它使用ElementHost在2中托管WPF控件 对话框应用程序使用控件从3
Winform ActiveX(3)通过在2中的函数暴露类实例1。通过ActiveX从MFC对话框应用程序访问此类实例。我已环顾四周,发现你可以使用CWinFormControl做到这一点。但是我不能自由地使用/ clr重新编译MFC应用程序。因此我不能使用CWinFormControl。
我可以通过COM从MFC应用程序访问类,并运行所有的函数等,但类是一个不同的实例,因为DLL被加载在自己的空间。
ActiveX运行良好,并显示所有WPF数据。
问题是如何获得一个指向ActiveX控件从MFC应用程序中的指针,而不使用CWinFormControl?
我已经尝试从ActiveX导入TLB,并尝试在类向导中为它创建一个变量,但它报告TLB不可用。我也尝试通过手动创建一个变量直接创建一个DDX条目,但DDX不允许指针。
任何想法?
在您的MFC对话框标题中添加一个CWnd
CWnd m_MyActiveX;
在您的MFC Cpp中动态创建控制
m_MyActiveX.CreateControl(MyActiveX.ProgId,,WS_VISIBLE,prect,this,5000);
注意:您可以在Winform ActiveX属性中找到progid
[ProgId(MyActiveX.ProgId)]
pre>
[ClassInterface(ClassInterfaceType.AutoDispatch)]
下一步获取COM对象的IUnknown和QueryInterface您需要
IOleObjectPtr pOleObj(m_MyActiveX.GetControlUnknown());
if(pOleObj!= NULL)
{
MyCOMObject :: IWpfHostPtr host;
pOleObj.QueryInterface(__ uuidof(MyCOMObject :: IWpfHostPtr),& host);
MyCOMWPFControl :: IWpfControl wpf;
host-> GetWpfControl(& wpf);
MyInternalCOMObject :: ICoolObject internal;
wpf-> GetInternalObject(& internal);
internal-> AndAPartridgeInaPearTree();
}
注意:实际Winform ActiveX必须来自一些已知的接口
public partial class WpfHost:UserControl,IWpfHost
使用此技术,您成功地在您的传统MFC应用程序上托管WPF控件,并通过COM与他们沟通,而无需使用托管C ++
My Project Consists of Four Items
- C# DLL Which Exposes a COM Interface via Interop
- A WPF Control which contains an Instance of an exposed class in 1
- A Winform ActiveX which hosts the WPF control in 2 using ElementHost
- An MFC Dialog Application using the Control from 3
The Winform ActiveX (3) exposes the class instance from 1 via a function in 2. I wish to access this class instance from the MFC dialog application through the ActiveX. I have looked around and found you can do this using CWinFormControl. However I am not at liberty to recompile the MFC app using /clr. Therefore I cannot use CWinFormControl.
I can access the class in 1 via COM from the MFC app and run all the functions etc however the class is a different instance as the DLL is loaded in its own space.
The ActiveX works well and displays all the WPF data nicely.
So the question is how do I get a pointer to the ActiveX control from within the MFC app without using CWinFormControl?
I have tried importing the TLB from the ActiveX and attempting to create a "Variable" for it in the Class wizard but it reports that the TLB is unavailable. I have also tried directly creating a DDX entry by manually creating a variable but DDX doesn't allow pointers.
Any ideas?
解决方案The Question is basically trying to access a Winform ActiveX Control in MFC without having to use clr or managed C++.
For anyone interested in the answer to this question here is how I solved it. First off you have to dynamically create the ActiveX and place it your self.
In Your MFC Dialog header add a CWnd
CWnd m_MyActiveX;
In your MFC Cpp dynamically create the Control
m_MyActiveX.CreateControl("MyActiveX.ProgId","",WS_VISIBLE,prect,this,5000);
NOTE: you can find the progid in your Winform ActiveX attributes
[ProgId("MyActiveX.ProgId")] [ClassInterface(ClassInterfaceType.AutoDispatch)]
Next Grab the IUnknown and QueryInterface for the COM Object You need
IOleObjectPtr pOleObj(m_MyActiveX.GetControlUnknown ()); if (pOleObj != NULL) { MyCOMObject::IWpfHostPtr host; pOleObj.QueryInterface(__uuidof(MyCOMObject::IWpfHostPtr),&host); MyCOMWPFControl::IWpfControl wpf; host->GetWpfControl ( &wpf ); MyInternalCOMObject::ICoolObject internal; wpf->GetInternalObject ( &internal ); internal->AndAPartridgeInaPearTree (); }
NOTE: The Actual Winform ActiveX must derive from some known Interface
public partial class WpfHost : UserControl, IWpfHost
Using this Technique you successfully host WPF Controls on your Legacy MFC Applications and communicate with them via COM without resorting to Managed C++
这篇关于如何访问Winform ActiveX控件在MFC没有CLR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!