?
Solved

How to include C++ in C#?

Posted on 2006-03-23
13
Medium Priority
?
800 Views
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?
0
Comment
Question by:w_briggs
  • 7
  • 4
  • 2
13 Comments
 

Author Comment

by:w_briggs
ID: 16277245
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 16277849
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
 
LVL 13

Expert Comment

by:devsolns
ID: 16280714
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:w_briggs
ID: 16280998
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 16281368
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
 
LVL 13

Expert Comment

by:devsolns
ID: 16282044
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
 

Author Comment

by:w_briggs
ID: 16289674
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
 

Author Comment

by:w_briggs
ID: 16289850
Just tried it in .NET 2003.  It requires a /clr option, but adding a /clr option has no effect.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16289966
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
 

Author Comment

by:w_briggs
ID: 16290401
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
 

Author Comment

by:w_briggs
ID: 16290418
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
 
LVL 48

Accepted Solution

by:
AlexFM earned 1500 total points
ID: 16292417
Project - Properties - Configuration Properties - General - Common Language Runtime Support - /clr.

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

Author Comment

by:w_briggs
ID: 16324374
Cool.  This works.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question