How do I Invoke Native C++ DLL from .NET Code
-
Monday, June 28, 2010 1:47 AM
How do I invoke native C++ DLL from .NET code?
All Replies
-
Monday, June 28, 2010 1:48 AM
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)
Download the All-In-One Code Framework (Library) package.
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)
Download the All-In-One Code Framework (Library) package.
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)
Download the All-In-One Code Framework (Library) package.
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)
Download the All-In-One Code Framework (COM) package.
- Marked As Answer by MSDN FAQ Monday, June 28, 2010 1:49 AM
-
Tuesday, November 15, 2011 3:59 PMFor 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.
- Edited by Brian MuthMVP Tuesday, November 15, 2011 4:31 PM
-
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,
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:
Just put an example then.
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

