Click here to Skip to main content
Click here to Skip to main content

SOAP client for C++

, 31 Mar 2004
Rate this:
Please Sign up or sign in to vote.
A base class to ease the work of calling a webservice in C++.

Introduction

While creating web service and consuming it are very easy and intuitive in the .NET world, sometimes we still need to handle it in legacy systems. Several days ago, I faced a problem to call a web service in my old VC6 project. I Googled the web and realized that MS SOAP SDK will be the solution. Being familiar with C# language, going back to C++ is kind of frustrating, especially when handling COM interfaces. So I decided to write a helper class to ease the work of calling a web service, which is attached here.

The base class

The base class is called SoapClientBase, which is for inheritance only, implements most tasks to talk with the SOAP SDK. I will rather not talk a lot about the code of the base class. Instead, I'll give the steps about how to write an inherited class. The code of the base class is short, take a look at it for yourself if you want.

The inherited class

The code of the inherited class is even shorter. Here is the complete code of my class:

#pragma once
#include "SoapClientBase.h"

class AuthServiceClient : public SoapClientBase
{
public:
 AuthServiceClient(void) : SoapClientBase()
 {
  Init("<A href="http://localhost/AuthService/AuthService.asmx?wsdl">http://localhost/AuthService/AuthService.asmx?wsdl</A>", "AuthService", "");
 }

 bool IsAuthorized(LPCTSTR username, LPCTSTR password)
 {
  _variant_t varParams[2] = {  password, username };
  _variant_t varResult;
  m_hr = Invoke(L"IsAuthorized", varParams, 2, &varResult);
  return VARIANT_TRUE == varResult.boolVal;
 }
};

The signature of my webservice method (C#) is:

  [WebMethod]
  public bool IsAuthorized(string username, string password);

As you have already seen, all that an inherited class should do is to call Init function in the constructor (or you might choose to call it explicitly outside the class), and then wrap all the web methods in local functions. You have to write all the wrap functions manually, but it's not hard to do. One thing that you should note here is that the parameter order should be reversed in the parameter array.

How to call

After you implemented the inherited class, use it to call the webservice almost the same way as you might do in C#. Here's an example:

 AuthServiceClient service;
 bool bResult = service.IsAuthorized(strName, strPassword);

Very simple, isn't itSmile | :)

Dependency

First of all, SOAP Toolkit3.0 SDK or SOAP Toolkit3.0 Redistributable must be installed. The base class file contains the import instruction to load the SOAP DLL. You might have to change the path if you installed the SDK in another place.

The base class does not depend on MFC nor ATL. Instead, I copied a little code from ATL to make life easierSmile | :) Therefore, you can use this class anywhere, no matter which library the application uses.

Okay, that's all. The code is very short, so don't expect too much Smile | :) The point is: it makes my life easier. And I hope it makes your life easier too. Happy programming!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Neil Yao

China China
I'm a chinese programer living in Shanghai, currently working for a software company whose main business is to deliver computer based testing. Software simulation for computer based testing and certifications is my main responsibility in this company. Execpt for software development, I like out-door activities and photography. I am willing to make friends in China and all over the world, so contact me if you have anything in common with meSmile | :)

Comments and Discussions

 
Questionsame problem Pinmembermonimon15-Mar-13 6:22 
QuestionVT_DISPATCH [modified] Pinmembertahar12-Mar-13 6:46 
QuestionCoCreateInstance Failed PinmemberLianchang_Mou29-Feb-12 18:03 
GeneralNo Deserializer found Error Pinmemberdenemeew21-Sep-10 4:08 
GeneralCannot create soap client object ! PinmemberBattosaiii7-Feb-10 22:52 
GeneralRe: Cannot create soap client object ! PinmemberMember 311365515-Mar-10 20:45 
GeneralRe: Cannot create soap client object ! Pinmemberlevashoff28-May-10 14:29 
GeneralRe: Cannot create soap client object ! Pinmemberkeatsin18-Feb-11 15:52 
GeneralFacing Problem in calling C# Web service PinmemberBinni shah27-Nov-09 23:12 
GeneralHELP!!! Pinmembernewhis26-Nov-09 20:10 
QuestionRe: HELP!!! PinmemberMember 311365515-Mar-10 20:38 
GeneralRe: HELP!!! Pinmemberwinstond14-Apr-10 18:55 
GeneralRe: HELP!!! PinmemberMedXLnce22-Oct-10 8:55 
GeneralRe: HELP!!! PinmemberMedXLnce22-Oct-10 8:54 
GeneralRe: HELP!!! PinmemberNicholas Bowen19-Jan-12 13:53 
Generalcool PinmemberAung Sithu Kyaw10-Feb-09 16:29 
GeneralError compiling SoapClientBase.h Pinmemberqwertypik28-Jan-09 3:55 
GeneralRe: Error compiling SoapClientBase.h Pinmember8200151819-Aug-09 19:47 
QuestionSee this subject in simple-Chinese code PinmemberShinGoZhang15-Dec-08 14:24 
AnswerRe: See this subject in simple-Chinese code PinmemberShinGoZhang15-Dec-08 14:26 
QuestionHow to declare at MFC if webservice function return a recordset Pinmemberotcinvestor27-Aug-07 6:26 
GeneralUrgent Help needed PinmemberFranco Amato15-Aug-07 6:38 
Generalneed help Pinmemberfllsm31-May-07 17:31 
AnswerE_INVALIDARG Pinmembergbjbaanb6-Nov-07 7:54 
GeneralGetting "Create soap client object fail" alwase. Pinmemberpavanbhai30-May-07 0:48 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 1 Apr 2004
Article Copyright 2004 by Neil Yao
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid