3

I've read many solutions to the issue of using C# code in a C++ project, including this one: How to use c# code in C++ project

I've also read this, and one answerer says Compile your C++ code with the /clr flag. With that, you can call into any .NET code with relative ease.

Does this mean that I can use C++ & C# code together, within the same project if I append the /clr flag? I'm a little confused by this, and it'll just waste time if I went the long route of converting the C# project that I'm trying to use parts of to C++, if doing so isn't actually needed.

Basically, I answered my own question on a different topic a couple of days ago without realising that the actual code of the sample project is C#: Obtaining Current Network Rate

Can anybody give me a firm clarification to whether I understand this correctly (that I can use C# code in a C++ project with no issues, apart from the /clr switch being required)? I'm not sure if SO is the right place to ask a question like this, so please tell me if it isn't instead of downvoting with no explanation.

Thanks.

Edit

Forgot to mention that this is a C++/CLI gameserver application DLL on Windows. If it matters, it is used on only Windows Server 2008 R2 and Windows 7.

Community
  • 1
  • 1
AStopher
  • 4,207
  • 11
  • 50
  • 75
  • If you use the /clr switch you can use .NET code in managed c++ classes. Managed c++ is just that, its on the managed heap. You use gcnew to allocate things and they are automatically deallocated. You can also mix non-managed heap items (regular c++) which you manage yourself. However, you can't access .NET from direct unmanaged c++. You need to go through the interop layer, which would be managed c++ to .NET. – devshorts Mar 04 '14 at 19:53
  • Take a look at [This](http://stackoverflow.com/questions/6095982/writing-c-sharp-managed-code-in-native-c) also. – Smitt Mar 04 '14 at 20:02
  • 1
    Not sure if it is relevant for you, but compiling all your C++ with /clr hurts performance. It is possible (but not neccesarily fun) to compile only the parts of your code /clr that actually require to talk to .net code. –  Mar 04 '14 at 20:17
  • To the downvoter: why downvote, especially on an old question? – AStopher Nov 15 '14 at 16:23

3 Answers3

2

If you use C++/CLI, then you can add references to other .net assemblies, but you do not mix C++ and C# code in the same project. What you would do is to create a new project (or add an existing one) in your solution using C#, and then add a reference to it in the C++/CLI project.

The drawback is that you need to marshal between C++ and .Net types (std::string vs System::String^), and you also need to learn the additional syntax used by C++/CLI (^, gcnew, etc.).

Further reading: Pure C++: Hello, C++/CLI

crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • +1 maybe clarify that the extra "project" amounts to a an extra dll/assembly, in this case written in C# and consumed by a C++(/CLI) .dll (or .exe). –  Mar 04 '14 at 20:14
1

You need to make a C++/CLI project in visual studio. C++/CLI is a bit different to C++, because there is the managed stack, so memory is managed and you can avoid memory leaks (garbage collection). Simply create a C++/CLI project and click on propietys and add a reference to your C# project, it should work :D

http://www.codeproject.com/KB/cs/ManagedCOM.aspx

1

It means, that with the /clr switch, you can access assemblies (DLL or EXE) written for the .Net framework. Because such assemblies can be created with any .net language (including C#), you can use "code" written in C# from within C++, but it doesn't mean that you would be able to write C# in C++ project. Unfortunately - you can't.

Shlomi Borovitz
  • 1,700
  • 9
  • 9
  • Does this mean I can `#pragma comment` C# libraries and use them in C++. – AStopher Mar 04 '14 at 20:01
  • 1
    I cannot say.. you can test it (create a very simple dll in C#, and try to use it with `#pragma comment` from the C++ source. Anyway, you can add the C# assembly as a reference to the C++ project... (right click on the project in `solution explorer` -> add -> reference. – Shlomi Borovitz Mar 04 '14 at 21:12
  • 1
    @zyboxenterprises: No, you cannot use `#pragma comment` with .NET assemblies. But there is `#using`, which is probably what you are looking for. http://msdn.microsoft.com/en-us/library/yab9swk4.aspx – Ben Voigt Mar 05 '14 at 01:48