C++和C# WebService相互调用

C#调用C++ gSOAP:
调用http://blog.csdn.net/ggz631047367/article/details/44567411的服务http://127.0.0.1:8089/

1.右键添加web服务引用,输入http://127.0.0.1:8089/
2.调用:
ServiceReference1.calcPortTypeClient service = new ServiceReference1.calcPortTypeClient();
label1.Text = service.add(1, 2).ToString();

C++ gSOAP调用C# WebService:
1.生成头文件:wsdl2h.exe -s -t typemap.dat -o hello.h http://127.0.0.1:8083/WebService1.asmx?wsdl
使用定义模板typemap.dat,typemap.dat是GSOAP自带的复制过来增加了以下内容xsd__string = | wchar_t*
因为默认GSOAP生成的C++代码都是ANSI的,而C#默认是用UTF的,所以最好转换成wchar_t这样兼容性好些,避免中文乱码。
2.生成其他文件:soapcpp2.exe -i -I C:UsersHunterDownloadsgsoap_2.8.21gsoap-2.8gsoapimport hello.h
3.将文件引用到项目:
这里写图片描述
4.test.cpp代码:

#include"soapWebService1SoapProxy.h"
#include"WebService1Soap.nsmap"
#include<iostream>

using namespace std;

int main()
{
    WebService1SoapProxy p;
    _ns1__HelloWorld nsh;
    _ns1__HelloWorldResponse nsr;
    p.HelloWorld(&nsh, nsr);
    cout << nsr.HelloWorldResult << endl;

    _ns1__add nsa ;
    _ns1__addResponse nar;
    nsa.a = 1; //设置形参
    nsa.b = 10;
    p.add(&nsa, nar);

    cout << nar.addResult << endl;

    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/ggzone/p/4429916.html