大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> COM与.NET的互操作(初级)

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

时间: 2021-07-31 作者:daque

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.vbsdim 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.snk2,运用强称呼出面,编写翻译成类库,csc /t:library netserver.cs3,天生典型库tlbexp netserver.dll /out:netserver.tlb4,备案dllregasm netserver.dll5,移入gac全部步调集缓存gacutil -i netserver.dll6,挪用尝试剧本wscript test.vbs在这边有几个须要提防的场合,1,必需要给步调集出面,让它具备强称呼。2,必需将运用regasm备案步调集,它将会在备案表中增添相映的项。3,必需将出面后的强称呼步调集移入全部步调集缓存(gac)。4,必需要先安置scriptengine了,微软的剧本实行引擎。这是从剧本挪用.net 步调集了,呵呵,很大略吧?j二,从c/c++挪用.net组件仍旧一段步调,呵呵,步调即是我的人命:)//file1 name:netserver.csusing 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_guidsint 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.snk2,运用强称呼出面,编写翻译成类库,csc /t:library netserver.cs3,天生典型库 //这一步很要害tlbexp netserver.dll /out:netserver.tlb4,备案dllregasm netserver.dll5,移入gac全部步调集缓存gacutil -i netserver.dll6,编写翻译尝试步调cl comclient.cpp7,实行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东西中的本领了.

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.