c++ .net winform 应用程序调用dll(gSOAP调用WebService)获取天气

c++ .net winform 应用程序中没有“添加web引用”的选项来调用webservice,困扰了好久,在参考了http://jingyan.baidu.com/article/295430f1d4de8e0c7e00508f.html 方法的基础上,终于有了解决方案!

1.下载开源的gSOAP。
2.下载天气预报wsdl文件。
保存为.wsdl文件
3.利用gSOAP创建一系列c++调用的文件。
生成.h文件
生成其他文件
生成后的所有文件
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")
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值