Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In C# how to call a C++ dll which written by unmanaged code file function? I checked online and the use of dllimport are provided in the form of commission calls, but this function is called when the number is relatively small it is quite easy, but if the number of functions need to call more, for example, I write a C++ dll file contains there are 50 functions, and that if all the C# reference to the use of dllimport way, then the defined code will very long. While efficiency is not high, may I ask the teacher has a quickly way to achieve C++ dll file for the function call? As in C# call C# function when the library is named by reference to get a namespace and then the function can be carried out in their own use. Experts your advise! Thank you.
Posted

I can see only one way which can be safer and faster in implementation, especially if you really have many exported unmanaged function to use. Write a mixed-mode (managed+unmanages) library using C++/CLI. It can use unmanaged DLL using the exact same declarations you have in the DLL's header files. From the other hand, you can create managed types to expose imported functionality and use your mixed-mode DLL as a regular .NET assembly. You can reference it as any other assembly in your C# code.

—SA
 
Share this answer
 
Comments
CPallini 12-Oct-11 3:22am    
Yeah, I suppose it would work. +5.
Sergey Alexandrovich Kryukov 12-Oct-11 9:28am    
Usually it does :-)
Thank you.
--SA
AFIK, you should use the 'dllimport way' there is no shortcut for that.
However, if you can modify the source DLL then you may consider building a 'simplified interface to your functions' (a sort of Facade pattern[^]) or pack it as COM component.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Oct-11 19:12pm    
Well, there is one "shortcut"; it may or may not be shorter; but when there are too many functions to import, it can easily be shorter. This is using C++/CLI; please see my answer. Also, I think messing up with COM won't pay off and would make a solution more contaminated with unwanted stuff.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900