JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

ASP.Net compatibility with C++

This blog post explains why is compatible with C++ more than any other option. We have shared this post for developers who would like to know about compatibility with C++.

C++ is an older language of the C family, whereas the C# followed in is comparatively a new version. But still there is a lot of compatibility and need of time where we have to use the C++ code in ASP.Net.

The C++ code can be migrated to the modern Apps Development by using CLR (Common Language Runtime) environment. This redesign is called C++/CLI, and is intended to provide a more natural syntax for consuming and authoring CLR types. The traditional C++ language is enabled to write more elegant and efficient code targeting the .NET Framework. The following ways provide an insight to use C++ for ASP.NET.

The existing tools, utilities and classes written in C++ are still useful in the higher version of languages. In Asp. Net ,there are three main methods to invoke the C++ code to be reused.

1. IJW :

This method provides the facility to reuse C++ code in the ASP. Net. A minor change in the code is required . After invoking the code , you have to recompile the code in . Net platform.

#include "stdafx.h" #using <mscorlib.dll> #include <tchar.h> #include <windows.h> using namespace System; using namespace System::Runtime::InteropServices; public __gc class MsgBox { public: MsgBox(String *str) { IntPtr ptrtxt = Marshal::StringToCoTaskMemUni(str); MessageBoxW(0,(LPCWSTR)ptrtxt.ToPointer(), L"IJW is powerful",0); Marshal::FreeCoTaskMem(ptrtxt); } }; int _tmain(void) { String *str; str = "WELCOME"; MsgBox *m_msgbox = new MsgBox(str); return 0;

StringToCoTaskMemUni copies the string to an unmanaged area in the heap and will not get garbage collected. This is the major advantage which can not be implemented using C#

2. COM. :

The COM model allows you to reuse the classes written in different languages. This particular method can be used with C++ or by any other class written in the object-oriented form. In C++ , this done by declaring the abstract classes which use pure virtual function.

class Box { public: // pure virtual function virtual double getVolume() = 0; private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box };


This method allows importing a class as functions. You can import class methods as individual functions. To marshal C++ classes into a C# project ,Microsoft provides marshal C-functions. the P/Invoke method creates a lot of complications and changes are to be made in C# code. hence this option is chosen rarely //** IJW means "It just works" and P/Invoke means " Platform Invocation Services". The above mentioned three methods are inbuilt in C++.

Retrieve Exported Information from the DLL

So whenever we have to import a class from C++ , import the class methods from the DLL. If the source code is not available, the Microsoft dumping tool "dumpbin.exe" to retrieve the name for each function from the DLL is used . Next is to determine functions and accessible methods based on the class definitions in the header. Mapping of the mangled names of the class members can be listed as:

C++ Decorated Name

Class Member



Default constructor

Added by compiler


Vehicle::Vehicle(char *)




Class default structure

Added by compiler


Virtual table (VTB)

Added by compiler






Default constructor

Added by compiler




Class default structure

Added by compiler


Virtual table (VTB)

Added by compiler



The "name mangling" are compiler-dependent and vary from one version to another. If you add/remove/change class members to the Win32 project, the new DLL will have different "mangled names" for the constructor or other class members having entire information about the class member and its relationship with the rest of the class. In the case of the relationship changes , the mangled name will also be changed in new DLL.

After seeing, the example , we can say that the C++ can access Common Language Runtime (CLR) in Visual C++ 2005. The redesign is named as CLI. The new CLI represents a more natural syntax for authoring and using CLR types.

To sum up the compatibility of the C++ with the ASP.Net , the following table elaborates the convertibility type from C++ to ASP.Net




Allocate reference type

ReferenceType^ h = gcnew ReferenceType;

ReferenceType h = new ReferenceType();

Allocate value type

ValueType v(3, 4);

ValueType v = new ValueType(3, 4);

Reference type, stack semantics

ReferenceType h;


Calling Dispose method

ReferenceType^ h = gcnew ReferenceType; delete h;

ReferenceType h = new ReferenceType(); ((IDisposable)h).Dispose();

Implementing Dispose method

~TypeName() {}

void IDisposable.Dispose() {}

Implementing Finalize method

!TypeName() {}

~TypeName() {}


int^ h = 123;

object h = 123;


int^ hi = 123; int c = *hi;

object h = 123; int i = (int) h;

Reference type definition

ref class ReferenceType {}; ref struct ReferenceType {};

class ReferenceType {}

Value type definition

value class ValueType {}; value struct ValueType {};

struct ValueType {}

Using properties

h.Prop = 123; int v = h.Prop;

h.Prop = 123; int v = h.Prop;

Property definition

property String^ Name  {     String^ get()     {         return m_value;     }     void set(String^ value)     {         m_value = value;     } }

string Name  {     get     {         return m_name;     }     set     {         m_name = value;     } }

As the many complicated tasks of the C# can be easily written in the C++ classes in simple forms , therefore the above mentioned tools are used to implement mangled classes of C++ in C# thus creating powerful applications in ASP. Net.

Read More

Ast Note

Some of our clients