1

I need to consume a web service (not wcf service) from a remote server in my c++ application. And I followed the example given by @Matt Davis in this link. Create WCF service for unmanaged C++ clients i.e. use a c++ dll to bridge the gap between managed c# WCF client and unmanaged c++ application. To test its feasibility, I almost copied the entire code and followed all of the configuration steps, except that the service is a legacy web service provided by a remote server. Below is my bridge.cpp

#include "bridge.h"
#include "Ibridge.h"
#include<string>

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::ServiceModel;
using namespace System::ServiceModel::Channels;

std::string Ibridge::getSupportCity(std::string name)
{   std::string rv;
    Binding^ binding = gcnew WSHttpBinding();
    EndpointAddress^ address = gcnew EndpointAddress(gcnew     String("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"));
    client::ServiceReference1::WeatherWebServiceSoapClient^ client1 = gcnew  client::ServiceReference1::WeatherWebServiceSoapClient(binding, address);
    try {
    String^ message=client1->getSupportCity(gcnew System::String(name.c_str()))[0];
        client1->Close();

        IntPtr ptr = Marshal::StringToHGlobalAnsi(message);
        rv = std::string(reinterpret_cast<char *>(static_cast<void *>(ptr)));
        Marshal::FreeHGlobal(ptr);
    } catch (Exception ^) {
        client1->Abort();
    }
    return rv;

} getSupportCity is one of the methods that returns a String[] in c#, and to simplify the process I return only its first element in method calling. Below is how I called the method in MFC application.

void CMFCappDlg::OnBnClickedButton1()
{
    std::string cities = Ibridge::getSupportCity("ALL");
       AfxMessageBox(CString(cities.c_str()));
}

I'm pretty sure that when running the code, exception 0xE0434352 is thrown in the first line of the "try" section (i.e. when getSupportCity is called) and the code jumps right to the "catch" part.

My programming environment is win7 64bit,vs2010 and .NET 4.0. I'm very new to this area and would really appreciate your suggestions.

Community
  • 1
  • 1
luerpia
  • 11
  • 1

0 Answers0