.Net调用Com组件主要分为两类:静态调用及动态调用。
所谓静态调用:指通过通过tlbimp.exe命名产生Com组件在.Net环境下的包装类,然后通过这个包装类来访问Com组件。
所谓动态调用:是指不通过Com组件的包装类来进行Com组件调用,而是在远行时通过反射来进行Com组件调用。
下面将分别详细的讨论这两种实现方法:
<!--[if !supportLists]-->1、 <!--[endif]-->编写Com组件MyComponent.dll
<!--[if !supportLists]-->2、 <!--[endif]-->产生可访问Com组件的包装类:
tlbimp /out:Interop.MyComponent.dll MyComponent.dll
<!--[if !supportLists]-->3、 <!--[endif]-->在.Net代码中访问
只要在项目添加Com包装类,就可以像访问.Net的装配件一样访问Com组件。
当然上面的三个步骤也可以在VS中直接添加对Com组件的引用,让VS自动完成第二步。
注意:
<!--[if !supportLists]-->1、 <!--[endif]-->如果引用Com组件的.Net项目为结果为强命名组件,这个时候要求Com组件的包装类也必须为强命名组件,所以这个时候必须手动进行第二步操作,并且在产生包装类的时候设置snk文件名称,具体操作方法为:
tblimp /keyfile:filename.snk /out:Interop.MyComponent.dll MyComponent.dll
<!--[if !supportLists]-->2、 <!--[endif]-->要想有更多的设置请参考tlbimp的使用方法。
<!--[if !supportLists]-->1、 <!--[endif]-->编写Com组件MyComponent.dll。
<!--[if !supportLists]-->2、 <!--[endif]-->在.Net程序中产生要被调用的Com组件类的Type:
Type comType = Type.GetTypeFromCLSID( Guid ); 或
Type comType = Type.GetTypeFromProgID( string );
<!--[if !supportLists]-->3、 <!--[endif]-->生成Com组件类对象
object comObj = Activator.CreateInstance( comType ); 或
object comObj = comType.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance,
null, null, args );
<!--[if !supportLists]-->4、 <!--[endif]-->设置参数及其对应的ByRef属性。
object[] args = new object[]{arg1, arg2 , …,argn};
ParameterModifier[] modifiers = new ParameterModifier[1];
modifiers[0] = new ParameterModifier( argNumCount );
//设置参数是否为ByRef
modifiers[0][0] = true;//表示该参数是ByRef(InOut/Out)
modifiers[0][n] = false;//表示该参数是ByValue(In)。
<!--[if !supportLists]-->5、 <!--[endif]-->调用Com组件方法或者属性。
object returnValue = comType.InvokeMember(
“MethodName” ,
BindingFlags.InvokeMethod|… ,
null ,
comObj ,
args ,
modifiers ,
null );
注意:
<!--[if !supportLists]-->1、 <!--[endif]-->调用Com组件方法的时候,不能够采用MethodInfo的方式进行调用,由于没有Com组件的源数据信息,不能通过Type.GetMethod等方法够获取与Com组件相关的任何特有类型信息。
<!--[if !supportLists]-->2、 <!--[endif]-->动态调用Com组件方法中,如果需要参数回传值,则必须将该参数对应的ParameterModifier表示设置为True,即使该参数类型为传址类型。(在.net中如果参数为传址类类型,那么参数默认行为是ByRef的)。
<!--[if !supportLists]-->3、 <!--[endif]-->如果传递给Com组件的参数设置为传引用的(ParameterModifier[n] = true)的时候,该参数的值不能够为空(Null),否则会产生一个TargetInvocationException异常。解决的方法为,如果参数为基本类型(传值)则产生一个默认的值,如果参数为非基本类型(传址)则使用new UnknownWrapper(null)进行替代该参数。
具体步骤演示如下:
<!--[if !supportLists]-->1、 <!--[endif]-->定义接口:
Com是通过抛出接口让外部应用调用的,每个接口、组件都有一个Guid,在.Net中开发Com组件也不例外。
[Guid(“0bb13346-7e9d-4aba-9ff2-862e
public interface IMyService
{
//定义接口方法或者属性
//每个方法都有一个DISPID属性,供VBScript等使用
[DispId( 1 )]
void Method1( args … );
}
<!--[if !supportLists]-->2、 <!--[endif]-->实现接口的派生类
//guid属性定义组件的Guid
//ProgID定义Com组件的ProgID
[Guid( "ba
ProgId("ClassNamespace.MyService"),
ClassInterface( ClassInterfaceType.None)]
public class MyService : IMyService
{
…… //实现代码部分
}
<!--[if !supportLists]-->3、 <!--[endif]-->将.Net装配件转换为公有装配件。
(1)、创建强名字
sn –k MyService.snk
并将强名字拷贝到工程目录下面
在AssembyInfo.cs修改对应的配置
[assembly: AssemblyKeyFile(@“../../MyService.snk”)]
(2)、注册装配件
RegAsm /codebase MyService.dll
注意:/codebase是指设置注册表中的基本代码,也就是记录下dll的路径,供ccw在Com客户端在任何位置都可以找到该dll,如果没有/codebase属性,则ccw默认为dll与Com客户端执行程序的当前目录下面。
(3)、将装配件加入GAC
gacutil -i MyService.dll
注意:该步骤不是必须的。
(4)、导出类型库
为了在VB中使用,必须使用tlbexp.exe导出Com类型库。
tlbexp /out:MyService.tlb MyService.dll
经过上面的工作,就将一个.NET装配件转化为COM组件。
注意:
在Com中调用的.Net对象必须具有下面的特性:
<!--[if !supportLists]-->l <!--[endif]-->类必须是public性质;
<!--[if !supportLists]-->l <!--[endif]-->特性、方法和事件必须是public性质的;
<!--[if !supportLists]-->l <!--[endif]-->特性和方法必须在类接口中定义;
<!--[if !supportLists]-->l <!--[endif]-->事件必须在事件接口中定义。