打开APP
userphoto
未登录

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

开通VIP
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 erm2graduated_designprojdot_net_projCharIdentifyCOMCharIdentifyC
OMinDebug>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 erm2graduated_designprojdot_net_projCharIdentifyCOMCharIdentifyC
OMinDebug>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 erm2graduated_designprojdot_net_projCharIdentif
yCOMCharIdentifyCOMinDebugCharIdentifyCOM.dll
        Application: CharIdentifyCOM
        TypeLib: E:master erm2graduated_designprojdot_net_projCharIdentify
COMCharIdentifyCOMinDebugCharIdentifyCOM.tlb

E:master erm2graduated_designprojdot_net_projCharIdentifyCOMCharIdentifyC
OMinDebug>

 这是你可以通过"管理工具"看到这个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安全等其他功能。自己试吧。


 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
snk文件和AssemblyInfo.cs文件的作用[转]
在VBA中调用C#写成的DLL
C#编写COM组件的方法分析 / 张生荣
unity疑难杂症(二)——导入动态链接库报错
C#中动态加载和卸载DLL
【CAD 批量打图精灵 6.4】带授权文件&多文档多格式打印
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服