Click here to Skip to main content
15,917,862 members
Articles / Web Development / ASP.NET

ASP.NET with Managed C++

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
19 Aug 2002CPOL2 min read 144.9K   1.2K   21   12
Use Managed C++ as your code-behind file for ASP.NET web pages

Sample Image - HelloWorldMC.gif


I recently discussed this topic with Chris Maunder on the Message Boards. Chris concluded that ASP.NET does not support Managed C++, well I agree. But it does support MSIL compiled assemblies.

Eventhough the .NET Framework supports many language compilers, ASP.NET has out of the box support for parsing three languages only; C#, VB.NET, JScript. Therefore, we must bypass ASP.NET parser to use a language that ASP.NET does not support. The method we will use to accomplish this is called "code-behind". This method should work for any language that supports a compiler for .NET (e.g. COBOL). I will be demonstrating this technique with the renowned "Hello World" example. This article's only objective is to show you, how to use Managed C++ as code-behind for ASP.NET pages, it doesn't demonstrate any complex C++ coding.

Using Managed C++ as ASP.NET code-behind

First, lets create a simple ASP.NET file called HelloWorldMC.aspx. The most crucial part of this file is Inherits="HelloWorldMC", this will tell ASP.NET to look for an assembly (code-behind) file.

<%@ Page AutoEventWireup="false" Inherits="HelloWorldMC" %>
        <title>HelloWorld MC++</title>
        <form runat=server ID="Form1">
           <asp:Button id="Button1" runat="server" Text="Click Me Please" 
           OnClick="SayHello" Width="172px"></asp:Button>

Next we create the Managed C++ file, as our code-behind file named HelloWorldMC.aspx.cpp, I am following the naming convention of Visual Studio.NET here. It is important to keep the names of your server controls consistent between your .aspx file and your .cpp (code-behind) file. For example, id="Button1" is used in the .aspx file and Button* Button1 is used in the code-benind file, which will bind it to the <asp:Button id="Button1" runat=server></asp:Button> control. Also remember that the function SayHello must be public so the .aspx page can access it.

#using <system.dll>
#using <mscorlib.dll>
#using <system.web.dll>

using namespace System;
using namespace System::Web::UI::WebControls;

public __gc class HelloWorldMC : public System::Web::UI::Page

    Button* Button1;


    void SayHello (Object* sender, EventArgs* e)
        Button1->Text = "Hello MC++ World";	


Compiling the project

Make sure that the HelloWorldMC.aspx and HelloWorldMC.aspx.cpp files are placed in the root of your IIS web server (i.e. C:\Inetpub\wwwroot). Now if the subdirectory bin does not exist in the root, create it (i.e. C:\Inetpub\wwwroot\bin). The following command line syntax will compile and create our code-behind file and place it in the bin. Make sure this is ran from the web server root direcotory.

cl /clr HelloWorldMC.aspx.cpp /link /dll /out:bin\HelloWorldMC.dll

Start up your browser and browse the file, http://localhost/HelloWorldMC.aspx, ASP.NET does not care how the code-behind (assembly) was created, as long as it adheres to the Inherits="HelloWorldMC" in the .aspx file, which tells ASP.NET there is an assembly named HelloWorldMC.dll. Please note that there are advantages to using code-behind techniques in all your ASP.NET programming, to list a couple:

  1. Compilation before execution
  2. Seperation of presentation code from business logic

Obtaining Intellisense for .CPP code-behind file, in VS.NET

My suggestion for obtaining intellisense for your .cpp file is, to add a blank Managed C++ project to a currentley existing web project. I tried creating the .cpp code-behind file first, then adding an existing item to my web project and the .cpp file was added to my web project, but the intellisense would not work properly.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionCould not load file or assembly 'TradeQueueApi' or one of its dependencies. An attempt was made to load a program with an incorrect format. Pin
ardyllesdavid23-Aug-07 21:35
ardyllesdavid23-Aug-07 21:35 
QuestionWhy not just refrence a managed dll? Pin
rj4518-May-07 7:55
rj4518-May-07 7:55 
Questionproblems with classes Pin
Saturday3AM6-Mar-06 23:17
Saturday3AM6-Mar-06 23:17 
Generalcannnot compile the cpp Pin
chenen16-May-04 17:47
chenen16-May-04 17:47 
GeneralRe: cannnot compile the cpp Pin
TigerNinja_16-May-04 18:05
TigerNinja_16-May-04 18:05 
GeneralRe: cannnot compile the cpp Pin
chenen17-May-04 4:02
chenen17-May-04 4:02 
GeneralLooking for more on this topic Pin
Mike Seck29-Nov-03 9:06
Mike Seck29-Nov-03 9:06 
GeneralRe: Looking for more on this topic Pin
Anonymous30-Nov-03 16:01
Anonymous30-Nov-03 16:01 
GeneralKnown problems with this scenario Pin
Nick Hodapp26-Aug-02 7:10
sitebuilderNick Hodapp26-Aug-02 7:10 
GeneralRe: Known problems with this scenario Pin
TigerNinja_26-Aug-02 22:32
TigerNinja_26-Aug-02 22:32 
GeneralCool Pin
James T. Johnson20-Aug-02 8:58
James T. Johnson20-Aug-02 8:58 
GeneralRe: Cool Pin
TigerNinja_20-Aug-02 9:47
TigerNinja_20-Aug-02 9:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.