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

MS Soap Toolkit 2.0 - Oi! Where's My Rope Gone

, 6 Feb 2001
Rate this:
Please Sign up or sign in to vote.
Using the high level API to send and receive SOAP messages in MFC
<!-- Link to source file download -->
  • Download MFC Calculator Client source files - 28 Kb
  • <!-- Link to demo file download -->
  • Download Demo ATL object and Web Service MFC client - 57 Kb
  • <!-- Article image -->

    Sample Image - calc_eg.gif

    <!-- Add the rest of your HTML here -->

    Introduction

    This is true - the day after I had completed converting ROPE.dll to work for my Win98 users, the Microsoft Soap Toolkit 2.0 arrives! Not only does the new toolkit work on more flavours of Windows but it has all changed.

    So OK, now I can get down to the real work of providing my users with great new Web Services using a single DLL set, but hang on where's the CPP samples??? Yet again, a new technology blighted by VB only samples! I decided as my first step in getting to grips with the new toolkit was to convert the samples to MFC, and here is the first. An MFC implementation using the high level Soap Client API. The first set of sample code uses the simple parameter types Calculator Service included with the Soap Toolkit. The second is a simple ATL object, client and ASP listener, to test passing strings around.

    Getting Started

    Before starting you should follow the instructions in the SOAP Toolkit samples.htm file. First, you will need to create a virtual directory on your web server. Then, you will need to create the ClcVBSrv DLL. The VB project can be found in the samples/ClcVBSrv folder under your SOAP Toolkit installation directory. After building the DLL, you will need to recreate the Web Services Description Language (WSDL) file and Web Services Meta Language (WSML) file, using wsdlgen.exe, to include the location of your web server. The ASP listener for this service does not need to be changed.

    The Project

    The project is a straight forward MFC dialog application. I have created a very simple interface similar to the one in the VB client.

    Including the SoapClient in the project

    The easiest way to include the SoapClient in an MFC application is by using COleDispatchDriver class. Basically the COleDispatchDriver class allows you to implement the client side of COM, providing access to an object's methods and properties.

    To add the SoapClient into the project, go to the Class Wizard. Click the 'Add Class' button and choose 'From a Type Library'. Then, locate MSSOAP1.dll. You will then be given a list of interfaces, contained within the DLL, to choose from. For the purposes of this simple project, I only needed the ISOAPClient interface. This adds two files to the project mssoap1.cpp and mssoap1.h.

    To use the SoapClient interface add a variable of type ISOAPClient, e.g.ISOAPClient * m_pClient

    Creating the ISOAPClient pointer

    As I have used a pointer for my SoapClient interface variable, two steps are required before it can actually be used. First, create a new pointer of type ISOAPClient. This creates a C++ wrapper for the object. Then create an IDispatch object which is automatically attached to our wrapper.

    //Allocate a new SoapClient pointer
    m_pClient = new ISOAPClient;
    //Now create the SoapClient pointer
    m_pClient->CreateDispatch("MSSOAP.SoapClient")

    The CreateDispatch function handles the creation of the IDispatch pointer for us. We just need to supply the ProgId (in this case MSSOAP.SoapClient) of the object we want to use.

    Note: You can create a version dependant instantiation by including the version (e.g. "MSSOAP.SoapClient.1") This is something I have never found a need for!

    After creating the SoapClient, initialise it using the mssoapinit function passing the name of the server, port and WSML file.

    m_pClient->mssoapinit("http://www.MyService.com/SoapSamples/CalcVB.wsdl", "CalcVB", "CalcVBPortType", NULL);

    Calling the Web Services functions

    So we have got our SoapClient ready to use, but how do we call the services functions. In this case, simply calling m_pClient->Add(23.34, 34.2 ) will cause a compiler error. The application needs to know something about the functions. To use the Web Services functions, a function header and body needs to be declared. Using the Add function as an example the header and body look like this

    //Function header
    double Add(double dblA, double dblB, DISPID dispid);
    
    //Function body
    double ISOAPClient::Add(double dblA, double dblB, DISPID dispid)
    {
    	double result;
    	static BYTE parms[] =
    		VTS_R8 VTS_R8;
    	InvokeHelper(dispid, DISPATCH_METHOD, VT_R8, (void*)&result, parms, dblA, dblB);
    	return result;
    }

    The function itself takes as parameters two doubles and returns a double. I have added the dispid parameter to the function to pass the dispatch id of the function I am calling. The function body uses InvokeHelper to call the Add function. The InvokeHelper function is fairly easy to use. Full details on using this function can be found in the MSDN documentation.

    I have added these functions to the mssoap cpp and header files, although you could declare them elsewhere and pass in the SoapClient pointer/object.

    Note: If you do add additional functions to the wizard created files, care must be taken when adding further interfaces from the COM object, as the additional functions maybe deleted.

    This only leaves getting the dispatch id of the function to call.

    In actual fact, a quick and dirty method for calling the functions is to use the internal index of the OLE controller. This will generally index the functions in the order that they are numbered. Indexing them from -1 upwards. This will probably work nine times out of ten. However, it is not a rule of COM that the controller must do this and so the result cannot be guaranteed.

    So, I have created a function to get the dispid that I need.

    //Function to get the Dispatch ID of the function we want to call
    //Pre : strFXName is the name of the function
    //Post: returns the dispath id of the function 
    DISPID CDemo1Dlg::GetDispid(CString strFxName)
    {
    	OLECHAR * name = strFxName.AllocSysString();
    	DISPID dispid;
    
    	//Using the dispatch pointer member get the dispatch id of the required
    	//function from the interface
    	m_pClient->m_lpDispatch->GetIDsOfNames(IID_NULL, &name, 1, GetUserDefaultLCID(), &dispid);
        return dispid;
    }

    This function takes the name of the Web Service function we want to call and retrieves its dispatch id.

    Conclusion

    So OK, there's a whole lot more code here than in the VB sample. And no, the high level API approach to SOAP messaging is not the most flexible. However, if you just want to get going, using your language of choice, and to start including SOAP in your MFC projects then this is a relatively painless way of doing it.

    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

    Share

    About the Author

    Nic Oughton
    Architect SPTS Technologies
    United Kingdom United Kingdom
    Nic has been programming for Windows and devices for many, many years using a wide variety of languages and methodologies including C,C++,C#,.Net,MFC.
     
    He is employed as the Global Applications Manager for SPTS Technologies (www.spts.com) a world leading supplier of capital equipment to the semi-conductor industry. Responsible for the smooth running and development of all of SPTS's business systems Nic still gets to, occasionally, write a little code!
     
    Currently pursuing all things mobile Nic is particularly interested in Windows phone development and cloud computing.
     
    Nic has a varied background including an honours degree in Drama, 18 months as a mechanical buyer and stints as lead singer in several (unknown) bands. He lives in Bristol, UK with his wife and two children.

    Comments and Discussions

     
    Questionsetting proxy Pinmemberwunderbart31-Jul-06 1:14 
    GeneralMSSOAP1.dll program problem in VC++ Pinmembersmpshehan9-Jun-05 17:46 
    Questionhow to use startHeaderElement for my soap message? Pinmembersmpshehan9-Jun-05 2:15 
    GeneralOleException Pinmemberutekii@yahoo.com7-Jan-05 6:48 
    GeneralRe: OleException Pinmembersmpshehan9-Jun-05 2:49 
    GeneralRe: OleException PinsussAnonymous9-Jun-05 15:48 
    GeneralDitched Soap 3.0 PinmemberLikePork8-Sep-04 12:09 
    QuestionWindows XP can not send big xml file throught SOAP??? Pinmembersue15962020-Aug-04 4:31 
    QuestionSOAP ..needs COM or .NET ??? Pinmemberhittmann4@hotmail.com8-Jul-04 22:07 
    GeneralMSSOAP.SoapClient Pinsussmiss_mystikal30-May-04 23:41 
    QuestionParameter names? PinmemberTony Truong21-Feb-04 7:40 
    QuestionDeleting after use? Pinmembertobylang31-Dec-02 6:34 
    GeneralUsing a string Pinmembertobylang23-Dec-02 12:17 
    GeneralSoap toolkit 2.0 PinmemberAnonymous21-Apr-02 8:47 
    GeneralNo installation at all! Pinmemberom30-Jan-02 3:05 
    GeneralNo rope.dll at all PinmemberMarleen Körner22-Nov-01 21:06 
    GeneralRe: No rope.dll at all PinmemberZekester11-Jan-02 6:00 
    GeneralRe: No rope.dll at all PinsussAnonymous12-Sep-02 19:54 
    GeneralC++ Documentation PinmemberE. Brown26-Jul-01 14:15 
    GeneralRe: C++ Doc. MSSoaptoolkit 2.0 Pinmemberkondi23-Nov-01 2:19 
    GeneralI receive one error... PinmemberAdrian Bacaianu31-May-01 3:23 
    GeneralRe: I receive one error... Pinmemberd@hotman.com23-Nov-01 2:10 
    GeneralAutomation method calls... PinmemberPaul Wolfensberger12-Jan-01 4:33 
    GeneralRe: Automation method calls... PinmemberNic Oughton12-Jan-01 6:30 
    GeneralRe: Automation method calls... PinmemberPaul Wolfensberger12-Jan-01 9:41 

    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 | Terms of Use | Mobile
    Web03 | 2.8.141223.1 | Last Updated 7 Feb 2001
    Article Copyright 2001 by Nic Oughton
    Everything else Copyright © CodeProject, 1999-2014
    Layout: fixed | fluid