Click here to Skip to main content
13,504,941 members
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 2 Apr 2001

Your first managed C++ Web Service

, 17 Oct 2001
Rate this:
Please Sign up or sign in to vote.
An introduction to writing your first WebService using C++ with managed extensions
<!-- Article Starts -->


This article assumes you are familiar with declaring and using managed types and the .NET Garbage Collector.

Creating your first web service is incredibly easy if you use C# or VB.NET (see my previous article for details). Writing a WebService using managed C++ in .NET is also extremely simple, but there are a couple of 'gotcha's that can cause a few frustrating moments.

My first suggestion is to use the Visual Studio .NET Wizards to create your WebService (in fact it's a great idea for all your apps when you are first starting out). This is especially important if you are moving up through the various builds of the beta bits of .NET. What is perfectly acceptable in one build may fail to compile in another build, and it may be difficult to work out which piece of the puzzle you are missing.

Using the Wizards can get you a managed C++ WebService up and running in minutes, but things can start to get a little weird as soon as you try something a little more risqué.

For this example I have created a service called MyCPPService by using the Wizard. Simply select File | New Project and run through the wizard to create a C++ WebService.

A new namespace will be defined called CPPWebService, and within this namespace will be the classes and structures that implement your webservice. For this example I have called the class MyService. Other files that are created by the wizard include the .asmx file that acts as a proxy for your service; the config.web file for configuration settings, and the .disco file for service discovery. Once you compile the class your assembly will be stored as CPPWebService.dll in the /bin directory.

I wanted to mimic the C# WebService created in my previous article, but with a few minor changes to illustrate using value and reference types. With this in mind I defined a Value Type structure ClientData and a managed reference type ClientInfo within the namespace that would both contain a name and an ID (string and int values respectively). 

__value public struct ClientData
	String *Name;
	int	ID;
__gc public class ClientInfo
	String *Name;
	int	ID;

In order to return an array of objects a quick typedef is also declared

typedef ClientData ClientArray[];

In a similar fashion I defined my MyService class as a simple managed C++ class with three methods:

  • MyMethod is a simple method that returns a single integer
  • GetClientData returns a single  ClientData structure
  • GetClientsData returns an array of ClientInfo objects
// CPPWebService.h

#pragma once
#using "System.EnterpriseServices.dll"

namespace CPPWebService
	__value public struct ClientData
		String *Name;
		int	ID;

	__gc public class ClientInfo
		String *Name;
		int	ID;

	typedef ClientData ClientArray[];

	__gc class MyService 
		int MyMethod();

		ClientData GetClientData();

		ClientArray GetClientsData(int Number);

The important thing to notice about the function prototypes is the [WebMethod] attribute - this informs the compiler that the method will be a method of a web service, and that it should provide the appropriate support and plumbing. The method you attach this attribute to must also be publicly accessible.

The implementation (.cpp) file is as follows.

#include "stdafx.h"
#using <mscorlib.dll>
#using "System.Web.dll"
#using "System.Web.Services.dll"

using namespace System;
using namespace System::Web;
using namespace System::Web::Services;

#include "CPPWebService.h"

namespace CPPWebService
	int MyService::MyMethod()
		return 42;

	ClientData MyService::GetClientData()
		ClientData data;
		data.Name = new String("Client Name");
		data.ID = 1;

		return data;

	ClientArray MyService::GetClientsData(int Number)
		// simple sanity checks
		if (Number < 0 || Number > 10)
			return 0;

		ClientArray data = new ClientData __gc[Number];

		if (Number > 0 && Number <= 10)
			for (int i = 0; i < Number; i++)
				data[i].Name = new String("Client ");
				data[i].ID = i;

		return data;

Note the use of the syntax i.ToString(). In .NET, value types such as int's and enums can have methods associated with them. i.ToString() simply calls the Int32::ToString() for the variable i.

One huge improvement of .NET beta 2 over beta 1 is that you no longer need to mess around with the XmlIncludeAttribute class to inform the serializer about your structure. A few bugs that either caused things to misbehave, or worse - not run altogether - have also been fixed. Writing a WebService in MC++ is now just as easy in C++ as it is in C#, with the advantage that you can mix and match native and managed code while retaining the raw power of C++.

Once you have the changes in place you can build the project then test the service by right clicking on the CPPWebService.asmx in the Solution Explorer in Visual Studio and choosing "View in Browser". The test page is shown below.

Clicking on one of the methods (say, GetClientsData) results in a proxy page being presented which allows you to invoke the method directly from your browser. The GetClientsData method takes a single int parameter which you can enter in the edit box.

When invoked this returns the following:


Writing WebServices using Visual C++ with managed extensions is just as easy as writing them using C# or VB.NET, as long as you remember a few simple things: use attributes, declare your classes as managed and make them publicly accessible. Using the Visual Studio.NET wizards makes writing and deploying these services a point and click affair, but even if you wish to do it by hand then the steps involved are extremely simple.


Oct 18 - updated for .NET beta 2


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


About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...


Comments and Discussions

QuestionWeb service in ANSI C and Pure C++ only Pin
Member 104452004-Dec-13 8:09
memberMember 104452004-Dec-13 8:09 
Question2005? 2008? Pin
Margaret Magnus14-Dec-09 9:19
memberMargaret Magnus14-Dec-09 9:19 
QuestionRequest an update?... this managed code is obsolete Pin
RedDK16-Sep-08 18:06
memberRedDK16-Sep-08 18:06 
Questionhow to access web service endpoint? I wanna know what its theory? Pin
colin helena13-Dec-05 18:55
membercolin helena13-Dec-05 18:55 
QuestionHow to change the translator of a web service from HTTP POST to SOAP? Pin
raewyn29-Nov-05 23:04
memberraewyn29-Nov-05 23:04 
GeneralNewbie doesnt understand how to compile this Web Service Pin
Mike Pliam9-Oct-05 5:21
memberMike Pliam9-Oct-05 5:21 
GeneralRe: Newbie doesnt understand how to compile this Web Service Pin
Chris Maunder9-Oct-05 11:09
adminChris Maunder9-Oct-05 11:09 
GeneralRe: Newbie doesnt understand how to compile this Web Service Pin
Mike Pliam9-Oct-05 20:22
memberMike Pliam9-Oct-05 20:22 
GeneralReceiving the Array in the Web Service Client Pin
Doug Knudson11-Aug-05 8:32
memberDoug Knudson11-Aug-05 8:32 
GeneralExposing web service without using IIS Pin
Anonymous12-Jan-05 11:20
sussAnonymous12-Jan-05 11:20 
GeneralIt's Good Article. Pin
baiju.km5-Sep-04 20:02
memberbaiju.km5-Sep-04 20:02 
QuestionHow to change the &quot;; namespace Pin
ldaoust14-May-04 10:02
memberldaoust14-May-04 10:02 
AnswerRe: How to change the &quot;; namespace Pin
ldaoust17-May-04 4:18
memberldaoust17-May-04 4:18 
GeneralRe: How to change the &quot;; namespace Pin
Pierrine14-Dec-04 4:04
memberPierrine14-Dec-04 4:04 
GeneralRe: How to change the &quot;; namespace Pin
ldaoust14-Dec-04 4:24
memberldaoust14-Dec-04 4:24 
Generalunmanaged code Pin
Isaac Lascasas15-Dec-03 7:32
sussIsaac Lascasas15-Dec-03 7:32 
GeneralResumable Downloads Pin
karl_marx22-May-03 17:26
memberkarl_marx22-May-03 17:26 
GeneralClient Pin
bumbyjumby6-Jan-03 13:22
memberbumbyjumby6-Jan-03 13:22 
GeneralInterop. Code Pin
MAXS72U19-Dec-02 20:27
memberMAXS72U19-Dec-02 20:27 
Generalperformance vs c# Pin
dbp25-Sep-02 8:03
memberdbp25-Sep-02 8:03 
Generalnamespace question Pin
Anonymous6-Jul-02 8:13
memberAnonymous6-Jul-02 8:13 
GeneralAgain, good one :-) Pin
Nish - Native CPian26-May-02 21:29
memberNish - Native CPian26-May-02 21:29 
QuestionRe: Again, good one :-) Pin
RedDK17-Sep-08 7:36
memberRedDK17-Sep-08 7:36 
GeneralWhy no number in results? Please see the detailed ... Pin
Gene6-Mar-02 0:14
memberGene6-Mar-02 0:14 
Questionhow to use webservice class in webclient? Pin
chungisu18-Dec-01 0:17
memberchungisu18-Dec-01 0:17 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180417.1 | Last Updated 18 Oct 2001
Article Copyright 2001 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid