与非托管代码进行交互操作Interoperating with unmanaged code

.NET Framework 提升与 COM 组件、COM+ 服务、外部类型库和许多操作系统服务的交互。The .NET Framework promotes interaction with COM components, COM+ services, external type libraries, and many operating system services. 托管和非托管对象模型之间的数据类型、方法签名和错误处理机制有所不同。Data types, method signatures, and error-handling mechanisms vary between managed and unmanaged object models. 要简化 .NET Framework 组件和非托管代码之间的互操作并简化迁移路径,公共语言运行时需对客户端和服务器隐藏这些对象模型中的差异。To simplify interoperation between .NET Framework components and unmanaged code and to ease the migration path, the common language runtime conceals from both clients and servers the differences in these object models.

在运行时控制下执行的代码称为托管代码。Code that executes under the control of the runtime is called managed code. 反之,在运行时以外运行的代码称为非托管代码。Conversely, code that runs outside the runtime is called unmanaged code. COM 组件、ActiveX 接口和 Windows API 函数都是非托管代码的示例。COM components, ActiveX interfaces, and Windows API functions are examples of unmanaged code.

本节内容In this section

向 .NET Framework 公开 COM 组件Exposing COM Components to the .NET Framework
描述如何使用 .NET Framework 应用程序的 COM 组件。Describes how to use COM components from .NET Framework applications.

向 COM 公开 .NET Framework 组件Exposing .NET Framework Components to COM
描述如何使用 COM 应用程序的 .NET Framework 组件。Describes how to use .NET Framework components from COM applications.

使用非托管 DLL 函数Consuming Unmanaged DLL Functions
描述如何使用平台调用调用非托管 DLL 函数。Describes how to call unmanaged DLL functions using platform invoke.

互操作封送处理Interop Marshaling
描述 COM 互操作和平台调用的封送处理。Describes marshaling for COM interop and platform invoke.

如何:映射 HRESULT 和异常How to: Map HRESULTs and Exceptions
描述异常和 HRESULT 之间的映射。Describes the mapping between exceptions and HRESULTs.

类型等效性和嵌入的互操作类型Type Equivalence and Embedded Interop Types
介绍如何在程序集中嵌入 COM 类型的类型信息,以及公共语言运行时如何确定嵌入的 COM 类型的等效性。Describes how type information for COM types is embedded in assemblies, and how the common language runtime determines the equivalence of embedded COM types.

如何:使用 Tlbimp.exe 生成主互操作程序集How to: Generate Primary Interop Assemblies Using Tlbimp.exe
介绍如何使用 Tlbimp.exe (类型库导入程序)生成主要互操作程序集。Describes how to produce primary interop assemblies using Tlbimp.exe (Type Library Importer).

如何:注册主互操作程序集How to: Register Primary Interop Assemblies
介绍如何注册主互操作程序集,然后才能在项目中引用它们。Describes how to register the primary interop assemblies before you can reference them in your projects.

免注册 COM 互操作Registration-Free COM Interop
介绍 COM 互操作如何在不使用 Windows 注册表的情况下激活组件。Describes how COM interop can activate components without using the Windows registry.

如何:配置基于 .NET Framework 的 COM 组件以进行免注册激活How to: Configure .NET Framework-Based COM Components for Registration-Free Activation
介绍如何创建应用程序清单以及如何创建并嵌入组件清单。Describes how to create an application manifest and how to create and embed a component manifest.

COM 包装COM Wrappers
介绍 COM 互操作提供的包装器。Describes the wrappers provided by COM interop.