Answered by:
Calling C++ DLL in C#

Question
-
Hello,
I am having C++ application with multiple functions (No Class ) in it.
I built this application as a DLL.
Also I am having a c# application in which I want to call 2-3 functions from built C++ DLL.
I am using following code to call a C++ function from C#;
using System.Runtime.InteropServices;
[DLLImport dllName]
public static extern function1(param1, param2, ...);
[DLLImport dllName]
public static extern function2(param11, param22, ...);
So, Is there any other way to do this instead of doing DLLImport for multiple times?
Is it possible to register a C++ DLL and use it in C# application without using DLLImport?
Thanks!!!
Thursday, January 13, 2011 9:05 AM
Answers
-
The DLL Import is the normal way to just import single functions of a DLL created with c++.
INstead of offering simple single functions, a c++ DLL could also be a COM Type Library. In such a case, you could use tlbimp.exe (Type Library Importer, http://msdn.microsoft.com/en-us/library/tt0cf3sx(v=VS.100).aspx) to import it for use in managed applications.
With kind regards,
Konrad
- Proposed as answer by Leo Liu - MSFTModerator Monday, January 17, 2011 7:44 AM
- Marked as answer by Leo Liu - MSFTModerator Thursday, January 20, 2011 1:49 AM
Thursday, January 13, 2011 9:19 AM -
Check this:
http://msdn.microsoft.com/en-us/library/aa302324.aspx
Hope this helps.
Please Mark as answer if it helped you.
Santosh.- Proposed as answer by Leo Liu - MSFTModerator Monday, January 17, 2011 7:49 AM
- Marked as answer by Leo Liu - MSFTModerator Thursday, January 20, 2011 1:49 AM
Thursday, January 13, 2011 10:48 AM -
If the C++-code is yours then you could also write a C++/CLI wrapper around it, that
exposes a managed version of the unmanaged interface.
Then in C# you would simply need to reference the managed C++-DLL.Chris
- Proposed as answer by Leo Liu - MSFTModerator Monday, January 17, 2011 7:49 AM
- Marked as answer by Leo Liu - MSFTModerator Thursday, January 20, 2011 1:49 AM
Thursday, January 13, 2011 11:27 AM
All replies
-
The DLL Import is the normal way to just import single functions of a DLL created with c++.
INstead of offering simple single functions, a c++ DLL could also be a COM Type Library. In such a case, you could use tlbimp.exe (Type Library Importer, http://msdn.microsoft.com/en-us/library/tt0cf3sx(v=VS.100).aspx) to import it for use in managed applications.
With kind regards,
Konrad
- Proposed as answer by Leo Liu - MSFTModerator Monday, January 17, 2011 7:44 AM
- Marked as answer by Leo Liu - MSFTModerator Thursday, January 20, 2011 1:49 AM
Thursday, January 13, 2011 9:19 AM -
Check this:
http://msdn.microsoft.com/en-us/library/aa302324.aspx
Hope this helps.
Please Mark as answer if it helped you.
Santosh.- Proposed as answer by Leo Liu - MSFTModerator Monday, January 17, 2011 7:49 AM
- Marked as answer by Leo Liu - MSFTModerator Thursday, January 20, 2011 1:49 AM
Thursday, January 13, 2011 10:48 AM -
If the C++-code is yours then you could also write a C++/CLI wrapper around it, that
exposes a managed version of the unmanaged interface.
Then in C# you would simply need to reference the managed C++-DLL.Chris
- Proposed as answer by Leo Liu - MSFTModerator Monday, January 17, 2011 7:49 AM
- Marked as answer by Leo Liu - MSFTModerator Thursday, January 20, 2011 1:49 AM
Thursday, January 13, 2011 11:27 AM