Tuesday, August 11, 2015

How do you create a Visual C++ CLR Console Application in Visual Studio 2015?

Common Language Runtime (CLR) applications depend on .NET Framework and are compiled to intermediate code interpreted by CLR at run time. Win32 applications compile/link directly to machine code and is independent of CLR.

You can create Visual C++ CLR Console application using Visual Studio.

Launch VS2015 Express 2015 for Windows Desktop.

Click File | New Project
Expand Visual C++ node and click CLR. The New Project Windows presents the diffrent project Templates as shown:


ClrConsole_01

Click CLR Console Application  shown above and change the Name at the bottom of screen to ClrConsoleApp.

Click OK to create the folder/files for the project template as shown. Accept default location for directory. The project gets created as shown:


ClrConsole_02

Make sure you read the ReadMe.txt file in the project that gives the overview of the project you created.
The References node shows the default references general, data and xml.

Reference System contains fundamental classes and bare classed that define commonly used ValueTypes and reference datatypes, events, handlers etc.

mscorlib: assembly dll that supports .NET Framework development

The template already has the 'Hello World' program. Note that for this program all that you need is the namespace System. The following is the main project file created by the template.
--
// ClrConsoleApp.cpp : main project file.
#include "stdafx.h"
using namespace System;
int main(array ^args)
{
    Console::WriteLine(L"Hello World");
    return 0;
}

-------------
Click Build | Build Solution for F7 (see next image)


ClrConsole_03

 
The build succeeds as it should as shown:

ClrConsole_04

Click Debug | Start Without Debugging. The Console window opens as
shown displaying the result of running the code:

ClrConsole_05
The next image shows the project in the file system of the computer.

ClrConsole_06
Access the project properties to view more details:


ClrConsole_07
The Application.exe file can be run from command line. There is no graphical user interface.

Note that intellisense is available in the VS 2015 IDE as shown here (just added a int variable):

ClrConsole_08

Here is some code added to ClrConsoleApp.cpp
--------------
// ClrConsoleApp.cpp : main project file.
#include "stdafx.h"
using namespace System;
int main(array ^args)
{
    Console::WriteLine(L"Hello World");
    Console::WriteLine("");
 int x = 5;
 Console::WriteLine(x);
 return 0;
}


The code when run produces this:

 

No comments: