Windows Web Services API Client code walkthrough on Windows 7
I decided to Dive into the WWSAPI Beta and see how I could call a simple HelloWorld Applications. Here are my adventures:
I installed Windows 7 in a HyperV VM and installed Visual Studio 2008 SP1.
I downloaded the Windows 7 SDK and attached the .ISO image as the CD drive and installed it.
I then ran the Windows SDK Configuration Tool and selected the Windows 7 SDK configuration.
Then I downloaded this wsdl: https://jsandersrocks.members.winisp.net/WebServiceTest/WebService.asmx?WSDL
I created a C++ console application with the defaults from the template and named it WWSAPITest.
I created a subdirectory called 'wsdl' in my new project and copied the WSDL into this directory and called it MyWebService.wsdl
Next I opened a CMD Shell Window from the Microsoft Windows SDK v7.0 menu.
I Navigated to the above directory were the WSDL file is and typed: wsutil *
This generated the .h and .c file that I then included in my project.
Just for fun I decided to build the Project to see if I had any errors.
I got these errors:
c:\users\jsanders\documents\visual studio 2008\projects\wwsapitest\wsdl\mywebservice.wsdl.c : fatal error C1853: 'Debug\WWSAPITest.pch' precompiled header file is from a previous version of the compiler, or the precompiled header is C++ and you are using it from C (or vice versa)
There are C files in the project now so I turned off precompiled header (under C++ Precompiled Headers) option and did a clean and rebuild of the project. I then got these errors:
MyWebService.wsdl.obj : error LNK2019: unresolved external symbol _WsCall@32 referenced in function _WebServiceSoap_HelloWorld@28
MyWebService.wsdl.obj : error LNK2019: unresolved external symbol _WsCreateServiceProxyFromTemplate@40 referenced in function _WebServiceSoap_CreateServiceProxy
MyWebService.wsdl.obj : error LNK2019: unresolved external symbol _WsCreateServiceEndpointFromTemplate@60 referenced in function _WebServiceSoap_CreateServiceEndpoint
I Included WebServices.lib in the input libraries in the Linker options rebuilt and then the project built fine.
Now the complete code listing for your enjoyment (Copy Code):
// WWSAPITest.cpp : Defines the entry point for the console application.
//
#include
"stdafx.h"
#include <WebServices.h>
#include ".\wsdl\MyWebService.wsdl.h"
int
_tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
WS_ERROR* error;
WS_HEAP* heap;
WS_SERVICE_PROXY* serviceProxy;
hr = S_OK;
error = NULL;
heap = NULL;
serviceProxy = NULL;// Creating error object
hr = WsCreateError(NULL, 0, &error);
if (FAILED(hr))
{
wprintf (L"Failed to create Error object\n");
return -1;
}
// Creating heap handle
hr = WsCreateHeap(10000000, 0, NULL, 0, &heap, error);
if (FAILED(hr))
{
wprintf (L"Failed to create Heap object\n");
if (heap != NULL)
{
WsFreeHeap(heap);
heap = NULL;
}
if (error != NULL)
{
WsFreeError(error);
error = NULL;
}
return -1;
}WS_HTTP_BINDING_TEMPLATE templateValue = {};
hr=WebServiceSoap12_CreateServiceProxy(
&templateValue,
NULL,
0,
&serviceProxy,
error);
if (FAILED(hr))
{
WsFreeHeap(heap);
WsFreeError(error);
return -1;
}WS_ENDPOINT_ADDRESS address = {};
WS_STRING Url = WS_STRING_VALUE(L"https://jsandersrocks.members.winisp.net/WebServiceTest/WebService.asmx");
address.url = Url;
hr = WsOpenServiceProxy(serviceProxy, &address, NULL, error);
if (FAILED(hr))
{
WsFreeServiceProxy(serviceProxy);
WsFreeHeap(heap);
WsFreeError(error);
return -1;
}
WCHAR *aResult = NULL;
hr= WebServiceSoap12_HelloWorld(
serviceProxy,
&aResult,
heap,
NULL, 0,NULL, error);
if (SUCCEEDED(hr))
{
wprintf(L"%s\n", aResult);
}
else
{
wprintf(L"failed\n");
}if (serviceProxy != NULL)
{
WsCloseServiceProxy(serviceProxy, NULL, error);
WsFreeServiceProxy(serviceProxy);
serviceProxy = NULL;
}
if (heap != NULL)
{
WsFreeHeap(heap);
heap = NULL;
}
if (error != NULL)
{
WsFreeError(error);
error = NULL;
}
return 0;
}
This console app ran and returned the expected 'Hello World'!
Let me know if you found this useful!
Comments
Anonymous
October 05, 2009
Hey....It seems gr8 to see that kind of work as I am seeing all .NET and related technologies every where in the MS world. I am not saying it is bad, but what it has done is to leave its C++ developers developing web applications completely stranded. By completely removing ATL server files out of VS 2008 and not including any guidance on how they could be integrated with VS 2008 on codeplex, they have left the job half done. Now who would want to build upon the stuff that is outcast and for which the policies are not cleared? If there was a walk through to integrate these things in VX 2008, there would have been atleast some development here as compared to nil. Having said that, your Http Client api for windows 7 looks cool and try it out soon. I am not sure how and if relates to my problem (ATL Server) in any remote sense, but I find it recent and from some one who is with MS team. If there is any information that is related, please email me at bhushanvi@hotmail.comAnonymous
September 18, 2013
Thanks man. It helped me in resolving the same problem. Great.