打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Matlab调用c#的实例(已测试)
C#环境:VS2008 
Mablab:7.10.1(R2010a)

一、C#端示范
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//添加com及数据库引用
using System.Runtime.InteropServices;

namespace MatlabClass
{
    [Guid("5E019C5C-C021-4D6E-BEDE-3120525A15D8")]  //工具->创建GUID->...
    public interface Mat_I
    {
        //没有[DispId(*)]也可以

        [DispId(0)] //返回常数,在Open Selection中可见. 
        string GetName();
        [DispId(1)] //在Open Selection中不可见
        float Add(float a, float b);
    }

//自定义ProgID:将MatlabCS类的注册名改变,也可以不改变,则注册名称为"命名空间.类名"(见MatlabCS2)
    [ProgId("MatlabID")]    
    [ClassInterface(ClassInterfaceType.None)]
    public class MatlabCS:Mat_I
    {
    /// <summary>获取本类的名称</summary>
    /// <returns>本类的名称</returns>
        public string GetName() { return "I am MatlabCS"; }

        /// <summary>浮点数加法 </summary>
        /// <param name="a">加数</param>
        /// <param name="b">加数</param>
        /// <returns>和</returns>
        public float Add(float a, float b) { return a + b; }
    }
    public interface Mat_I2
    {
        [DispId(0)]  //在Open Selection中不可见?  未与[ClassInterface(ClassInterfaceType.None)]配匹
        string GetName2();
        [DispId(1)]
        float Add2(float a, float b);
    }
    public class MathlabCS2:Mat_I2  //COM 的注册名为MatlabClass.MatlabCS2
    {
        public string GetName2() { return "I am Number 2";}
        public float Add2(float a, float b) { return -(a+b);}
    }
}

修改AssemblyInfo.cs
[assembly: ComVisible(true)]

编译注册
MatlabClass属性 为COM互操作注册 (或者在外面注册也可)

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\regasm MatlabClass.dll /tlb: MatlabClass.tlb /codebase

OK
总结:C#制作COM(DLL)是常规做法,唯一要注意的是COM的注册名(ProgID)

二、Matlab端
>> h=actxserver('MatlabID')
 
h =
 
COM.MatlabID

>>  h=actxserver('MatlabClass.MathlabCS2')
 
h =
 
COM.MatlabClass_MathlabCS2

-----注意注册名(ProgID),而不是DLL的路径名

其他的h.get等不再说明

三、关于64位机的注意事项
应用一段原文:
Reinstall MATLAB as a 32-bit program.Your 32-bit driver will work with a 64-bit OS, but MATLAB can't be 64-bit for actxserver to load a 32-bit dll.

Under 32 bit environment, configure automatic regasm:
                Project properties
                   >Build tab
                       > check "Register for COM Interop"

        But this will fail under 64 bit (i.e. Build target x64" with the following error message: "File <path to assembly> is not a valid assembly"
        In an attempt to resolve this, uncheck "Register for COM Interop" from Project properties. Then run the following command after build (or add post-build command):
            C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\regasm SQLCmdRunner.dll /tlb: SQLCmdRunner.tlb
----在64位机上 Uncheck“为COM互操作注册”项。在64为机器上需要要用”Framework64“下的regasm进行显性注册


四、其他

1、 com注册信息全部存在于注册表的HKEY_CLASSES_ROOT键下,最主要的是CLSID子健。在CLS子键下,列除了当前机器上所有组件的信息。

2、interface可以简化,如果没有在接口中声明,即使该方法(属性)为公有,也不能正常导出到COM。但他们可以被别的.NET程序所使用;

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
老生常谈之C++和C#之间互相调用
progid滤镜的应用
通达信DLL函数编程规范(图文版)
win7和vista拨号上网与Matlab冲突
COM编程技术基础之四
MATLAB的实现Hilbert变换程序_matlab
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服