<!-- Download Links -->
<!-- Add the rest of your HTML here -->
Unless you are a biblically familiar with COM, prior to .NET creating applications that were written
in multiply languages was quite the chore. XML based Web Services are becoming a very popular method to transfer
information between distributed systems, even thought web services themselves have been around well before
the .NET Framework was introduced. The .NET Framework allows us to quickly develop web services even
when multiple languages are introduced to the formula. The purpose of this article is two fold: I wanted to
learn a little more about Managed C++, and thought that writing the core database portion in Managed C++ would be a
great learning experience, while exposing the functionality to a C# web service. I had not actually written
a web service before and so I thought tying the two together would make a nice example. I will go ahead and
try to explain everything; however being familiar with concepts of classes themselves in C++ is helpful. Someone recently asked if it was possible for multiple languages to work together under .NET within one program, hopefully this example should identify this possibility.
Managed C++ Assembly Core
We need to begin by creating a new Managed C++ assembly; this is where the majority of the
actual work will reside. If you are remotely familiar with C# you will notice that Managed
C++ uses a similar notation to include namespaces, however it is slightly different. For our
assembly we will need to include the following namespaces (This is listed in the mcpp.h header file):
using namespace System;
using namespace System::Data;
using namespace System::Data::SqlClient;
using namespace System::Xml;
In Managed C++ we also have to identify the files that the namespaces reside in,
this is done using the #using statement, somewhat similar to the #include
for including header files with standard C++.
We will need to include the following files for our example project under the Stdafx.h header file.
When we created our project you will see that Visual Studio.NET created a base class called
The next step is to locate the header file that contains our class description and add a
function prototype to identify our new method, which we will use to get data from SQL Server.
You will find the mcpp.h header file contains out class framework. Upon opening up this file you should
see an initial class declaration that looks something like this, I have gone ahead and added the prototype below:
public __gc class MyNumber
int GetWebStat(); <font color="green">
Now we will need to add the implementation to this function in the mcpp.cpp file located inside your project.
sql = S"select count(*) as myCount from site_stats";
mySQLConnection = new SqlConnection(S"server=[YourServerName];" +
sqlCommand = new SqlCommand(sql, mySQLConnection);
dr = sqlCommand->ExecuteReader();
i = Convert::ToInt32(dr->get_Item(S"myCount")->ToString());
Now you can build and compile this assembly into your .DLL, which is all we will
need to do in the Managed C++ world.
C# Web Services
The next step is to create a new Web Service in C# to expose the functionality
we just wrote into our Managed C++ assembly. You will need to load up Visual Studio.NET and
select choose ASP.NET Web Service as the project template.
To incorporate the Managed C++ assembly into our C# project we will need to do two things.
First, go to Projects and select "Add Reference". A dialog will load and you will need to select
"Browse" in the upper right hand corner. You will need to select the assembly .DLL from the
Managed C++ project, this is located in either the Debug or Release folder depending on how you
built your release. At the top of the code where all of the using statements are located identifying
all the namespaces in the project, include the following:
This was the name of the namespace that the Managed C++ assembly used earlier.
Now we simply add a new method in C# and create an instance of our Managed C++ class,
it's really that simple. Refer to the following code as an example for the
remaining C# source to the web service. What really becomes interesting is that if you have worked
with Windows Forms in C# your class declaration no longer inherits from
System.Web.Services.WebService instead. Once you have added the following function to your
C# code you can save and build the project. If you have never worked with XML based web services before,
Chris Maunder has written Your first C# Web Service
is a nice source for additional information. You may need to map a virtual directory to your web server as I did for this
[WebMethod(Description="This is an implementation of a" +
" cross language web service between Managed C++ and C#.")]
public int MyWebStats()
<font color="green"> mcpp.MyNumber m = new mcpp.MyNumber();
<font color="green"> i = m.GetWebStat();
Consuming the Web Service in an Application
The ability to consumer these new XML based Web Services in our applications can add a great level of extensibility to any application. I have included another project that consumes this web service we have demonstrated. To include a Web Service in your project you will need to select "Add Web Reference" by right-clicking on the References in your project. This will load a dialog for you to locate your Web Service. You can enter the following URL to find my example service in your dialog: http://www.developernotes.com/web_svr/service1.asmx. As soon as the service is located you can hit the "Add Reference" button at the bottom right corner. I created a simple form and added the following code to gain access to our new Web Service; amazingly the plumbing is taken care of for us.
private void button1_Click(object sender, System.EventArgs e)
this.label2.Text = "Checking...";
com.developernotes.www.MyWebSite stats = new com.developernotes.www.MyWebSite();
this.label2.Text = stats.MyWebStats().ToString();
A live example can be seen here running on my website to show the returning XML feed:
Live XML Web Service Example
This simple example should be enough to show you how easily the .NET Framework allows
multiple languages to talk to each other with ease and without the granularity COM introduces.
Any question, comments, or flames can be posted below.
- 12/06/2002 - Initial Release.
- 12/08/2002 - Added Web Service Consumer application.