How to Consume WebService via unmanaged C++

Someone asked me how to consume webservice using unmanaged c++ client.

1. Open your C++ project in Visual Studio 2003/2005
2. Right click on the project name in the solution explorer and add reference to the web service
    This operation will create the web service proxy and add if to your c++ solution.
3. Call the web service
    Assuming that you want to consume the basic HelloWorld webservice these are the lines that you need to add to your C++ app:

Service::CService proxy;
BSTR Result=NULL;
HRESULT hr = proxy.HelloWorld(&Result);
MessageBox(0,Result,CComBSTR("Hello World Result"),0);

5 Comments

  • That's great - but how can you set the proxy to point at runtime to a different endpoint than the one defined in the WSDL?

  • Use the real server name when you add the web reference, then on your local machine (to be able to point to a different environment) edit your hosts file in c:\windows\system32\drivers\etc\hosts to override the ip address of the web service.

  • But is there any method to access an ASP.NET web service from a Visual C++ 6.0 project?

    Thanks in advance.

  • Nice and fine, in this way we can work with MFC and if we add managed caps to the project we can also add web services references. It is cool, thanks for the perspective.

  • Unfortunately it's not so simple. In fact without adding the /clr option the communication with the ws does'nt work. Moreover with the code posted, the stub of the ws is'nt intialized with new and so the program crashes...

Comments have been disabled for this content.