We help IT Professionals succeed at work.

How to include C++ in C#?

Medium Priority
Last Modified: 2013-11-18
I have sample code from Microsoft, showing how to wrap a C++ class for use in C#; it's at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp12192002.asp .  The compiler complains, in compiling the main C# class, that it doesn't have a definition for the C++ class.  (It also doesn't have the .cpp or .h files listed in the project, but that's easily remedied.)  

How do I tell it what .h files I want it to know about?
Watch Question


I may as well ask, also:  I need to set certain flags for compiling the C++ part of the project; but there's no place under Properties where I can set flags (at least nowhere I can see).

You need to create managed C++ Class Library project and compile all this stuff there. Then you can reference this library from C# project and use wrapper class.

I think your misreading that msdn post.  It is showing you how to use unmanaged C++ code in managed C++, not how to use C++ in C#.  The only way to "include" C++ code in your C# projects is mark it with unsafe.  You'll notice the use of pointes below...

class CProgram
    unsafe static void SetVal(int *pInt)
    public unsafe static void Main()
        CData d = new CData();
        Console.WriteLine("Previous value: {0}", d.x);
        fixed(int *p=&d.x)
        Console.WriteLine("New value: {0}", d.x);



Thanks, everyone.  Alex, I know what you're saying is true; what I need to know is *how* to reference the C++ from the C#.  Also:  how do I make a managed C++ project?  (That is, it is a console app, dll, static library, or something else?)

devsolns, I'm not seeing in your example a C++ file being used.  

The reason I think the link I posted is about using C++ in C# is how it starts:  "If you need to use some C++ code from C# ... you may need to use the Managed Extensions to C++."  Of course, the author could be wrong.

You need to create managed C++ Class Library. Add to it unmanaged C++ class and managed wrapper.
Add refererence to this library to C# project and use managed wrapper from C# code. You can add reference to this library exactly as reference to C# Class Library.

w_briggs, you would not be able to use an actual c++ file along with its .h so yeah its capability is pretty limited.  but lets say you had a an algorithm that you needed to use in c# without writing over you would just place all the code from the .cpp files into a c# file and mark it unsafe.  that is assuming that new code doesnt make use of types outside of that code.


OK.  I'm trying to make the C++ code into a DLL.  Using Visual 6.0, I get the complaint that __nogc is undefined.  

Using Visual 8.0, I get the complaint that

error C4980: '__nogc' : use of this keyword requires /clr:oldSyntax command line option

Adding /clr:oldSyntax under Properties, Linker, Command Line, Additional Options has no effect.  

I tried removing all the managed code; now it works as a DLL.

...however, I can't add this DLL as a reference; "A reference to [this DLL] could not be added.  Please make sure that the file is accessible, and that it is a valid assembly or COM component."


Just tried it in .NET 2003.  It requires a /clr option, but adding a /clr option has no effect.

If you have 8.0 version, use it, why do you need older version? Managed C++ syntax was changed in this version, find differences somewhere, for example, in What's New for 8.0 version.
Managed reference: ^ instead of *  (String^)
Creating managed class: gcnew instead of new: String^ s = gcnew String("...");
Managed reference class: ref instead of __gc: ref class SomeClass{...}
__nogc - not used.

If you want to use unmanaged Dll in C#, Dll must export functions (not classes). Client calls these functions using PInvoke.


I'm trying different versions because it isn't working in any of them.  I'd be happy to use 8.0.

When I discard __nogc and replace __gc with ref, I get these problems in 8.0:

using namespace System; <-- no such namespace exists
ref , IDisposable, String <-- unknown
InteropServices, no such namespace
GC undefined
GC::SuppressFinalize, undefined


Also, about using ^, it says, you must use /clr if you're going to do this.

I have attempted to set /clr in various ways in Properties, Linker, Command Line, Additional Options, but the list of command line options above it does not change, and the compiler still thinks /clr is missing.
Project - Properties - Configuration Properties - General - Common Language Runtime Support - /clr.

Or create new project of type: Visual C++ - CLR - Class Library.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Cool.  This works.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.