第四章 COM组件与.Net组件互操作---3、DotNet使用COM组件
2011-11-26 17:28
393 查看
在这里我们只讨论如何在Dotnet中使用COM,首先我们必须创建一个COM组件。为了节省篇幅,我们直接使用第三章的Calcualtor COM组件库,这个组件库我们实现了ICaluclator和ICaluclator2接口。
为了在DotNet中使用COM组件,首先必须创建一个Runtime Callable Wrapper (RCW),它实现了对COM组件的包装,隐藏了IUnknown和IDispatch接口,并处理COM对象的引用计数问题。RCW使用TlbImp.exe来实现对COM的.Net包装。这个工具位于…\Microsoft Visual Studio 8\SDK\v2.0\Bin目录下。下面我们看看这个工具命令参数。
Syntax: TlbImp TypeLibName [Options]
Options:
/out:FileName File name of assembly to be produced
/namespace:Namespace Namespace of the assembly to be produced
/asmversion:Version Version number of the assembly to be produced
/reference:FileName File name of assembly to use to resolve references
/tlbreference:FileName File name of typelib to use to resolve references
/publickey:FileName File containing strong name public key
/keyfile:FileName File containing strong name key pair
/keycontainer:FileName Key container holding strong name key pair
/delaysign Force strong name delay signing
/unsafe Produce interfaces without runtime security checks
/noclassmembers Prevents TlbImp from adding members to classes
/nologo Prevents TlbImp from displaying logo
/silent Suppresses all output except for errors
/verbose Displays extra information
/primary Produce a primary interop assembly
/sysarray Import SAFEARRAY as System.Array
/machine:MachineType Create an assembly for the specified machine type
/transform:TransformName Perform the specified transformation
/strictref Only use assemblies specified using /reference and
registered PIAs
/strictref:nopia Only use assemblies specified using /reference and
ignore PIAs
/? or /help Display this usage message
一般我们将一个COM组件进行包装采用下列的简单命令行来完成。
Tlbimp COM.dll /out:Dotnet.dll
例如,我将Calcualtor.dll转换成DotNet组件的命令行如下:
Tlbimp Calculculator.dll /out: interop.Calculator.dll.
这样会生成一个interop.Calculator.dll文件。这个工程也可以直接通过Visual Studio 2005来完成。下面我们创建一个DotNet C#的控制台程序TestCOM,见图4-1。(具体创建步骤见参考文献[4])。
图4-1 C#版的控制台程序
接着选择“项目”->“添加引用…”,出现如图4-2所示的界面,选择COM属性业,选择Calculator 1.0类型库。单击确定。
图4-2 添加COM引用
现在Calculator组件库已经添加到了工程中。我们会发现在工程的应用内已经出现了Calculator。如图4-3。
图4-3 完成COM组件Calculator的引用添加
在项目的Obj\debug内已经出现了Interop.Calculator.dll文件。这说明已经自动调用了TlbImp.Exe。下面我们开始在c#中使用COM组件。
using System;
using System.Collections.Generic;
using System.Text;
using Calculator;//使用组件库
namespace TestCOM
{
class Program
{
static void Main(string[] args)
{
//创建Calculator组件
Calculator.CCalculatorClass obj = new Calculator.CCalculatorClass();
ICalculator calc = (ICalculator)obj;//强制转换获取接口
double d = calc.Add(2, 3);//调用接口的方法
System.Console.WriteLine("2+3=" + d);
ICalculator2 calc2 = (ICalculator2)obj;//强制转换获取接口
d = calc2.Mul(2, 3);//调用接口的方法
System.Console.WriteLine("2X3=" + d);
System.Console.WriteLine("\n");
}
}
}
我们可以看到,在C#中使用组件比我们在C++中使用组件要简单许多,也更容易理解。对于这一点,我只能说,对于傻瓜相机和专业相机,如果你不想成为一个好的专业摄影师,你完全可以使用傻瓜相机。
为了在DotNet中使用COM组件,首先必须创建一个Runtime Callable Wrapper (RCW),它实现了对COM组件的包装,隐藏了IUnknown和IDispatch接口,并处理COM对象的引用计数问题。RCW使用TlbImp.exe来实现对COM的.Net包装。这个工具位于…\Microsoft Visual Studio 8\SDK\v2.0\Bin目录下。下面我们看看这个工具命令参数。
Syntax: TlbImp TypeLibName [Options]
Options:
/out:FileName File name of assembly to be produced
/namespace:Namespace Namespace of the assembly to be produced
/asmversion:Version Version number of the assembly to be produced
/reference:FileName File name of assembly to use to resolve references
/tlbreference:FileName File name of typelib to use to resolve references
/publickey:FileName File containing strong name public key
/keyfile:FileName File containing strong name key pair
/keycontainer:FileName Key container holding strong name key pair
/delaysign Force strong name delay signing
/unsafe Produce interfaces without runtime security checks
/noclassmembers Prevents TlbImp from adding members to classes
/nologo Prevents TlbImp from displaying logo
/silent Suppresses all output except for errors
/verbose Displays extra information
/primary Produce a primary interop assembly
/sysarray Import SAFEARRAY as System.Array
/machine:MachineType Create an assembly for the specified machine type
/transform:TransformName Perform the specified transformation
/strictref Only use assemblies specified using /reference and
registered PIAs
/strictref:nopia Only use assemblies specified using /reference and
ignore PIAs
/? or /help Display this usage message
一般我们将一个COM组件进行包装采用下列的简单命令行来完成。
Tlbimp COM.dll /out:Dotnet.dll
例如,我将Calcualtor.dll转换成DotNet组件的命令行如下:
Tlbimp Calculculator.dll /out: interop.Calculator.dll.
这样会生成一个interop.Calculator.dll文件。这个工程也可以直接通过Visual Studio 2005来完成。下面我们创建一个DotNet C#的控制台程序TestCOM,见图4-1。(具体创建步骤见参考文献[4])。
图4-1 C#版的控制台程序
接着选择“项目”->“添加引用…”,出现如图4-2所示的界面,选择COM属性业,选择Calculator 1.0类型库。单击确定。
图4-2 添加COM引用
现在Calculator组件库已经添加到了工程中。我们会发现在工程的应用内已经出现了Calculator。如图4-3。
图4-3 完成COM组件Calculator的引用添加
在项目的Obj\debug内已经出现了Interop.Calculator.dll文件。这说明已经自动调用了TlbImp.Exe。下面我们开始在c#中使用COM组件。
using System;
using System.Collections.Generic;
using System.Text;
using Calculator;//使用组件库
namespace TestCOM
{
class Program
{
static void Main(string[] args)
{
//创建Calculator组件
Calculator.CCalculatorClass obj = new Calculator.CCalculatorClass();
ICalculator calc = (ICalculator)obj;//强制转换获取接口
double d = calc.Add(2, 3);//调用接口的方法
System.Console.WriteLine("2+3=" + d);
ICalculator2 calc2 = (ICalculator2)obj;//强制转换获取接口
d = calc2.Mul(2, 3);//调用接口的方法
System.Console.WriteLine("2X3=" + d);
System.Console.WriteLine("\n");
}
}
}
我们可以看到,在C#中使用组件比我们在C++中使用组件要简单许多,也更容易理解。对于这一点,我只能说,对于傻瓜相机和专业相机,如果你不想成为一个好的专业摄影师,你完全可以使用傻瓜相机。
相关文章推荐
- .NET组件和COM组件之间的相互操作
- .NET组件和COM组件之间的相互操作
- .NET组件和COM组件之间的相互操作(转载)
- .NET组件和COM组件之间的相互操作
- .NET组件和COM组件之间的相互操作
- .NET中开源文档操作组件DocX的介绍与使用
- .NET操作RabbitMQ组件EasyNetQ使用中文简版文档。
- .NET组件和COM组件之间的相互操作//(转)
- 深入Lumisoft.NET组件POP3邮件接收与删除操作的使用详解
- 在C#中使用WinGraphviz,一个COM图论组件,兼谈COM接口IStream在.NET下的互操作
- .NET组件和COM组件之间的相互操作
- .NET组件和COM组件之间的相互操作
- .NET组件和COM组件之间的相互操作
- .NET组件和COM组件之间的相互操作
- Flex与.NET互操作(八):使用FluorineFx网关实现远程访问
- [.NET] 打造一个很简单的文档转换器 - 使用组件 Spire.Office
- 在项目中使用SmartUpload组件可以进行文件的上传和下载操作
- 从COM组件调用.NET组件编程实战
- .Net服务组件(ServicedComponent)简介及其使用
- 使用C#轻松编写.Net组件(1)