Jeff Sanders Technical Blog

I am a Microsoft employee that has worked on all aspects of the Web Stack for a long time. I hope these blogs are useful to you! Use this information at your own risk.


<< Go Back

Windows Web Services Api Client Code Walkthrough On Windows 7

- 24 Mar 2009

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.<p mce_keep="true">Then I downloaded this wsdl: http://jsandersrocks.members.winisp.net/WebServiceTest/WebService.asmx?WSDL</p> <p mce_keep="true">I created a C++ console application with the defaults from the template and named it WWSAPITest.</p> <p mce_keep="true">I created a subdirectory called ‘wsdl’ in my new project and copied the WSDL into this directory and called it MyWebService.wsdl</p> <p mce_keep="true">Next I opened a CMD Shell Window from the Microsoft Windows SDK v7.0 menu.</p> <p mce_keep="true">I Navigated to the above directory were the WSDL file is and typed:

wsutil * </p> <p mce_keep="true">This generated the .h and .c file that I then included in my project.</p> <p mce_keep="true">Just for fun I decided to build the Project to see if I had any errors.</p> <p mce_keep="true">I got these errors:</p>

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):<div style="BACKGROUND-COLOR: #e0e0e0" id=wwsapiwin7code1><font color=#008000 size=2><font color=#008000 size=2></p>

// WWSAPITest.cpp : Defines the entry point for the console application.

//

</font></font><font color=#0000ff size=2><font color=#0000ff size=2>

#include</font></font> <font color=#a31515 size=2><font color=#a31515 size=2>“stdafx.h”
</font></font><font color=#0000ff size=2><font color=#0000ff size=2>#include</font></font> <font color=#a31515 size=2><font color=#a31515 size=2> </font></font><font color=#0000ff size=2><font color=#0000ff size=2>#include</font></font> <font color=#a31515 size=2><font color=#a31515 size=2>“.\wsdl\MyWebService.wsdl.h”

</font></font><font color=#0000ff size=2><font color=#0000ff size=2>

int</font></font> _tmain(<font color=#0000ff size=2><font color=#0000ff size=2>int</font></font> argc, _TCHAR* argv[]) {

HRESULT hr; WS_ERROR* error; WS_HEAP* heap; WS\_SERVICE\_PROXY* serviceProxy; hr = S_OK; error = NULL; heap = NULL; serviceProxy = NULL;

<font color=#008000 size=2><font color=#008000 size=2>// Creating error object</font></font>

hr = WsCreateError(NULL, 0, &error);

<font color=#0000ff size=2><font color=#0000ff size=2>if</font></font> (FAILED(hr)) {     wprintf (L<font color=#a31515 size=2><font color=#a31515 size=2>“Failed to create Error object\n”</font></font>); <font color=#0000ff size=2><font color=#0000ff size=2>    return</font></font> -1; } <font color=#008000 size=2><font color=#008000 size=2>// Creating heap handle
</font></font>hr = WsCreateHeap(10000000, 0, NULL, 0, &heap, error); <font color=#0000ff size=2><font color=#0000ff size=2>if</font></font> (FAILED(hr)) {     wprintf (L<font color=#a31515 size=2><font color=#a31515 size=2>“Failed to create Heap object\n”</font></font>); <font color=#0000ff size=2><font color=#0000ff size=2><font color=#000000>    </font>if</font></font> (heap != NULL)     {

        WsFreeHeap(heap);         heap = NULL;     }

<font color=#0000ff size=2><font color=#0000ff size=2><font color=#000000>    </font>if</font></font> (error != NULL)     {         WsFreeError(error);         error = NULL;     } <font color=#0000ff size=2><font color=#0000ff size=2><font color=#000000>    </font>return</font></font> -1; }

WS\_HTTP\_BINDING_TEMPLATE templateValue = {}; hr=WebServiceSoap12_CreateServiceProxy(                                                                     &templateValue,                                                                     NULL,                                                                     0,                                                                     &serviceProxy,                                                                     error);

<font color=#0000ff size=2><font color=#0000ff size=2>if</font></font> (FAILED(hr)) {     WsFreeHeap(heap);     WsFreeError(error); <font color=#0000ff size=2><font color=#0000ff size=2><font color=#000000>    </font>return</font></font> -1; }

WS_ENDPOINT_ADDRESS address = {};
WS_STRING Url = WS_STRING_VALUE(L</font><font color=#a31515 size=2><font color=#a31515 size=2>“http://jsandersrocks.members.winisp.net/WebServiceTest/WebService.asmx”</font></font>);

address.url = Url;

hr = WsOpenServiceProxy(serviceProxy, &address, NULL, error);
</font><font color=#0000ff size=2><font color=#0000ff size=2>if</font></font> (FAILED(hr)) {     WsFreeServiceProxy(serviceProxy);     WsFreeHeap(heap);     WsFreeError(error); <font color=#0000ff size=2><font color=#0000ff size=2><font color=#000000>    </font>return</font></font> -1; } WCHAR *aResult = NULL; hr= WebServiceSoap12_HelloWorld(                                                         serviceProxy,                                                         &aResult,                                                          heap,                                                         NULL, 0,NULL, error); <font color=#0000ff size=2><font color=#0000ff size=2>if</font></font> (SUCCEEDED(hr)) {      wprintf(L<font color=#a31515 size=2><font color=#a31515 size=2>“%s\n”</font></font>, aResult); }
<font color=#0000ff size=2><font color=#0000ff size=2>else</font></font> {     wprintf(L<font color=#a31515 size=2><font color=#a31515 size=2>“failed\n”</font></font>); } <font color=#0000ff size=2><font color=#0000ff size=2>
if</font></font> (serviceProxy != NULL) {      WsCloseServiceProxy(serviceProxy, NULL, error);     WsFreeServiceProxy(serviceProxy);     serviceProxy = NULL; } <font color=#0000ff size=2><font color=#0000ff size=2>if</font></font> (heap != NULL) {     WsFreeHeap(heap);     heap = NULL; } <font color=#0000ff size=2><font color=#0000ff size=2>if</font></font> (error != NULL) {     WsFreeError(error);     error = NULL; } <font color=#0000ff size=2><font color=#0000ff size=2>return</font></font> 0;

}</div> <p mce_keep="true">This console app ran and returned the expected ‘Hello World’!</p> <p mce_keep="true">Let me know if you found this useful!</p>

</font>

<< Go Back