1

我需要在我的 c++ 应用程序中从远程服务器使用 Web 服务(不是 wcf 服务)。我遵循了@Matt Davis 在此链接中给出的示例。 为非托管 C++ 客户端创建 WCF 服务, 即使用 c++ dll 来弥合托管 c# WCF 客户端和非托管 c++ 应用程序之间的差距。为了测试它的可行性,我几乎复制了整个代码并遵循了所有的配置步骤,除了该服务是由远程服务器提供的遗留 Web 服务。下面是我的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 是在 c# 中返回 String[] 的方法之一,为了简化过程,我在方法调用中只返回它的第一个元素。下面是我如何在 MFC 应用程序中调用该方法。

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

我很确定在运行代码时,“try”部分的第一行(即调用 getSupportCity 时)会引发异常 0xE0434352,并且代码会直接跳转到“catch”部分。

我的编程环境是win7 64bit,vs2010和.NET 4.0。我对这个领域很陌生,非常感谢你的建议。

4

0 回答 0