c++ .net winform 应用程序中没有“添加web引用”的选项来调用webservice,困扰了好久,在参考了http://jingyan.baidu.com/article/295430f1d4de8e0c7e00508f.html 方法的基础上,终于有了解决方案!
1.下载开源的gSOAP。
2.下载天气预报wsdl文件。
3.利用gSOAP创建一系列c++调用的文件。
4.打开vs2010 ,新建项目(控制台程序)。
5.将第3步生成的所有文件连同gSOAP目录下的两个文件stdsoap2.h,stdsoap2.cpp加入新建的项目中。
6.main.cpp文件中代码如下:
#include "stdafx.h"
#include <stdio.h>
#include "soapH.h"
#include "WeatherWSSoap.nsmap"
#include "soapWeatherWSSoapProxy.h"
using namespace std;
void GB2312_ToUtf8(char* pstrOut,int dwOutLen,const char* pstrIn,int dwInLen)
{
int i = MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, NULL, 0);
wchar_t* strSrc = new wchar_t[i+1];
MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, strSrc, i);
i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, pstrOut, i, NULL, NULL);
delete strSrc;
}
void Utf8ToGb2312(char* pstrOut,int dwOutLen,const char* pstrIn,int dwInLen)
{
if (NULL == pstrOut)
{
return;
}
int i = MultiByteToWideChar(CP_UTF8,0,pstrIn,-1,NULL,0);
wchar_t* strSrc = new wchar_t[i+1];
MultiByteToWideChar(CP_UTF8,0,pstrIn,-1,strSrc,i);
i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(CP_ACP, 0,strSrc,-1,pstrOut,i,NULL,NULL);
delete strSrc;
}
int _tmain(int argc, _TCHAR* argv[])
{
WeatherWSSoapProxy soap(SOAP_C_UTFSTRING);
_ns1__getWeather cityName;
_ns1__getWeatherResponse weatherResponse;
char name[64] = {0};
char name_utf8[64] = {0};
sprintf(name,"2061");
GB2312_ToUtf8(name_utf8,64,name,64);
cityName.theCityCode = name_utf8;
int result = soap.getWeather(NULL,NULL,&cityName,weatherResponse);
if (result == SOAP_OK)
{
ns1__ArrayOfString *aos = weatherResponse.getWeatherResult;
int n = aos->__sizestring;
for (int i = 0; i <n;++i)
{
char out[2048] = {0};
Utf8ToGb2312(out,1024,aos->string[i],strlen(aos->string[i]));
printf("%s\n",out);
}
}
}
7.运行成功如下图:
8.准备开始写dll文件,vs2010中新建win32应用程序。
弹出的应用程序向导中选择DLL。
9. .h文件和cpp文件代码如下:
//.h 文件
#ifndef _WEATHER_H
#define _WEATHER_H
void getweather(char* out);
#endif
//.cpp文件
#include "stdafx.h"
#include <stdio.h>
#include "soapH.h"
#include "WeatherWSSoap.nsmap"
#include "soapWeatherWSSoapProxy.h"
using namespace std;
void GB2312_ToUtf8(char* pstrOut,int dwOutLen,const char* pstrIn,int dwInLen)
{
int i = MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, NULL, 0);
wchar_t* strSrc = new wchar_t[i+1];
MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, strSrc, i);
i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, pstrOut, i, NULL, NULL);
delete strSrc;
}
void Utf8ToGb2312(char* pstrOut,int dwOutLen,const char* pstrIn,int dwInLen)
{
if (NULL == pstrOut)
{
return;
}
int i = MultiByteToWideChar(CP_UTF8,0,pstrIn,-1,NULL,0);
wchar_t* strSrc = new wchar_t[i+1];
MultiByteToWideChar(CP_UTF8,0,pstrIn,-1,strSrc,i);
i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(CP_ACP, 0,strSrc,-1,pstrOut,i,NULL,NULL);
delete strSrc;
}
void getweather(char* out)
{
WeatherWSSoapProxy soap(SOAP_C_UTFSTRING);
_ns1__getWeather cityName;
_ns1__getWeatherResponse weatherResponse;
char name[64] = {0};
char name_utf8[64] = {0};
sprintf(name,"2061");
GB2312_ToUtf8(name_utf8,64,name,64);
cityName.theCityCode = name_utf8;
int result = soap.getWeather(NULL,NULL,&cityName,weatherResponse);
if (result == SOAP_OK)
{
ns1__ArrayOfString *aos = weatherResponse.getWeatherResult;
int n = aos->__sizestring;
for (int i = 0; i <n;++i)
{
char out[2048] = {0};
Utf8ToGb2312(out,1024,aos->string[i],strlen(aos->string[i]));
printf("%s\n",out);
}
}
}
10.右键项目属性,在配置类型中选择静态库(.lib)后,生成项目。
11.将生成的.lib文件和.h文件加入需要调用dll的工程中。
12.加入下列代码即可调用dll
#include "weather.h"
#pragma comment(lib,"weather.lib")