Jump to content
  • Advertisement
Sign in to follow this  
Dirk Gregorius

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

This topic is 3812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!