当前位置:编程学习 > C#/ASP.NET >>

matlab调用c# COM+的的例子

我们以下假设:

matlab版本:r2007a

dotnet:2005

com使用dotnet的服务化组件来生成并发布在机器192.168.10.69上,客户端使用matlab来调用这个组件。

1.使用dotnet vs2005来建立一个类库。关键代码如下:

User.cs: 

using System;
using System.Collections.Generic;
using System.Text;
//添加com及数据库引用
using System.EnterpriseServices;
using System.Collections;
using System.Windows.Forms;

using System.Data.OleDb;
using System.Data;
//using System.Collections.Generic;
//这里是实现一个com应用,包含3个实体user,Catalogue,Article;分别表示用户、资料类别、资料
//此模块最终被编译成dll并作为com部署在服务器。并且被已经实现的被作为客户端的matlab脚本调用
//陈海2009/3/15
/*-------------------------------------------------
    -----------------------------------------------------* */
namespace CharIdentifyCOM
{
    [Transaction(TransactionOption.Required)]
    //声明为服务器应用程序,还可以选择Library,表示为库应用程序



    public class User : ServicedComponent
    {
        private string connectionString;
        public User()
        {
        }
        protected override void Construct(string s)
        {
            connectionString = s;
            base.Construct(s);
        }
        [AutoComplete] //这就是一会儿我们要在客户端调用的方法
        public string  getUserName()
        {
            return "chenhai";
        }
    }

}
//注意您要添加引用 System.EnterpriseServices

Assembly.cs:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CharIdentifyCOM")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("ztc")]
[assembly: AssemblyProduct("CharIdentifyCOM")]
[assembly: AssemblyCopyright("Copyright © ztc 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("41dc3ec7-2b22-4f02-80eb-302c8930eb8c")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
//[assembly: AssemblyKeyFile(@"..\..\CharIdentifyKey.snk")] //2005似乎已经取消了。请在菜单project->app properties->Signing->Sign the assembly中指定由sn生成的文件名

2.打开vs的命令行,给dll生成强名称

生成强命名的文件E:\master\term2\graduated_design\proj\dot_net_proj\CharIdentifyCOM\CharIdentifyC
OM\bin\Debug>sn -k CharIdentifyCOM.snk

Microsoft (R) .NET Framework Strong Name Utility  Version 2.0.50727.42
Copyright (c) Microsoft Corporation.  All rights reserved.

Key pair written to CharIdentifyCOM.snk

3。注册com

E:\master\term2\graduated_design\proj\dot_net_proj\CharIdentifyCOM\CharIdentifyC
OM\bin\Debug>regsvcs /fc CharIdentifyCOM.dll
Microsoft (R) .NET Framework Services Installation Utility Version 2.0.50727.143
3
Copyright (c) Microsoft Corporation.  All rights reserved.

WARNING: The assembly does not declare an ApplicationAccessControl Attribute.  A
pplication security will be enabled by default.
Installed Assembly:
        Assembly: E:\master\term2\graduated_design\proj\dot_net_proj\CharIdentif
yCOM\CharIdentifyCOM\bin\Debug\CharIdentifyCOM.dll
        Application: CharIdentifyCOM
        TypeLib: E:\master\term2\graduated_design\proj\dot_net_proj\CharIdentify
COM\CharIdentifyCOM\bin\Debug\CharIdentifyCOM.tlb

E:\master\term2\graduated_design\proj\dot_net_proj\CharIdentifyCOM\CharIdentifyC
OM\bin\Debug>

 这是你可以通过"管理工具"看到这个com组件了

   如果注册错误,可删除注册:regsvcs /u CharIdentifyCOM.dll

4。打开matlab r2007a,调用COM:

在matlab命令行做,

>> h=actxserver('CharIdentifyCOM.User','machine','192.168.10.69') %%生成到com的访问服务器句柄h
 
h =
 
 COM.CharIdentifyCOM_User

>> get(h,'getUserName') %%调用com的方法

ans =

chenhai

>> release(h)  %%释放h

>> 

我们这里是最简单的验证,实际上,你可以在那个User类中实现更复杂的逻辑,比如访问数据库并返回给某个方法。我们也没有实现诸如COM安全等其他功能。自己试吧。




--------------------编程问答-------------------- 呵呵小弟受教了
最近我也在做MATLAB调COM,可以交流下吗
QQ21405589 --------------------编程问答-------------------- 我按照你的做了下,为什么你不要访问接口就直接调用方法了啊?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,