如何:从 COM 中引用 .NET 类型How to: Reference .NET Types from COM

从客户端和服务器代码的角度看,COM 和 .NET Framework 之间的区别在很大程度上是不可见的。From the point of view of client and server code, the differences between COM and the .NET Framework are largely invisible. Microsoft Visual Basic 客户端可在对象浏览器中查看 .NET 对象,这将公开对象方法和语法、属性和字段,正如任何其他 COM 对象那样。Microsoft Visual Basic clients can view a .NET object in the object browser, which exposes the object methods and syntax, properties, and fields exactly as if it were any other COM object.

尽管使用相同的工具将元数据导出到 COM 类型库,导入类型库的过程对于 C++ 客户端来说稍微复杂一些。The process for importing a type library is slightly more complicated for C++ clients, although you use the same tools to export metadata to a COM type library. 要从非托管 C++ 客户端引用 .NET 对象成员,通过“#import”指令引用 TLB 文件(使用 Tlbexp.exe 生成)。To reference .NET object members from an unmanaged C++ client, reference the TLB file (produced with Tlbexp.exe) with the #import directive. 从 C++ 引用类型库时,必须指定“raw_interfaces_only”选项或在基类库 Mscorlib.tlb 中导入定义。When referencing a type library from C++, you must either specify the raw_interfaces_only option or import the definitions in the base class library, Mscorlib.tlb.

导入库To import a library

  • 在“#import”指令中指定“raw_interfaces_only”选项。Specify the raw_interfaces_only option in the #import directive. 例如:For example:

    #import "..\LoanLib\LoanLib.tlb" raw_interfaces_only  
    

    -or-

  • 包括用于 Mscorlib.tlb 的 #import 指令。Include an #import directive for Mscorlib.tlb. 例如:For example:

    #import "mscorlib.tlb"  
    #import "..\LoanLib\LoanLib.tlb"  
    

请参阅See Also

向 COM 公开 .NET Framework 组件Exposing .NET Framework Components to COM
向 COM 注册程序集Registering Assemblies with COM
调用 .NET 对象Calling a .NET Object
为 COM 访问部署应用程序Deploying an Application for COM Access