Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
6 Feb 2001 209.4K   2.4K   34   25
Using the high level API to send and receive SOAP messages in MFC
  • Download MFC Calculator Client source files - 28 Kb
  • Download Demo ATL object and Web Service MFC client - 57 Kb
  • Sample Image - calc_eg.gif

    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


    Written By
    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 AI, ML and cloud Nic is particularly interested in Python, Angular and web development as well as having a passing interest in iOS, mobile and AR.

    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.

    Comments and Discussions

     
    Questionsetting proxy Pin
    wunderbart31-Jul-06 0:14
    wunderbart31-Jul-06 0:14 
    GeneralMSSOAP1.dll program problem in VC++ Pin
    smpshehan9-Jun-05 16:46
    smpshehan9-Jun-05 16:46 
    Questionhow to use startHeaderElement for my soap message? Pin
    smpshehan9-Jun-05 1:15
    smpshehan9-Jun-05 1:15 
    GeneralOleException Pin
    7-Jan-05 5:48
    suss7-Jan-05 5:48 
    GeneralRe: OleException Pin
    smpshehan9-Jun-05 1:49
    smpshehan9-Jun-05 1:49 
    GeneralRe: OleException Pin
    Anonymous9-Jun-05 14:48
    Anonymous9-Jun-05 14:48 
    GeneralDitched Soap 3.0 Pin
    LikePork8-Sep-04 11:09
    LikePork8-Sep-04 11:09 
    It seems everyone uses soap toolkit 2.0, what's wrong with the 3.0 version. I used it in a little app I created and it worked fine but I've found so little said about it. Is it that Microsoft ditched it in favor of webservices before it was a proven technology, anybody who knows why it's so unpopular please enlighten me thanks.

    You Know It!
    QuestionWindows XP can not send big xml file throught SOAP??? Pin
    sue15962020-Aug-04 3:31
    sue15962020-Aug-04 3:31 
    QuestionSOAP ..needs COM or .NET ??? Pin
    Member 1915118-Jul-04 21:07
    Member 1915118-Jul-04 21:07 
    GeneralMSSOAP.SoapClient Pin
    miss_mystikal30-May-04 22:41
    miss_mystikal30-May-04 22:41 
    QuestionParameter names? Pin
    Tony Truong21-Feb-04 6:40
    Tony Truong21-Feb-04 6:40 
    QuestionDeleting after use? Pin
    tobylang31-Dec-02 5:34
    tobylang31-Dec-02 5:34 
    GeneralUsing a string Pin
    tobylang23-Dec-02 11:17
    tobylang23-Dec-02 11:17 
    GeneralSoap toolkit 2.0 Pin
    21-Apr-02 7:47
    suss21-Apr-02 7:47 
    GeneralNo installation at all! Pin
    30-Jan-02 2:05
    suss30-Jan-02 2:05 
    GeneralNo rope.dll at all Pin
    22-Nov-01 20:06
    suss22-Nov-01 20:06 
    GeneralRe: No rope.dll at all Pin
    11-Jan-02 5:00
    suss11-Jan-02 5:00 
    GeneralRe: No rope.dll at all Pin
    Anonymous12-Sep-02 18:54
    Anonymous12-Sep-02 18:54 
    GeneralC++ Documentation Pin
    26-Jul-01 13:15
    suss26-Jul-01 13:15 
    GeneralRe: C++ Doc. MSSoaptoolkit 2.0 Pin
    23-Nov-01 1:19
    suss23-Nov-01 1:19 
    GeneralI receive one error... Pin
    31-May-01 2:23
    suss31-May-01 2:23 
    GeneralRe: I receive one error... Pin
    23-Nov-01 1:10
    suss23-Nov-01 1:10 
    GeneralAutomation method calls... Pin
    Paul Wolfensberger12-Jan-01 3:33
    Paul Wolfensberger12-Jan-01 3:33 
    GeneralRe: Automation method calls... Pin
    Nic Oughton12-Jan-01 5:30
    professionalNic Oughton12-Jan-01 5:30 
    GeneralRe: Automation method calls... Pin
    Paul Wolfensberger12-Jan-01 8:41
    Paul Wolfensberger12-Jan-01 8:41 

    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.