sgsrules's picture

Generating C# wrapper from c++ headers.

Ok So i know this is somewhat off topic but i don't really know where else to ask and seeing how OpenTK is a wrapper and i've always gotten good feedback here i figured why not?

Anyhow i'm trying to figure out how to create a wrapper from a c++ header file. Is it possible to do this automatically? I came across SWIG and P/Invoke Interop Assistant which seem to do what i need. I'm a total N00b in this field and wanted to know if this is the preferred way of creating a wrapper. Any help, hints tutorials on the subject would be greatly appreciated.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Tal's picture

You can use DLLImport attribute.
You need first to compile the c++ headers to one file(DLL), and then use that attribute to use them in .Net.
Also, if you need to send a struct via this method to unmanaged code, you need to rebuild that struct and use the
[StructLayout(LayoutKind.Sequential)] attribute.
Here is a guide how to do that:
You may ask now why there is no unmanaged DLL file for OpenTK? The reason is that OpenTK use this attribute(I guess):
[DllImport("opengl32.dll", ...]
And in a typical Windows OS for example, this file is located in C:\WINDOWS\system32, so there is no need to rebuild it, but in your case, there is.
Hope that this is what you ment and good luck!

sgsrules's picture

Thanks for the pointers Tal that article cleared up a couple of things. But what i was wondering is if there was a way to automatically generate the wrapper from a header, it would save me alot of time in the future. I've spent the last two days creating a basic wrapper for ffmpeg and it involved a lot of copying and pasting from the headers which was a lengthy semi brainless process.

jdomnitz's picture

The P/Invoker app is supposed to do exactly that but i've yet to see it actually work...always crashes on the headers i've tried. I too would be interested if anyone knows of anything else that works.

the Fiddler's picture

Some time ago I found out about a project that meant to compile several different languages into a common xml representation. Were that possible, it should be easy (if not trivial) to transform the xml into a set of p/invokes.

Unfortunately, I cannot seem to find that project anymore. The closest thing I've found is GCC-XML which might work.