0

The program developed by C# was to get realtime data from server. The project I am working on is to do machine learning/signal processing on those data in realtime and finally give a decision.

I come up with following approach. Develop machine learning based algorithm with C/C++ and make it into DLL. The reason to choose C/C++ is just due to there are many toolbox/libraries for signal processing, machine learning and so on. The C# program will call DLL and feed raw data into DLL and finally get output from DLL.

my questions is: 1. is it easy and possible to call DLL written in C/C++ by a program written in C#? 2. can everything be done in C# easily? 3. is there other way to walk around or other software architecture (C# program is already well developed)?

Thanks!

  • Last week it was the DLL written in C# & the application in C++ - is there a reason to the change in approach. The answers to all three of your questions can be very easily found online BTW (simple answer to all three is YES). – PaulF Feb 06 '20 at 15:38
  • First off, C# is not the ideal tool for Realtime Programming. C# is executed by the .NET Runtime, wich uses a GarbageCollection memory management approach. And GC and RealTime do not mix: https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Disadvantages – Christopher Feb 06 '20 at 15:39
  • Look up `P/Invoke`. It's the method/technology used for managed code calling exported C endpoints. You may want to get an understanding of what the best call signatures should be as you design this – Flydog57 Feb 06 '20 at 15:39
  • The usual trinity would be: 1) It is C++ .NET DLL? Just call it like you do any other .NET DLL. 2) COM Interop. 3) PInvoke and the like | But again, if you need realtime performance .NET is a no go area most of the time. – Christopher Feb 06 '20 at 15:40
  • You can write _**soft** real time_ systems using .NET, but you need to design it properly up front. Pre-allocate and reuse buffers to prevent spurious garbage, manage multiple thread pools, suppress GC operations at key times, etc. – Flydog57 Feb 06 '20 at 15:47
  • Yes it's possible; you have a choice between a C native interface (see dupe), or a [COM](https://learn.microsoft.com/en-us/cpp/ide/creating-a-com-interface-visual-cpp?view=vs-2019) interface (more work but is more object-oriented). Don't expose C++ classes directly via a DLL, that will be [very](https://stackoverflow.com/questions/22797418/how-do-i-safely-pass-objects-especially-stl-objects-to-and-from-a-dll) problematic. – rustyx Feb 06 '20 at 15:51
  • 1
    @Christopher While this surely applies to embedded stuff and traditional industry, the approach of using compiled code for components that need maximum speed and use the comfort of an interpreted language for the "business logic" is quite common. Many game engines for example use that concept. Unity even allows the user to add c++ "plugins" (DLLs on windows) themselves. The whole language of python is sorta based on that because math libraries etc. are using native code. – AlexGeorg Feb 06 '20 at 15:56
  • @AlexGeorg That only works if you really know what you are doing and the Native Code in question was designed for that. Not something I could asume for this case. Still, it is nice to know. – Christopher Feb 06 '20 at 16:00
  • Thanks for all inputs! My plan would be C# call functions developed by C++. For some machine learning, My C++ program might integrate Python script. – victoria G. Feb 07 '20 at 08:55
  • The approach mentioned above might take advantage of C/C++ realtime performance and comfort of data science with python, and at the same time working with developed C# program. – victoria G. Feb 07 '20 at 09:07

0 Answers0