I want to write an algorithmic library in standard, platform-independent C++.
Can I then take the same C++ code and compile it for .NET so that a C# application can use the library?
If not, what is the easiest way to achieve this kind of interop?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have three ways to expose your library to easily be called from managed code:
public ref class– the C# code will call methods of this class, which will call native methods of your libraryThat third one, COM, I include only for completeness. It wouldn’t be my first choice. Or my third. It’s one thing if the COM component exists, it’s another to write it just for this.
Between the first two it’s a matter of who else might use this library. If the C# code is the only one using it, do what you like. But for example you might have 15 or 20 native methods that set various properties, followed by a
goorexecutemethod that does the actual work. Because managed-native interop has a cost, I would tell you to write one C++/CLI function that takes 20 parameters, calls the various native functions and then calls thegoorexecute. This gives you a chunkier interface instead of chatty and reduces your interop costs.