Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


C++ Lib with C# GUI


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 gnomgrol   Members   -  Reputation: 622

Like
0Likes
Like

Posted 17 August 2013 - 01:34 PM

Hello there, 

 

I have a problem with my DirectX11/C++-Engine. I started writing it as an independent project, with a main.cpp and all that. When it had grown bigger and I started to need it for multiple projects, I came up with the idea of putting it into a library. I had to choose between .lib and .dll, and since I am not experienced with either of these, I went for the .lib, since from what I read, exporting it as .dll would require me to rewrite ALL classes that are supposed to be accessed from outwards. 

Now I want to create a worldbuilder from it, and for the interface I used .NET. C++ at frist, but I noticed that it was dropped from Visual Studio since 2012 (it worked out fine though). I was told to use C# instead, but now I can't figure how to include my C++ lib into a C# project, and how to call functions from it - they require e.g a HWND handle, which can't be used in C#, since it is declared "unsafe". 

 

What would you suggest me to do? Just carry on with the old C++.Net, or find a way of using it in C#? And should I stick with the .lib or rewrite my code so it can be used in a .dll file?

 

Thanks for your concern and best whishes!

-gnomgrol



Sponsor:

#2 djtesh   Members   -  Reputation: 306

Like
1Likes
Like

Posted 17 August 2013 - 02:54 PM

This might not be useful in all cases, but I have a game engine written in C++ and I wanted an editor in C# from the very beginning.

 

== My Approach ==

 

In my case, I use a library called SWIG to generate C# wrappers around C++ classes that I want to expose to an editor. It might take a long time to tweak the ".i" files so that the C# output is exactly what you want.

 

So, the typical setup is:

 

- Special build for Editor. First we run SWIG to generate C# wrappers and a master .cxx file that contains the DLL invoke receiver functions.

- Engine compiles to a DLL with regular C++ files and the .cxx file.

- Link in your DLL into the C# project as a reference.

- File link your C# files into the Editor C# project.

 

You'll need to keep them somewhat synchronized when you make changes to your C++ api. You can write many extensions on top of the C++ classes to bring in really nice C# functionality like reflection, properties etc.

 

== Second Approach ==

 

I did this in the past but it is Windows ONLY.

 

- A version of your C++ editor that properly compiles into a C++/CLI DLL.

- Use the DLL as a reference in your pure C# editor project.

 

Writing CLI compatible C++ code requires a lot of consideration though.

 

Hope this helps,

Mitesh



#3 gnomgrol   Members   -  Reputation: 622

Like
0Likes
Like

Posted 17 August 2013 - 03:39 PM

Hey Mitesh, thanks for your answer!

Since I am targeting windows-only anyways, I would like to go with your second suggestion.

My c++-files compile fine with the /cli:pure option checked - I needed that for the c++.NET editor anyways. Does that mean it is already fully CLI compatible?

I can just call my C++-classes from a dll in my C# project as soon as I linked it?

 

Is rewriting my .lib to a .dll a lot of work? I got around 200 classes, and having to change all of them would take quite some time. Is there some kind of trick to it?



#4 djtesh   Members   -  Reputation: 306

Like
0Likes
Like

Posted 18 August 2013 - 10:22 PM

As far as I know, the dll is executable code while a .lib is compiled code which is yet to be linked to any executable.

 

You'll need to adjust your Visual Studio project, or just create a new one for this DLL build.

 

I did this many years ago, but what I remember with C++/CLI dlls is that they work like any other .NET reference. Which means they are usable with any language supported by the common language system. C#, F#, VisualBasic - you name it.

 

I'm a bit rusty so other members who have done this recently should share this easy bit of info.



#5 ChaosEngine   Crossbones+   -  Reputation: 2499

Like
2Likes
Like

Posted 19 August 2013 - 04:56 PM

OP, you don't need to rewrite you lib as a dll.

 

Leave your lib as it is, and create a new C++/CLI dll that links to the lib.

 

Then write a few C++/CLI wrapper classes that expose the functionality you want to call from C#/.net. You can actually use HWNDs in .net, they are usually wrapped in an IntPtr class. The WindowInteropHelper class will help you here.


if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#6 kunos   Crossbones+   -  Reputation: 2207

Like
0Likes
Like

Posted 20 August 2013 - 02:13 AM

OP, you don't need to rewrite you lib as a dll.

 

Leave your lib as it is, and create a new C++/CLI dll that links to the lib.

 

Then write a few C++/CLI wrapper classes that expose the functionality you want to call from C#/.net. You can actually use HWNDs in .net, they are usually wrapped in an IntPtr class. The WindowInteropHelper class will help you here.

 

+1, that's also how I do it. Engine into a static lib.. C++/CLI wrapper that exposes only what I need from the static lib to C# world.


Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS