简体   繁体   English

在64位MATLAB中使用64位.Net ActiveX控件

[英]Using a 64bit .Net ActiveX control in 64bit MATLAB

We started using MATLAB 64 bits in our system and some of our legacy M code uses a custom version of the MSFlexGrid ActiveX component so we decided to write a .Net 64 bit version of it. 我们开始在系统中使用MATLAB 64位,并且我们的某些旧版M代码使用MSFlexGrid ActiveX组件的自定义版本,因此我们决定为其编写.Net 64位版本。

The ActiveX is exposed via a Windows Forms host. ActiveX通过Windows Forms主机公开。

[ProgId("FlexiGrid")]
[Guid("88888888-4444-4444-4444-CCCCCCCCCCCC")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public partial class GridWinFormsHost : UserControl
{
}

The Windows Form User Control embeds a WPF UserControl via ElementHost. Windows窗体用户控件通过ElementHost嵌入WPF用户控件。

    private IGrid grid;

    private void GridWinFormsHostLoad(object sender, System.EventArgs e)
    {
        var host = new ElementHost { Dock = DockStyle.Fill };
        this.grid = new GridView();
        host.Child = (GridView)this.grid;
        this.Controls.Add(host);
    }

The control is successfully registered, visible to MATLAB, and can be instantiated via 该控件已成功注册,对于MATLAB可见,可以通过以下方式实例化

actxcontrol('FlexiGrid', Position, Fig, CallBack);

Methods and properties exposed in the GridWinForms are visible and can be get/set/invoked. GridWinForms中公开的方法和属性是可见的,可以获取/设置/调用。

However after instantiating the ActiveX we eventually need to call into .Net - in the MATLAB process - via a MEX DLL. 但是,在实例化ActiveX之后,我们最终需要通过MEX DLL在MATLAB过程中调用.Net。 The call executes successfully in .Net but the MATLAB process freezes when control returns from .Net. 调用在.Net中成功执行,但是当控件从.Net返回时,MATLAB进程冻结。 Running the same code without instantiating the ActiveX control succeeds and MATLAB doesn't freeze, which means that instantiating the .Net ActiveX control is probably the cause of MATLAB freezing. 在不实例化ActiveX控件的情况下运行相同的代码成功,并且MATLAB不会冻结,这意味着实例化.Net ActiveX控件可能是MATLAB冻结的原因。

When searching for solutions I came across this MSDN thread in which it is stated: "This problem occurs because the message loop that the Windows Form uses and the message loop that the COM client application provides are different." 在搜索解决方案时,我遇到了一个MSDN线程 ,其中指出: “发生此问题的原因是Windows窗体使用的消息循环和COM客户端应用程序提供的消息循环是不同的。” The original poster concludes that he solved the issue with WPF + MFC but doesn't detail the solution. 原始海报得出的结论是,他使用WPF + MFC解决了该问题,但未详细说明解决方案。

I also found this MSDN article which states that: "To make a Windows Form work correctly from a COM client application, you must run the form on a Windows Forms message loop." 我还发现该MSDN文章指出: “要使Windows窗体从COM客户端应用程序正常工作,必须在Windows窗体消息循环上运行该窗体。” This is also promising except that the solution focus on creating new Windows Form windows but I need to run a Windows Forms user control embedded in a MATLAB window. 除了解决方案着重于创建新的Windows Form窗口外,这还很有希望,但是我需要运行MATLAB窗口中嵌入的Windows Forms用户控件。

So the issue seems to be related to hosting a managed ActiveX control on an unmanaged application - any ideas? 因此,问题似乎与在非托管应用程序上托管托管ActiveX控件有关-有什么想法吗?

Not a real "solution" to your issue, but maybe a (imho good) alternative for the activeX stuff: 这不是解决问题的真正“解决方案”,但可能是ActiveX的替代方案(imho好):

Starting from some MATLAB version (I think ~2009 or so, others might correct me if not) you can use .NET libraries directly from MATLAB, without the need for the COM-interface: http://www.mathworks.de/de/help/matlab/getting-started.html 从某些MATLAB版本开始(我认为大约是2009年,如果没有,其他版本可能会纠正我),您可以直接从MATLAB使用.NET库,而无需COM接口: http : //www.mathworks.de/de /help/matlab/getting-started.html

Syntax-wise you can use .NET classes almost as good as java-classes. 在语法方面,您可以使用几乎与Java类一样好的.NET类。 Particularly, this should be much more comfortable than talking to .NET via MEX - I assume. 特别是,这应该比通过MEX与.NET进行交谈要舒服得多-我认为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM