Archived

This topic is now archived and is closed to further replies.

C++ and C# together

This topic is 5042 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

Hi I''ve been working with C# for the last couple of days and I really like that it''s SO easy to do simple things like GUI and importers/converters... BUT! I would like to keep my engine code in C++. Can I have one assembly where I have both C# files with my GUI code and C++ files with my engine code? I''ve read that it should be possible but I just can''t see how right now. If it is possible, does my C++ files need to managed? I''d rather prefer them to be good ol'' fashioned unmanaged.

Share this post


Link to post
Share on other sites
It''s definitely possible. Whether you want managed C++ or ANSI-style depends on how difficult you want to make things. If C# code only reads C++ functions then you can use PInvoke without much problem. If you want the C++ code to be using C# it gets difficult beyond the point of my experience with the matter. Maybe someone else can elaborate on how, but for what you''ve described I think you can get by without.

Share this post


Link to post
Share on other sites
I''m curious about this too for the same reasons. With all the C# hoopla around here, I thought I''d pick up a book and start messing with it. I''m haven''t done enough to have any real opinion on the language thus far, but I do like the GUI setup tools

-John

Share this post


Link to post
Share on other sites
How would you set it up in Visual Studio? Can you have one project containing both .cpp and .cs files and make it into one .exe file? Or would you use a .dll for the c++ functions?

Share this post


Link to post
Share on other sites
You can use Managed C++. It lets you use the .NET framework from Visual C++. The sintax isn''t pretty, but you have GC, etc.

The same project can have managed and unmanaged parts, so you keep your engine as it is and add the rest in managed C++. You can call to and from managed code without problems.

Share this post


Link to post
Share on other sites
I do this for my ODE C-sharp wrapper.

You can''t mix CPP files and CS files in the same project file, but you can add both projects to the same solution, and import the C++ assembly as a reference in the C-sharp project, and it''s about as good.

I just compile everything as "managed" code, because it makes debugging and interop easiest. Note that, even if you compile "managed" C++, the rules are exactly the same as for unmanaged C++, unless you start decorating your classes with __gc and __value and stuff -- even if you compile "managed," by default everything goes on the unmanaged heap and runs with unmanaged rules.

The interface between C# and C++ uses a set of thin interface classes which take in managed pointers/types and __pin them for the un-managed C++ code. This works fine.

Share this post


Link to post
Share on other sites
Br1 I know that I can use Managed C++ and I did my first tests in that, but I think the compile time is very slow and also the program start up time is slow compared to that of C#. The compile speed and the ease of use makes C# a better alternative for light weigh GUI stuff, while I prefer to keep critical engine code as unmanaged C++.... yet!

hplus0603, I will check out what you wrote, sounds good.

Share this post


Link to post
Share on other sites