What is the main difference between C++ vs C++.NET? [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between Managed C++ and C++/CLI?
What is CLI/C++ exactly? How does it differ to 'normal' c++?
I am in doubt of distinguishing between C++ and C++.NET.
Is that right C++ is unmanaged code and C++.NET is managed code?
I need to program for a project in C++. For better building the GUI, I would prefer to use C++.NET.
I also have another plain C++ library (unmanaged C++ DLL file), will it be possible to use it as a normal DLL library in the C++.NET project?

Is that right C++ is unmanaged code and C++.NET is managed code.
There's no such thing as "C++.NET". There's C++/CLI, which is basically C++ with Microsoft extensions that allow you to write code targeting the .NET framework. C++/CLI code compiles to CLR bytecode, and runs on a virtual machine just like C#. I'll assume you're actually talking about C++/CLI.
With respect to that, one can say standard C++ is unmanaged and C++/CLI is managed, but that's very much Microsoft terminology. You'll never see the term "unmanaged" used this way when talking about standard C++ unless in comparison with C++/CLI.
Both standard C++ and C++/CLI can be compiled by the same Visual C++ compiler. The former is the default on VC++ compilers, while a compiler switch is needed to make it compile in latter mode.
I need to program for a project in C++. For better building the GUI, I
would prefer to use C++.NET.
You can build GUI programs in C++ just as well as C++/CLI. It's just harder because there isn't a standard library in standard C++ for building GUI like the .NET framework has, but there are lots of projects out there like Qt and wxWidgets which provide a C++ GUI framework.
I also have another plain C++ library (unmanaged C++ dll), will it be
possible to use it as a normal dll library in the C++.NET project?
Yes. It might take some extra work to deal with the different standard C++ data types and .NET data types, but you can certainly make it work.

Managed C++ is a now deprecated Microsoft set of deviations from C++, including grammatical and syntactic extensions, keywords and attributes, to bring the C++ syntax and language to the .NET Framework. These extensions allowed C++ code to be targeted to the Common Language Runtime (CLR) in the form of managed code as well as continue to interoperate with native code. Managed C++ was not a complete standalone, or full-fledged programming language.
Managed C++
#using <mscorlib.dll>
using namespace System;
int main() {
Console::WriteLine("Hello, world!");
return 0;
}
Vanilla C++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!";
return 0;
}

Well... C++ .NET is kind of a misnomer.
You can program in C++ using visual studio .NET. Well that's what it was called along time ago. Now a days folks just call it Visual Studio, with the dot NET moniker. Well, at least the splash screen doesn't have a big ol .NET in the logo anymore.
It is kind of understood that using Visual Studio (VS), you can program in managed and unmanaged languages (Lots of choices there btw).
If you want to program in C++ using Visual Studio you have two choices:
Unmanaged or native C/C++. This is the old (or new I guess too) C++
that you have always known, and you program with unmanaged memory.
Managed C++. They call this C++/CLI. That is read C++ over CLI, not
C++ divided by CLI! This is C++ that has extra keywords, and a few
extra syntax elements than the native C++. This allows you to
utilize the .NET Foundation Class Library and do other fun things in
the .NET framework. This of course uses the garbage collector for
memory for managed types.
Personally my favorite language is C#, but if you need to interop between C++ and .NET than definitely use Managed C++. It is very easy to do, and I think is easier than that other P/Invoke stuff.
If you are going to some project, I would suggest you do your UI in C# and take advantage of all that it has to offer. Then have that reference a mixed mode managed library that contains your C++ code. I think that will be a lot easier for you.
The answer to your last question is yes, you can definitely use that in your app.
Here is how the dependencies would work:
[C# App/GUI] depends on [Managed C++ assembly] depends on [Native C++ Lib]

Yes, C++ is unmanaged code and C++/CLI is managed.
Yes, you can use your unmanaged C++ DLL in your C++/CLI project. But you have to write a wrapper for that. That means you have to define the unmanaged methods you want to access in your C++/CLI project.
Example:
using System.Runtime.InteropServices;
[DllImport("YourDLLName")]
public static extern void UnmanagedMethodName(string parameter1);

Related

Delphi - C++ interoperability concerning containers

I have a C/C++ .dll containing core functionality of my program, and a Delphi project for the graphical user interface.
Is there any way I can export something like a std::vector or std::map to Delphi and use it in C++?
Thanks in advance
C++ classes can only be directly consumed by C++ code. Similarly, Delphi classes can only be directly consumed by Delphi code.
An exception to these rules is that Embarcadero's compilers have interop support and it is possible to consume some Delphi classes from C++ code, so long as the C++ code is compiled using an Embarcadero compiler.
Assuming that you are not using an Embarcadero C++ compiler you will need to wrap any classes that you wish to export in an interop friendly manner. The obvious choice for this is COM which was designed to solve this very problem.

What is CLI/C++ exactly? How does it differ from 'normal' c++?

Let me clarify what I mean by 'normal' C++ first- I'm currently reading Walter Savitch's "Problem Solving in C++". As far as I am aware this is not written specifically for Microsoft or Unix. So my question is, how does what I am learning in this book (which I am using for my universal knowledge-gaining of c++) differ from what I keep reading about CLI C++?
Is CLI C++ just what I would encounter if I used Visual C++? I'm totally confused.
C++/CLI, (Also sometimes C++/CLR) refers to a language which is positioned somewhere in between native C++, and the .NET framework.
It's usually used for applications where you need to bridge some native code (pure C++) and managed code (Like VB, C#, F#, etc).
C++/CLI is a much different beast than regular C++ though. And when people say Visual C++, the meaning can vary depending on context. Sometimes they mean C++ with the common language runtime (CLR) layer enabled, other times they mean just plain C++. It's unfortunate that there's a lot of different terminology out there, and a lot of misnomers, but what can you do?
C++ and C++/CLI differ greatly. C++/CLI is the managed .NET-version of C++, made by Microsoft to enable a layer from .NET to native code.
c++/cli is the current version of Microsoft's Visual c++ brand of tools. (There was a different design before c++/cli)
c++/cli is really two versions of c++ in one. There is a highly standardized c++ compiler and also a version of c++ that runs on the CLI virtual machine. Obviously standard c++ never runs on a VM so that determined the two in one approach to the language.
When using it you can mix unman aged and managed code together. "Unmanaged" is code compiled just like standard c++. "Managed" is code compiler to the CLI (.Net) virtually machine's bytecode. Microsoft has extensive APIs for both managed and unmanaged code. With the product you can access both APIs.
You can develop standard c++ apps with the language and tool, you just have to be careful not to used the non-standard extensions. I suggest compiling in another compiler once in a while if you are in doubt. The managed side is totally non standard and even the unmanaged side has tons of non standard extensions.
PS I'm no expert but I was curious and read about this last week. I thought your question deserved an answer. Good luck!

Why does the C++ "Hello World" project generated by Visual Studio look a little strange?

I'm new to C++ programming (although I have experience in Java, C#, and Visual Basic). I have used Visual Studio 2010 to create a default "Hello World" example project, but when I investigate the sample code it generates, it looks a little bit different from the code that I see when looking at C++ tutorials.
In my investigations, I've learned that there are two versions of C++, or at least two different standards. I think they're called CLR and CLI. What standard or version must I learn to program further in the future?
There's regular, plain-old, ISO-standards-based C++ which is probably the kind you're seeing in tutorials. If you want to write Windows applications in regular C++, you will probably be targeting the Win32 API (or using a set of classes that wrap the basic functionality of the Win32 API, such as MFC).
Then there's C++/CLI, which can almost be thought of as an entirely new language (albeit a superset of C++) that includes Microsoft's extensions in order to support the .NET Framework. It is standardized as ECMA-372. The .NET Framework runs on top of the CLR, so the version of C++ that is compatible with the CLR is called "C++/CLI".
You probably want to ignore the C++/CLI variant of the language right now entirely. It's really only useful in interoperability scenarios with .NET code. Since you appear to want to learn C++, the extra CLI stuff is just going to be a confusing distraction. You want to learn real C++, not the .NET Framework grafted on top of C++. If you want to learn .NET, start with either C# or VB.NET, instead.
A bit more information on the distinction between C++ and C++/CLI is available in my answer here.
c++/cli is a dotNET-based language, depending on the CLR.
C++ is defined by an ISO Standard, and should not be confused with microsoft's c++/CLI
If you wish to work with a dotNET-based language, learn C# not C++/CLI. Most likely your tutorials are for regular C++ - this is a lot more common than C++/CLI. Visual Studio works well with either standard C++ or C++/CLI, you select which to use when creating a project.

How to port C++ code to C++/CLI in Visual Studio?

I have an application written in native C++ which I'd like to get running on the .NET virtual machine. I was thinking of recompiling the C++ code as C++/CLI, using the Visual Studio 2008 compiler. Regrettably, I don't find any documentation on how to do this, so hence my questions:
Does this actually make sense? Am I trying the impossible?
Where can information on the topic be found?
Go to project properties -> General -> Common Language Runtime support -> change to /clr
It's called CLR now. Read about it here and here.
A lot of native C++ code will actually just compile and run on C++/CLI. This is really a kind of hybrid compiler that can call native Win32 functions and use standard C libraries like OpenGL. You can even call COM interfaces directly (all the stuff you can do with a native C++ compiler).
The .Net library is also available but for these you create managed classes (using the ref class keyword). You will use gcnew to allocate memory for these classes (from a garbage collected heap). Memory for your normal classes is still allocated using new and delete (from a standard, non garbage-collected heap).
In short, you can migrate to .Net in bits and pieces, though there is still some friction when switching between managed and unmanaged classes.
I found this book useful: Pro Visual C++/CLI.
In C++ you can simply recompile your codebase with /clr. This technique called IJW (It Just Works) so you can easily use your existing classes with CLR.

New keywords and new type of pointers in Visual C++ 2005. What is managed C++?

Possible duplicates
What is gcnew?
What does the caret mean in C++/CLI?
Difference between managed c++ and c++
I am a advanced C++ programmer with g++. But currently I am working on Visual C++ 2005 doing Windows Forms Application programming . But I am finding it hard with its new terminology. For e.g. instead of new it has gcnew and
String ^ kind of thing. Can someone explain what is ^, similar to pointer?
Can I make Visual C++ work in the same way as normal C++ like g++ compiler? I also heard something about managed C++. What is that?
The gcnew and ^ values are managed C++ which is a different language to c++. You can use VS2005 as a normal C++ compiler by not using a project type from the CLR section of the new project window.
gcnew and ^ are from new Visual C++ syntax. The new syntax is not part of the ISO/ANSI C++ standard, but is a set of extensions to C++ standardized under the Ecma C++/CLI Standard. You can to not use them if you do not want to. Here you could read more about Visual C++ extensions.
Windows Forms are .Net specific, so you need to use C++/CLI (That's "managed C++") if you want to do those.
However, I'm not sure you really want windows forms? You just want some kind of windowy GUI, right? If that's the case you can go with something like MFC or just native O/S calls, to create your GUI, or you could use a wrapper API like Qt
[EDIT] Just to clarify a bit :)
I also heard something about managed C++. What is that?
C++/CLI is the .Net implementation of C++. Here you can use both regular C++ and managed code. C++/CLI was Microsofts attempt to ease the learning curve for C++ developers to get into the managed framework, however, it's not used a lot these days, wo you'll probably have a hard time finding (m)any good tutorials. Visual C++ is just the name of the IDE, it has nothing to do with which kind of C++ you use. You can use Visual C++ just as you use g++, but if you wan't anything over a console app, you'll need to wire some GUI logic into your application. As said in my original answer, this can be done a number of ways.
If you are sure you want to use .Net, I recommend spending a day with C#, as it's really easy to learn if you got C++ experience, but judging from your question I don't think this is what you want :)
Managed C++ is used for .Net development in the Common Language Runtime (CLR) of Microsoft. This special C++ syntax was created to allow C++ developper to come in the .Net community without learning a new language like C#.
You can use Visual C++ in the same way as g++ by creating a non-CLR project.
Note that Windows Forms are not the only way to make GUIs under Windows. You can use the regular Win32 API instead, if you want pure C (which you can wrap with C++ if you like).
If you do choose Windows Forms, then you have the option of using C#, VB.NET, or C++/CLI (Managed C++). You need not use C++/CLI, as seems to be your presumption.