How do I Invoke Native C++ DLL from .NET Code

Answered How do I Invoke Native C++ DLL from .NET Code

All Replies

  • Monday, June 28, 2010 1:48 AM
     
     Answered

    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
    • Marked As Answer by MSDN FAQ Monday, June 28, 2010 1:49 AM
    •  
  • Tuesday, November 15, 2011 3:59 PM
     
     
    For some reason MS didn't find usefull to include an example of how to invoke windows DLL (example avifil32.dll) from a managed C++ project in .NET framework.
  • Tuesday, November 15, 2011 4:31 PM
     
     

    I don't see why you would need an example. Managed C++ can call a native DLL directly. Nothing fancy is needed.

    Incidentally you should start a new thread for current discussions such as this, rather than dredging up an old thread.


  • Tuesday, November 15, 2011 4:39 PM
     
     

    Well genious,

    Just put an example then.

  • Monday, April 23, 2012 9:42 AM
     
     

    Example could be:

    [DllImport("YourCPPDll.dll")]

    private static extern void Foo(ref float value, string data);

    Viral.


    MCTS - WPF, WinForms, Sql Server 2008

  • Monday, April 23, 2012 11:49 AM
     
     
    Well genious,
     
    Just put an example then.
    As Brian says, the great advantage of C++/CLI over C# is that it can call functions in native DLL's without using P/Invoke. It's called It Just Works (IJW). The procedure is the same as uisng a DLL in a native application:
     
    1. #include necesary header (.h)
    2. Link to necessary import library (.lib)
    3. Call the function in your code.
     
    What function in avifil32.dll do you want to call? Look it up in the documentation to find out what you need for items 1 and 2.
     

    David Wilkinson | Visual C++ MVP