首页 > 开发 > .Net > 正文

COM与.NET的互操作(初级)

2020-02-03 15:56:04
字体:
来源:转载
供稿:网友
com与.net的互操作(初级)
com与.net的互操作中从.net调用com组件,如果使用vs.net将变得非常容易,你只需要在你的工程中,添加对应的com引用,编译工具就在后台悄悄的把com“变成”了.net程序集。而从传统的语言调用调用.net组件却不如那么方便了。所以,我整理了个人调试成功的几段程序希望对大家有一些帮助,好了废话少说进入正题。



一,从vbscript等脚本调用.net组件

首先我们可以写一个.net dll如下

//the first file:netserver.cs

using system;

using system.reflection;

using system.runtime.interopservices;



[assembly: assemblykeyfile("key.snk")]

namespace csharpserver

{

//缺省的是classinterfacetype.autodispatch,该方式下只生成dispatch接口

//只能被使用script、vb等late binding方式的com客户使用

[classinterfaceattribute(classinterfacetype.autodual)]

public class sharpobject

{

private string m_strname;



public sharpobject(){}



public string name //property: name, get/set

{

get { return m_strname; }

set { m_strname = value; }

}

}

}



//the second file: test.vbs

dim obj

set obj = createobject("csharpserver.sharpobject")

obj.name = "chang ming"

msgbox "my name is " & obj.name



对这两个文件按如下方式编译,为了清晰起见我们使用命令行工具(命令行工具环境可以从开始——>microsoft visual studio .net——>visual studio .net 工具——>visual studio .net 命令提示中进入)

1,生成密钥文件,用于给程序集强名称签名

sn -k key.snk



2,使用强名称签名,编译成类库,

csc /t:library netserver.cs



3,生成类型库

tlbexp netserver.dll /out:netserver.tlb



4,注册dll

regasm netserver.dll



5,移入gac全局程序集缓存

gacutil -i netserver.dll



6,调用测试脚本

wscript test.vbs



在这里有几个需要注意的地方,1,必须要给程序集签名,让它具有强名称。2,必须将使用regasm注册程序集,它将会在注册表中添加相应的项。3,必须将签名后的强名称程序集移入全局程序集缓存(gac)。4,必须要先安装scriptengine了,微软的脚本执行引擎。这是从脚本调用.net 程序集了,呵呵,很简单吧?j



二,从c/c++调用.net组件

还是一段程序,呵呵,程序就是我的生命:)

//file1 name:netserver.cs

using system;

using system.reflection;

using system.runtime.interopservices;



[assembly: assemblykeyfile("key.snk")]

namespace csharpserver

{



public interface iobject //声明接口

{

double sub(double c,double d);

}



//[classinterfaceattribute(classinterfacetype.autodual)]



public class sharpobject:iobject

{

private string m_strname;



public sharpobject(){}



public string name //property: name, get/set

{

get { return m_strname; }

set { m_strname = value; }

}



public double add(double a,double b)

{

console.writeline("the answer is {0}",a+b);

return a+b;

}

public double sub(double c,double d) //实现接口方法

{

console.writeline("the answer is {0}",c-d);

return c-d;

}

}

}





//file2 name: comclient.cpp

#include <windows.h>

#include <stdio.h>

#include <iostream.h>

#pragma warning (disable: 4278)



#import "netserver.tlb" no_namespace named_guids



int main(int argc, char* argv[])

{

iobject *cpi = null;

int retval = 1;



// initialize com and create an instance of the interfaceimplementation class:

coinitialize(null);

hresult hr = cocreateinstance(clsid_sharpobject,

null,

clsctx_inproc_server,

iid_iobject,

reinterpret_cast<void**>(&cpi));



if (failed(hr))

{

printf("couldn't create the instance!... 0x%x/n", hr);

}

else

{



printf("calling function./n");



retval = 0;

cout<<"10-4="<<cpi->sub(10,4)<<endl;

printf("returned from function./n");



cpi->release();//释放com对象

}



// be a good citizen and clean up com:

couninitialize();

return retval;

}



编译方法还是如前

1,生成密钥文件,用于给程序集强名称签名

sn -k key.snk



2,使用强名称签名,编译成类库,

csc /t:library netserver.cs



3,生成类型库 //这一步很重要

tlbexp netserver.dll /out:netserver.tlb



4,注册dll

regasm netserver.dll



5,移入gac全局程序集缓存

gacutil -i netserver.dll



6,编译测试程序

cl comclient.cpp

7,执行comclient.exe



说明:

在c/c++中调用com要麻烦一些,首先要调用com库函数coinitialize(null);进行初始化,然后调用hresult hr = cocreateinstance(clsid_sharpobject,

null,

clsctx_inproc_server,

iid_iobject,

reinterpret_cast<void**>(&cpi));

其中clsid_sharpobject是sharpobject类(com类)的类id它是由工具生成的用来唯一标识sharpobject类,iid_iobject唯一标识iobject接口,如果cocreateinstance成功的创建了com对象,那么failed(hr)将为false,得到com对象的指针后,就可以用它调用com对象中的方法了.

  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表