Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

ASP.Net compatibility with C++

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

C++ is an older language of the C family, whereas the C# followed in ASP.net 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.

Using CLR (Common Language Runtime) environment, we can migrate C++ code to the modern Asp.net Apps Development by using CLR (Common Language Runtime) environment. This redesign is called C++/CLI, and the intent is 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.

If you want to migrate your business application from any other language to .NET, hire ASP.NET developers for error-free and secure application migration.

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.

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 the .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 a heap and will not get garbage collected. It is a major advantage that we can’t implement 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++, you can do this by declaring the abstract classes which use the 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 a function. 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 the C# code. Hence this option is chosen rarely. //** IJW means "It just works", and P/Invoke means "Platform Invocation Services". The above 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" is used to retrieve the name for each function from the DLL. Next is to determine functions and accessible methods based on the class definitions in the header. We can list the mapping of the mangled names of class members as follows:

C++ Decorated Name Class Member Note
??0Vehicle@@QAE@ABV0@@Z Default constructor Added by compiler
??0Vehicle@@QAE@PAD@Z Vehicle::Vehicle(char *) -
??1Vehicle@@UAE@XZ Vehicle::~Vehicle() -
??4Vehicle@@QAEAAV0@ABV0@@Z Class default structure Added by compiler
??_7Vehicle@@6B@ Virtual table (VTB) Added by compiler
?GetId@Vehicle@@QBEPADXZ Vehicle::GetId() -
?Move@Vehicle@@UAEXXZ Vehicle::Move() -
??0Car@@QAE@ABV0@@Z Default constructor Added by compiler
??1Car@@UAE@XZ Car::~Car() -
??4Car@@QAEAAV0@ABV0@@Z Class default structure Added by compiler
??_7Car@@6B@ Virtual table (VTB) Added by compiler
?Move@Car@@UAEXXZ Car::Move() -

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

After seeing, the example, we can say that C++ can access Common Language Runtime (CLR) in Visual C++ 2005. The redesign is named 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.

Description C++/CLI C#
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; N/A
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() {}
Boxing int^ h = 123; object h = 123;
Unboxing 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; } }

We can use the above tools to implement mangled classes of C++ in C# as many complicated tasks of the C# can be easily written in the C++ classes in simple forms. Thus, one can build powerful applications in ASP.Net.

Software Development Team
Need Software Development Team?

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs


NSS Note
Trusted by Global Clients