-1

I'm in fairly early stages of learning c# / .NET using MonoDevelop to do my work in. I've come across some API's which were developed in c or sometimes c++. I've been looking for information to get me started on implementing APIs written in c or c++ so I can use them for my c# projects. I don't have any specific code to show you right now, what I need is to be pointed in the right direction.

Here is a sampling of places I've perused:

I am thinking about implementing a c/C++ API in C#. I have done some research, but I'm having trouble getting started.

http://www.codeproject.com/Articles/1285/Calling-API-functions-using-C (a somewhat dated article, looks doable, but doesn't seem to go very far.)

http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=219 (some basics, but not much detail about what is going on.)

found some stuff on SO: C api to C# GUI (this looks farily good)

https://stackoverflow.com/questions/9747607/c-apivs-2005-integrate-into-c-sharp-vs-2010project (no answer given)

talking to C from C# (looks good, some stuff about Pinvoke)

Anyway, I'm a little overwhelmed, and could use some guidence to get me started. Maybe this is too much for a noob to byte [sic] off?

Any help is much appreciated. CHEERS

Community
  • 1
  • 1
happy coder
  • 1,517
  • 1
  • 14
  • 29
  • For a C++ API using classes, you will need a C++/CLI (easier to use) or COM (also usable from non-.NET languages) wrapper. P/Invoke only works for simple functions. – Ben Voigt Dec 03 '12 at 12:13
  • What exactly is your question? – Security Hound Dec 03 '12 at 13:03
  • Hi Ramhound, I want to use some APIs which were written in c or c++ (either wrap them or otherwise) so that I can make use of them in my c# projects. I've tried to wade through information on how to do this, but am overwhelmed and need some help getting started. (Is that a bit more clear?) – happy coder Dec 03 '12 at 13:11

1 Answers1

3

Try SWIG - it is a tool for creating wrappers around C and C++ code for many languages.

I have used it many times with good results. Basically you just define which C and C++ code files / libraries you want to wrap and SWIG generates corresponding C# classes (and some boilerplate code) which you can simply add to your C# project (and of course you have to compile the C/C++ part also and make it a DLL).

Eg. you might have small C++ library which had classes

class Foo { public: int myMethod(Bar& bar); }
class Bar { ... }

After SWIGing, your C# code would look something like:

Bar bar = new Bar();
Foo foo = new Foo();
int result = foo(bar);
user544511
  • 759
  • 6
  • 15
  • I've heard of SWIG, your testimonial makes me think I could give it a go and see how it works. I'll check around for some tutorials. This may be a good way to go. One API is for doing sound, so it may not be trivial. Another API is probably way over the top right now ... thinking about using another GUI toolkit. – happy coder Dec 03 '12 at 13:13