如何在 MFC No CLR 中访问 Winform ActiveX 控件 [英] How to Access Winform ActiveX Control in MFC No CLR

查看:32
本文介绍了如何在 MFC No CLR 中访问 Winform ActiveX 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目由四个项目组成

My Project Consists of Four Items

  1. 通过互操作公开 COM 接口的 C# DLL
  2. 一个 WPF 控件,其中包含 1 中公开类的实例
  3. 使用 ElementHost 承载 WPF 控件 2 的 Winform ActiveX
  4. 使用来自 3 的控件的 MFC 对话框应用程序

Winform ActiveX (3) 通过 2 中的函数从 1 公开类实例.我希望通过 ActiveX 从 MFC 对话框应用程序访问该类实例.我环顾四周,发现您可以使用 CWinFormControl 做到这一点.但是我不能使用/clr 重新编译 MFC 应用程序.因此我不能使用 CWinFormControl.

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.

我可以通过 COM 从 MFC 应用程序访问 1 中的类并运行所有函数等,但是该类是不同的实例,因为 DLL 是在其自己的空间中加载的.

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.

ActiveX 运行良好,可以很好地显示所有 WPF 数据.

The ActiveX works well and displays all the WPF data nicely.

所以问题是如何在不使用 CWinFormControl 的情况下从 MFC 应用程序中获取指向 ActiveX 控件的指针?

So the question is how do I get a pointer to the ActiveX control from within the MFC app without using CWinFormControl?

我尝试从 ActiveX 导入 TLB 并尝试在类向导中为其创建变量",但它报告 TLB 不可用.我也尝试通过手动创建变量直接创建 DDX 条目,但 DDX 不允许指针.

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.

有什么想法吗?

推荐答案

问题基本上是尝试在 MFC 中访问 Winform ActiveX 控件,而无需使用 clr 或托管 C++.

The Question is basically trying to access a Winform ActiveX Control in MFC without having to use clr or managed C++.

对于任何对此问题的答案感兴趣的人来说,我是如何解决它的.首先,您必须动态创建 ActiveX 并将其放置在您自己的位置.

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.

在您的 MFC 对话框标题中添加一个 CWnd

In Your MFC Dialog header add a CWnd

   CWnd m_MyActiveX;

在您的 MFC Cpp 中动态创建控件

In your MFC Cpp dynamically create the Control

   m_MyActiveX.CreateControl("MyActiveX.ProgId","",WS_VISIBLE,prect,this,5000);

注意:您可以在 Winform ActiveX 属性中找到 progid

NOTE: you can find the progid in your Winform ActiveX attributes

[ProgId("MyActiveX.ProgId")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]  

接下来为您需要的 COM 对象获取 IUnknown 和 QueryInterface

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 ();
 }

注意:实际的 Winform ActiveX 必须派生自某个已知接口

NOTE: The Actual Winform ActiveX must derive from some known Interface

public partial class WpfHost : UserControl, IWpfHost 

使用此技术,您可以成功地将 WPF 控件托管在旧版 MFC 应用程序上,并通过 COM 与它们进行通信,而无需使用托管 C++

Using this Technique you successfully host WPF Controls on your Legacy MFC Applications and communicate with them via COM without resorting to Managed C++

这篇关于如何在 MFC No CLR 中访问 Winform ActiveX 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆