How to include C++ in C#?

I have sample code from Microsoft, showing how to wrap a C++ class for use in C#; it's at .  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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

w_briggsAuthor Commented:
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);
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

w_briggsAuthor Commented:
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.
w_briggsAuthor Commented:
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."
w_briggsAuthor Commented:
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.
w_briggsAuthor Commented:
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
w_briggsAuthor Commented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
w_briggsAuthor Commented:
Cool.  This works.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.