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 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?
w_briggsAsked:
Who is Participating?
 
AlexFMCommented:
Project - Properties - Configuration Properties - General - Common Language Runtime Support - /clr.

Or create new project of type: Visual C++ - CLR - Class Library.
0
 
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).
0
 
AlexFMCommented:
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.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
devsolnsCommented:
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)
    {
        *pInt=1979;
    }
   
    public unsafe static void Main()
    {
        CData d = new CData();
       
        Console.WriteLine("Previous value: {0}", d.x);
       
        fixed(int *p=&d.x)
        {
            SetVal(p);
        }
       
        Console.WriteLine("New value: {0}", d.x);
    }
}

http://www.codeproject.com/csharp/unsafe_prog.asp
0
 
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.
0
 
AlexFMCommented:
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.
0
 
devsolnsCommented:
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.
0
 
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."
0
 
w_briggsAuthor Commented:
Just tried it in .NET 2003.  It requires a /clr option, but adding a /clr option has no effect.
0
 
AlexFMCommented:
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.
0
 
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
0
 
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.
0
 
w_briggsAuthor Commented:
Cool.  This works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.