0

I've developed an application in C# that handles TCP/IP connections and after running some diagnostics, I concluded that compiling and runtime can be improved. I decided to rewrite the entire program in C++ since it also runs smoother in a Lunix envrionment.

Is there a way I can use C# libraries like Sockets, Net, and Net.IPAddress in C++?.

I did some research on this and found out I can call these libraries in C++ through: #using (shevron)System.dll> But I'm hesistant if it will cause overhead and problems in a Lunix envrionment.

  • 2
    If you are concerned about your code's portability (to Linux) and it's performance, you need to rewrite your code in C++. Using same old C# code with some additional libraries in C++ will not help. Also just writing your code in C++ will not automatically make it cross-platform – 72DFBF5B A0DF5BE9 Oct 23 '15 at 15:20
  • 2
    "Lunix"? Do you mean "Linux"? Microsoft's C++ language extension C++/CLI is deisgned for .Net interoperability, but pretty sure this does not work on mono [Does Mono .NET support and compile C++ / CLI?](http://stackoverflow.com/questions/183377/does-mono-net-support-and-compile-c-cli) – crashmstr Oct 23 '15 at 15:20
  • 3
    Possible duplicate of [Using C# dll in C++ code](http://stackoverflow.com/questions/19144006/using-c-sharp-dll-in-c-code) – Bob Oct 23 '15 at 15:24
  • 2
    You don't have to worry about "overhead and problems", your program will simply never run on Linux or any other Unix flavor. This flavor of C++ interop is only available on Windows. – Hans Passant Oct 23 '15 at 15:24
  • @HansPassant I actually compiled the C# using mono and ran it fine on Linux.. It's just performance and portability issue I wanted to resolve. – Caleb Eom Oct 23 '15 at 15:32
  • @crashmstr It actually worked using mono. – Caleb Eom Oct 23 '15 at 15:35
  • Hmya, your question is about C++, not C#. And you actually meant C++/CLI. Getting languages mixed up like that isn't a great way to get to a "no problems" solution, it matters. – Hans Passant Oct 23 '15 at 15:35
  • @CalebEom the question I linked does talk about this, but the problem is when you use anyhing *other than* .Net libraries, which is generally the whole point of C++/CLI - to interoperate between C++ and .Net. If you just want to use .Net, then C# is cleaner and simpler. – crashmstr Oct 23 '15 at 15:37
  • @HansPassant Where does the OP say anything about C++/CLI? Using a C# library from C or C++, i.e. native code, should work both in Windows and Linux, cf. for Mono http://www.mono-project.com/docs/advanced/embedding/. That won't be simple for less than trivial cases but sounds by no means unreasonable or impossible. – Peter - Reinstate Monica Oct 23 '15 at 15:40
  • 2
    The #using directive is a C++/CLI feature. – Hans Passant Oct 23 '15 at 15:41
  • @HansPassant So there isn't an overhead in the #using directive that uses a C# dll library? – Caleb Eom Oct 23 '15 at 15:53
  • 1
    That is not a meaningful question, only the compiler cares about that directive. It is the exact equivalent of #include but for managed metadata. – Hans Passant Oct 23 '15 at 16:21

1 Answers1

1

As I said in my comment, there is Mono documentation about using C# libraries from native code, cf. the link in the comment, http://www.mono-project.com/docs/advanced/embedding/. The native program basically embeds the Mono runtime for that.

Hans Passant made the valid objection that the #using directive in your C++ code indicates that your "C++" code is actually the Microsoft proprietary C++/CLI which will not compile under Linux. How much work it would be to port that depends strongly on how many non-C++ features you used. The interop with Mono will also be more complicated than calling the C# functions in the Microsoft ecosystem.

If you look at performance my gut feeling is that the performance of network-heavy code will be dominated by the networking. Wasting a few CPU cycles should not be an issue, unless you are targeting an embedded system. On a PC, running in Mono or native may not make much of a difference. But this also depends a lot on the actual application (more communication or more computation?).

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62