互通性概觀 (C# 程式設計手冊)Interoperability Overview (C# Programming Guide)

本主題說明可在 C# Managed 程式碼和 Unmanaged 程式碼之間啟用互通性的方法。The topic describes methods to enable interoperability between C# managed code and unmanaged code.

平台叫用Platform Invoke

「平台叫用」服務,可讓 Managed 程式碼呼叫 Unmanaged 函式在動態連結程式庫 (DLL) 中實作,例如 Win32 API 中。Platform invoke is a service that enables managed code to call unmanaged functions that are implemented in dynamic link libraries (DLLs), such as those in the Microsoft Win32 API. 它會找出並叫用匯出的函式,並且在需要的時候於交互操作界限之間封送處理其引數 (整數、 字串、 陣列、 結構和其他) 。It locates and invokes an exported function and marshals its arguments (integers, strings, arrays, structures, and so on) across the interoperation boundary as needed.

如需詳細資訊,請參閱使用 Unmanaged DLL 函式如何:使用平台叫用播放 WAV 檔For more information, see Consuming Unmanaged DLL Functions and How to: Use Platform Invoke to Play a Wave File.

注意

Common Language Runtime (CLR) 管理對系統資源的存取。The Common Language Runtime (CLR) manages access to system resources. 在 CLR 外部呼叫 Unmanaged 程式碼會略過此安全性機制,因而造成安全性風險。Calling unmanaged code that is outside the CLR bypasses this security mechanism, and therefore presents a security risk. 例如,Unmanaged 程式碼可能會直接呼叫 Unmanaged 程式碼中的資源,並略過 CLR 安全性機制。For example, unmanaged code might call resources in unmanaged code directly, bypassing CLR security mechanisms. 如需詳細資訊,請參閱 .NET 的安全性For more information, see Security in .NET.

C++ InteropC++ Interop

您可以使用 C++ Interop (也稱為 It Just Works (IJW)) 包裝原生 C++ 類別,以供使用 C# 或其他 .NET Framework 語言撰寫的程式碼取用。You can use C++ interop, also known as It Just Works (IJW), to wrap a native C++ class so that it can be consumed by code that is authored in C# or another .NET Framework language. 若要這樣做,您可以撰寫 C++ 程式碼來包裝原生 DLL 或 COM 元件。To do this, you write C++ code to wrap a native DLL or COM component. 不同於其他 .NET Framework 語言,Visual C++Visual C++ 提供互通性支援,因此可將 Managed 和 Unmanaged 程式碼放置在相同的應用程式,甚至是相同的檔案中。Unlike other .NET Framework languages, Visual C++Visual C++ has interoperability support that enables managed and unmanaged code to be located in the same application and even in the same file. 您接著可使用 /clr 編譯器參數建立 C++ 程式碼,以產生 Managed 組件。You then build the C++ code by using the /clr compiler switch to produce a managed assembly. 最後,您可以在 C# 專案中新增組件的參考,並使用包裝的物件,就像是使用其他 Managed 類別一樣。Finally, you add a reference to the assembly in your C# project and use the wrapped objects just as you would use other managed classes.

將 COM 元件公開給 C#Exposing COM Components to C#

您可以從 C# 專案取用 COM 元件。You can consume a COM component from a C# project. 一般步驟如下所示:The general steps are as follows:

  1. 找出並註冊所要使用的 COM 元件。Locate a COM component to use and register it. 使用 regsvr32.exe 註冊或取消註冊 COM DLL。Use regsvr32.exe to register or un–register a COM DLL.

  2. 將 COM 元件或型別程式庫的參考新增至專案。Add to the project a reference to the COM component or type library.

    當您新增參考時,Visual Studio 會使用接受型別程式庫作為輸入的 Tlbimp.exe (型別程式庫匯入工具),以輸出 .NET Framework Interop 組件。When you add the reference, Visual Studio uses the Tlbimp.exe (Type Library Importer), which takes a type library as input, to output a .NET Framework interop assembly. 此組件 (也稱為執行階段可呼叫包裝函式 (RCW)) 包含 Managed 類別和介面,以包裝型別程式庫中的 COM 類別和介面。The assembly, also named a runtime callable wrapper (RCW), contains managed classes and interfaces that wrap the COM classes and interfaces that are in the type library. Visual Studio 會將產生的組件參考新增至專案。Visual Studio adds to the project a reference to the generated assembly.

  3. 建立定義於 RCW 之類別的執行個體。Create an instance of a class that is defined in the RCW. 這會接著建立 COM 物件的執行個體。This, in turn, creates an instance of the COM object.

  4. 就像是使用其他 Managed 物件一樣來使用此物件。Use the object just as you use other managed objects. 當記憶體回收將物件回收時,也會從記憶體釋放 COM 物件的執行個體。When the object is reclaimed by garbage collection, the instance of the COM object is also released from memory.

如需詳細資訊,請參閱將 COM 元件公開給 .NET FrameworkFor more information, see Exposing COM Components to the .NET Framework.

將 C# 公開給 COMExposing C# to COM

COM 用戶端可取用已正確公開的 C# 類型。COM clients can consume C# types that have been correctly exposed. 公開 C# 類型的基本步驟如下所示:The basic steps to expose C# types are as follows:

  1. 在 C# 專案中新增 Interop 屬性。Add interop attributes in the C# project.

    您可以藉由修改 Visual C# 專案屬性來顯示組件 COM。You can make an assembly COM visible by modifying Visual C# project properties. 如需詳細資訊,請參閱組件資訊對話方塊For more information, see Assembly Information Dialog Box.

  2. 產生 COM 型別程式庫並註冊供 COM 使用。Generate a COM type library and register it for COM usage.

    您可以修改 Visual C# 專案屬性,以自動為 COM Interop 註冊 C# 組件。You can modify Visual C# project properties to automatically register the C# assembly for COM interop. Visual Studio 使用 Regasm.exe (組件登錄工具),並使用接受受控組件作為輸入的 /tlb 命令列參數,以產生型別程式庫。Visual Studio uses the Regasm.exe (Assembly Registration Tool), using the /tlb command-line switch, which takes a managed assembly as input, to generate a type library. 此型別程式庫描述組件中的 public 類型,並新增登錄項目,讓 COM 用戶端可以建立 Managed 類別。This type library describes the public types in the assembly and adds registry entries so that COM clients can create managed classes.

如需詳細資訊,請參閱將 .NET Framework 元件公開給 COM範例 COM 類別For more information, see Exposing .NET Framework Components to COM and Example COM Class.

請參閱See Also