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

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.

Related

Visual Basic Interface and C++ DLL

Is it a good idea to use c++ for creating DLL files and Visual basic for the windows forms? It's like Visual Basic manages all inputs and the interface while c++ takes care of all computation. Is there a big difference from creating those with Visual Basic only?
It can be a good solution to perform fast in memory operations with C++ and then use the results in .NET, we did it for an application in my previous company and it worked really well.
To make it easier to use the C++, we created a C++/CLI project wrapping the native C++ classes so we could just reference the C++/CLI project from VB.NET.

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

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);

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!

CLR C++ VS C++ (pstsdk)

Considering Simon Mourier's answer to this question:
Processing Microsoft Office Outlook 2003/2007 email messages…
I plan to use the PST File Format SDK which is written in C++.
I would take this opportunity to learn more about C++ and to renew with it, since it's been quite 15 years since the last time I used it. I have already downloaded and configured Boost 1.45 which is required to work with pstsdk.
Now, I'm currently writing a Windows Forms application using CLR C++ and plan to use the pstsdk to read from PST files.
Does it matter in any way that I'm using both CLR C++ and pure C++ altogether?
Shall I consider using it a different way, or is this okay?
If you want to use a .NET (Windows Forms, or maybe even the newer WPF) user interface, the simplest approach is to build an object model in C++/CLI, implemented in terms of the native code but having a .NET interface.
Then write the UI in C# and call the C++/CLI object model (which differs from using the .NET base class library in only one way -- you have to add a reference to the C++/CLI assembly... but the C++/CLI compiler will create all the metadata that C# uses).
You can mix managed and unmanaged code, but it will be a pain to marshal everything except the built-in types across the boundaries. It's much easier to stay with more powerful unmanaged C++. You could use CodeGear C++ Builder for example (or QT). The problem with CodeGear is compiler isn't that great, so you won't be able to compile everything from Boost, but you might not need that.
C++/CLI is intended to interop with unmanaged C++- that's pretty much it's entire purpose. However, I feel that it's probably easier to write in C# if you need .NET for, say, WPF, which is an excellent technology, and just use C++/CLI for interop.

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.