Sign in to follow this  
Dirk Gregorius

[.net] C# GUI for native C++ static library

Recommended Posts

I want to write a GUI frontend for a native C++ library. In my particular case this is some kind of editor for my physic engine. What is the best way of getting a native C++ static library into a C#/.NET application? I did some googling and one suggestion is to wrap the native C++ library into managed C++ and then to import this into the C# application. Is this really the best way to do it? Is it actually better to use maybe managed C++/.NET for the frontend instead of C#? Any suggestions and references are appreciated.

Share this post


Link to post
Share on other sites
It depends on how large your C++ project is, or at least how large the point of contact must be. If you can create an interface for your C++ project that boils down to a small set of functions, then I would recommend using P/Invoke from C# in order to import the functions necessary. If this is the case, Googling P/Invoke should give you a ton of information on the subject.

If, on the other hand, you find yourself with a ton of contact points between your C++ and C# applications, then you will want to look into using C++/CLI as a glue layer between them. C++/CLI is NOT a good language for writing your whole front end in. However, it IS very good at acting as glue logic between a C++ library and a C# application. If you decide to go this route, take a look at my C++/CLI tips page. If you need examples of C++/CLI in use, our project SlimDX makes use of it extensively.

I would think you could stick with the first option. Usually, the second option is only necessary if you will be exposing an entire library. In your case, I think the subset of the library necessary to write the editor can be simplified down to where it will be manageable to P/Invoke.

Share this post


Link to post
Share on other sites
Using P/Invoke would require to write a thin C wrapper for the contact points with the GUI front end and export it in a DLL. Then I can use this directly in using P/Invoke. Is this correct?

Where would I need to look in SlimDX for a simple example of gluing C++ and managed C++?


Thanks!
-Dirk

Share this post


Link to post
Share on other sites
Quote:
Original post by DonDickieD
Using P/Invoke would require to write a thin C wrapper for the contact points with the GUI front end and export it in a DLL. Then I can use this directly in using P/Invoke. Is this correct?

Yes, that is correct.

Quote:
Where would I need to look in SlimDX for a simple example of gluing C++ and managed C++?

Unfortunately, creating glue between the two is not simple, so you won't really be able to find a simple example. For the sake of demonstration, I'll try to whip up a small sample.

Here's your native library:

class CMyNativeClass
{
public:
CMyNativeClass();

void DoSomething();
void DoSomethingElse();
};



Here's your C++/CLI library:

public ref class MyManagedClass
{
private:
CMyNativeClass *nativePointer;
void Destruct() { delete nativePointer; nativePointer = NULL; }

public:
MyManagedClass() { nativePointer = new CMyNativeClass(); }
~MyManagedClass() { Destruct(); }
!MyManagedClass() { Destruct(); }

void DoSomething() { nativePointer->DoSomething(); }
void DoSomethingElse() { nativePointer->DoSomethingElse(); }
};



And then in your C# application:

MyManagedClass c = new MyManagedClass();
c.DoSomething();
c.DoSomethingElse();

c.Dispose(); // alternatively, the finalizer will take care of it

Share this post


Link to post
Share on other sites
Thanks for the example.

Anyway, I think the P/Invoke is the better suited solution for me. The C wrapper is also useful in other scenarios while the managed C++ is limited only to import into C#. I will go down this road first. Please let me know if you are aware of some good examples and maybe pointers about possible pitfalls.

Cheers and thanks again,
-Dirk



Share this post


Link to post
Share on other sites
Using C++/CLI makes it hard to work with Mono on Linux, if that is something you care about.

P/Invoking directly into C++ is one of those "wow, that is hard" things which I don't think anyone has really found an answer, except for a sadist who actually entered the mangled names for a single library version of Qt. Which was not pretty, hence not an answer.

You can use extern "C" with C++, I think that allows you to P/Invoke a bit easier, but most C++ applications don't use that. The C layer basically uses the extern "C" to show it.

Its sad since most OpenGL GUI's are C++, which makes it really hard to do a cross-platform GUI. :) One reason I've been working on my own, I haven't found a good one.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this