简体   繁体   中英

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.

The ActiveX is exposed via a Windows Forms host.

[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.

    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

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

Methods and properties exposed in the GridWinForms are visible and can be get/set/invoked.

However after instantiating the ActiveX we eventually need to call into .Net - in the MATLAB process - via a MEX DLL. The call executes successfully in .Net but the MATLAB process freezes when control returns from .Net. 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.

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." The original poster concludes that he solved the issue with WPF + MFC but doesn't detail the solution.

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." 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.

So the issue seems to be related to hosting a managed ActiveX control on an unmanaged application - any ideas?

Not a real "solution" to your issue, but maybe a (imho good) alternative for the activeX stuff:

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

Syntax-wise you can use .NET classes almost as good as java-classes. Particularly, this should be much more comfortable than talking to .NET via MEX - I assume.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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