c++ .net winform 应用程序调用dll(gSOAP调用WebService)获取天气
来源:互联网 发布:java监听器原理 编辑:程序博客网 时间:2024/05/16 13:59
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_Hvoid 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")
0 0
- c++ .net winform 应用程序调用dll(gSOAP调用WebService)获取天气
- 利用C++调用天气webservice-gSOAP方法
- gsoap(C/C++调用webservice)
- gsoap(C/C++调用webservice)
- gsoap(C/C++调用webservice)
- gsoap(C/C++调用webservice)
- gsoap(C/C++调用webservice)
- winform(C#)调用dll
- winform(C#)调用dll
- c/c++ 使用gsoap调用webservice
- Qt+gsoap调用WebService
- [gSOAP]MFC调用WEBService
- Qt+gsoap调用WebService
- Qt+gsoap调用WebService
- Qt+gsoap调用WebService
- gsoap的使用(调用webservice)
- C++调用gSoap编写的WEBSERVICE与C#.NET间接口自定义结构体不能重复使用
- C/C++通过gSoap发布webservice接口,供ASP.Net网站工程调用
- 基于BrightBeaconSDK的iBeacon开发总结
- REST无状态的理解
- Java-发送邮件(附件、图片)---(五)实例
- html5入门(基于HTML4)
- Nginx源代码分析之accept(十)
- c++ .net winform 应用程序调用dll(gSOAP调用WebService)获取天气
- SPOJ 1825 Free tour II
- Search for a Range
- Android UI性能优化实战 识别绘制中的性能问题
- 【译】Unity3D Shader 新手教程(1/6)
- poj 2993 Emag eht htiw Em Pleh 暑假第11题 模拟 大水
- JS检查浏览器类型和版本
- 多重继承
- 十道海量数据处理面试题与十个方法大总结