C# wrapper for C++ API

Started by RickJ, Mon 17/04/2017 19:30:19

Previous topic - Next topic

RickJ

For a non-AGS application what are the ins and outs of using a third party C++ API with C#.  How easy or difficult would you expect the task to be?  What kind of pitfalls might there be?  The C++ API would be doing math and communication through an ethernet port.  The C# application would likely be a periodically scheduled task.  Any insights or commentary would be greatly appreciated.

Snarky

I take it you're not thinking of using the Ethernet port to communicate between the components of the application?

It's been probably a decade since I did this (actually I think the first program I had to write in C# back in 2002 involved something similar), but I remember it being pretty easy to bridge C# and native code. Basically you build the C++ project as a dll, then you write some C# function stubs that match the API of the functions you want to call, and add some kind of tag (extern, I think) to let the program know the implementation is defined elsewhere.

It might take some trial and error to get everything set up correctly, but after that it's pretty trivial.

RickJ

Thank you very much Snarky.  That's what I wanted to know.  I have since found out that there is also a C# API currently in beta testing so that is likely the way I will go with it.

Thanks for the help.

Snarky

Yeah... it's starting to come back to me. Calling functions isn't particularly hard. What can be tricky is managing memory and converting the data to the right formats ("marshaling") when they're not primitive types (if you've ever written Spring code to convert between a class and database representation of complex data, it's a bit like that). An API to handle all of that could definitely be worthwhile.

Crimson Wizard

#4
By the way , AGSEditor still has C++ DLL, you may check out how C# code interacts with it.

There is a C# class called NativeProxy:
https://github.com/adventuregamestudio/ags/blob/master/Editor/AGS.Editor/NativeProxy.cs

And C++ class called NativeMethods:
https://github.com/adventuregamestudio/ags/blob/master/Editor/AGS.Native/NativeMethods.h

They serve as an API bridge between both parts.

RickJ

Thanks for everything guys.  The API is for HEBI Robotics which can be seen in action in the video below. 

https://youtu.be/kC5sjQXJCxg

I plan on hosting the robot api on a Beijer touch panel.  I have since found out that HEBI has a C# version of it's API in beta test.  The Beijer touch panels use WIN7 and are programmed with their own HMI software (sort of like AGS) which is scriptable via C#.

So now all I need to do is learn C#.  I'm open to suggestions on where to start ... books, links, videos, etc.

Adeel

#6
Quote from: RickJ on Tue 18/04/2017 22:42:42
So now all I need to do is learn C#.  I'm open to suggestions on where to start ... books, links, videos, etc.

Try this one for starters: https://docs.microsoft.com/en-us/dotnet/articles/csharp/tour-of-csharp/

E: I'm reading this one these days. Check it out. Portions of it might be useful for your particular needs.

RickJ

Thanks Adeel for the recommendation.  I know there is a lot of material out there and the problem is finding yhe right ones. 

Thanks.

Adeel

Another good place to start is the Microsoft Virtual Academy.

If you're interested, check out this free C# Programming Tutorial: https://mva.microsoft.com/en-US/training-courses/programming-in-c-jump-start-14254

SMF spam blocked by CleanTalk