Consuming Web Services in C

team lib

Consuming Web Services in C++

Visual Studio .NET makes it simple to consume Web services from C++ projects. Whenever you create a C++ projecteven the simplest of Win32 console applicationsyoull find that Solution Explorer adds a References folder to the project tree. Right-click on this folder, and choose the Add Web Reference... entry from the context menu. This will display the Add Web Reference dialog box, a as shown in Figure 7-7.

click to expand
Figure 7-7: Adding a reference to a Web service to a Visual Studio .NET project

You then need to enter a URL that tells the Web service to send back its WSDL description. If the service is named MyService, the URL will look like this:


You would obviously substitute the correct server name , if the service isnt hosted on the local machine. The Handler argument takes a value of the form Gen ServiceName WSDL, where ServiceName is the name of the Web service. Click the Go button, and after a pause you should see details of the methods exported by the Web service, as shown in Figure 7-8.

click to expand
Figure 7-8: The dialog displays the WSDL description of a Web service

If you decide that this is the service you require, you can click the Add Reference button to generate a proxy class and have it added to the project. You will find that a header file called named WebService.h is added to the project, and that this references another header called named localhost.h. Opening the localhost.h header will show you the proxy class that has been generated to talk to the Web service.

To find out what methods you can call, you can either browse the header file code, or use the Class View window to examine the proxy class.


If the Class View window isnt already visible, use Ctrl+Shift+C, or use the Class View entry on the View menu.

The program in Listing 7-8 calls a Web service method. You can find this sample in the Chapter07\WebClient folder of the books companion content.

Listing 7-8: WebClient.cpp
start example
 #include<iostream> usingnamespacestd; #define_WIN32_WINNT0x0500 //Includethegeneratedheader #include "webservice.h" //Thenamespacecreatedfortheservice usingnamespaceAtlWebSrvService; intmain() { //COMmustbeinitializedbeforeusingWebservices CoInitialize(NULL); { //Createaproxy CAtlWebSrvServicetheService; //CalltheWebservicemethod CComBSTRbResp; HRESULThr=theService.HelloWorld(CComBSTR(L"foo"),&bResp); if(FAILED(hr)) { wcout<<L"HelloWorldfailed(" <<hex<<hr<<dec <<L")" <<endl; } //Printthereturnedstring wcout<<static_cast<constwchar_t*>(bResp)<<endl; } CoUninitialize(); return0; } 
end example

Note how the CAtlWebSrvService object is created within a code block. Its essential for the object to release any COM interface pointers that it holds before the call to CoUninitialize . If you dont put the declaration in a code block, the destructor for the CAtlWebSrvService object will be called after the call to CoUninitialize , and the client program will crash.

team lib

COM Programming with Microsoft .NET
COM Programming with Microsoft .NET
ISBN: 0735618755
EAN: 2147483647
Year: 2006
Pages: 140

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: