非托管c++调用托管C++写的程序集 实现非托管C++对.NET库的访问

Invoke Native C++ DLL from .NET Code


Solution 1. (Explicit) P/Invoke
Solution 2. Dynamic P/Invoke
Solution 3. Implicit P/Invoke (Use a C++/CLI wrapper)
Solution 4. Convert C++ DLL to a COM server, and call it from .NET code through .NET-COM interop


Solution 1. (Explicit) P/Invoke

Samples:
  • CppDynamicLinkLibrary (a native C++ DLL module that exports global data, functions and classes)
  • CSPInvokeDll (a C# application that P/Invokes the functions exported by CppDynamicLinkLibrary)
  • VBPInvokeDll (a VB.NET application that P/Invokes the functions exported by CppDynamicLinkLibrary)
Platform Invocation Services (P/Invoke) in .NET allows managed code to call unmanaged functions that are implemented and exported in unmanaged DLLs. The C# sample application CSPInvokeDll and the VB.NET sample application VBPInvokeDll demonstrate P/Invoking the functions exported by the native C++ DLL CppDynamicLinkLibrary.dll.

Download Download the All-In-One Code Framework (Library) package.

PInvoke.png

Solution 2. Dynamic P/Invoke

Samples:
  • CppDynamicLinkLibrary (a native C++ DLL module that exports global data, functions and classes)
  • CSLoadLibrary (a C# application that dynamically P/Invokes the functions exported by CppDynamicLinkLibrary)
  • VBLoadLibrary (a VB.NET application that dynamically P/Invokes the functions exported by CppDynamicLinkLibrary)
Dynamic P/Invoke serves as a supplement for the P/Invoke technique and is useful especially when the target DLL is not in the search path of P/Invoke. If you use P/Invoke, CLR will search the dll in your assembly's directory first, then search the dll in directories listed in PATH environment variable. If the dll is not in any of those directories, you have to use the so called Dynamic P/Invoke technique. Dynamic P/Invoke dynamically loads the native DLL by calling LoadLibrary and gets the address of the target function through GetProcAddress and Marshal.GetDelegateForFunctionPointer. CSLoadLibrary and VBLoadLibrary use the technology to dynamically load and call CppDynamicLinkLibrary.

Download Download the All-In-One Code Framework (Library) package.

DynamicPInvoke.png

Solution 3. Implicit P/Invoke (Use a C++/CLI wrapper)

Samples:
  • CppDynamicLinkLibrary (a native C++ DLL module that exports global data, functions and classes)
  • CppCLINativeDllWrapper (a C++/CLI wrapper of the native C++ DLL CppDynamicLinkLibrary)
  • CSCallNativeDllWrapper (a C# application that invokes CppDynamicLinkLibrary through CppCLINativeDllWrapper)
  • VBCallNativeDllWrapper (a VB.NET application that invokes CppDynamicLinkLibrary through CppCLINativeDllWrapper)
The interoperability features supported by Visual C++/CLI offer a particular advantage over other .NET languages when it comes to interoperating with native modules. Apart from the traditional explicit P/Invoke, C++/CLI allows implicit P/Invoke, also known as C++ Interop, or It Just Work (IJW), which mixes managed code and native code almost invisibly. The feature provides better type safety, easier coding, greater performance, and is more forgiving if the native API is modified. You can use the technology to build .NET wrappers for native C++ classes and functions if their source code is available, and allow any .NET clients to access the native C++ classes and functions through the wrappers.

Download Download the All-In-One Code Framework (Library) package.

CLIWrapper.png

Solution 4. Convert C++ DLL to a COM server, and call it from .NET code through .NET-COM interop

Samples:
  • ATLDllCOMServer (a native C++ DLL converted to an in-process COM server)
  • CSCOMClient (a C# application that invokes the C++ in-process COM server ATLDllCOMServer)
  • VBCOMClient (a VB.NET application that invokes the C++ in-process COM server ATLDllCOMServer)
The .NET sample applications CSCOMClient and VBCOMClient invoke the in-process COM server ATLDllCOMServer that was converted from a native C++ DLL.

Download Download the All-In-One Code Framework (COM) package.

COMInterop.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值