Adventure Game Studio

Community => General Discussion => Topic started by: RickJ on Mon 17/04/2017 19:30:19

Title: C# wrapper for C++ API
Post by: RickJ on Mon 17/04/2017 19:30:19
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.
Title: Re: C# wrapper for C++ API
Post by: Snarky on Mon 17/04/2017 19:48:10
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.
Title: Re: C# wrapper for C++ API
Post by: RickJ on Tue 18/04/2017 06:14:12
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.
Title: Re: C# wrapper for C++ API
Post by: Snarky on Tue 18/04/2017 08:06:36
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.
Title: Re: C# wrapper for C++ API
Post by: Crimson Wizard on Tue 18/04/2017 19:18:03
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.
Title: Re: C# wrapper for C++ API
Post by: RickJ on Tue 18/04/2017 22:42:42
Thanks for everything guys.  The API is for HEBI Robotics (http://hebirobotics.com) 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. (http://beijerinc.com/)  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.
Title: Re: C# wrapper for C++ API
Post by: Adeel on Tue 18/04/2017 22:47:27
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 (https://www.amazon.com/C-4-0-How-Ben-Watson/dp/0672330636) these days. Check it out. Portions of it might be useful for your particular needs.
Title: Re: C# wrapper for C++ API
Post by: RickJ on Wed 19/04/2017 14:55:15
Thanks Adeel for the recommendation.  I know there is a lot of material out there and the problem is finding yhe right ones. 

Thanks.
Title: Re: C# wrapper for C++ API
Post by: Adeel on Wed 19/04/2017 23:07:16
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