采用gSOAP+VC开发的一个DLL,该程序从其他程序中接收一组参数值,将至条用Web Service(WS)接口传给WS服务器……
这里面参数值有中文,传送过去乱码。DLL运行在Windows环境中。
不知道如何判断从其他程序里接收到的中文是什么编码格式;
通过gSOAP从wsdl文件生成的SOAP文件中发现,其采用的是iso-8859-1或utf-8。是否有方式在DLL中将获取到的中文值转换成UTF8编码?如何做到呀?
谢谢。

解决方案 »

  1.   

    以下body.name是中文。
    // telpay.cpp : Defines the entry point for the DLL application: PhonePaymentService.wsdl.
    // It is a program for the WSDL - Web Service linking.#include <afx.h>
    #include "stdafx.h"
    #include "soapH.h"
    #include "PhonePaymentServiceHttpBinding.nsmap"
    #include "soapPhonePaymentServiceHttpBindingProxy.h"
    #include<stdio.h>BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
     )
    {
        return TRUE;
    }extern "C" _declspec(dllexport) int SendParamter( char *IDNum, char *name )
    ////extern "C" _declspec(dllexport) int SendParamter(char *cardNo, char *validDate, char *cvv2 )
    {

    ns2__MBFRequestHeader header;  //定义的header
    ns3__sendPaymentInfoRequestBody body; //定义的body
        ns3__sendPaymentInfoRequest request; //定义的request
    ns3__sendPaymentInfoResponse response;//定义的response
        _ns3__sendPaymentInfo soapinput;// 定义的输入
        _ns3__sendPaymentInfoResponse soapoutput;// 定义的输出 //初始化soap
    struct soap soap;
    soap_init ( &soap );

    const char* server = "http://192.168.100.53:9106/SuNingServiceWeb/mb"

    //给输入变量赋值
    header.MBServiceCode = "PHONE_PAY_SENDPAYMENTINFO";
    body.IDNum = IDNum;
    body.name = name; request.MbfHeader = &header;
    request.MbfBody = &body;

    soapinput.input1 = &request;
        
    char *flag = new char[256]; //soap输出的值
    //调用Webservice
    if(soap_call___ns1__sendPaymentInfo(&soap,server,NULL,&soapinput,&soapoutput) == 0)
    {
    response = *soapoutput.output1;
    strcpy(flag,response.flag);

    return atoi(flag);
    }
    else
    {
    soap_print_fault(&soap, stderr); // display the SOAP fault message on the stderr stream 
    return 2;
    // exit(1);
    } //收尾
    soap_destroy(&soap); // delete deserialized class instances  
    soap_end(&soap); // remove deserialized data and clean up 
    soap_done(&soap); // detach the gSOAP environment 

    delete []flag;
    return 0;
    }
      

  2.   

    给你一个转换函数Convert( CString("example"), CP_ACP,  CP_UTF8 );CString   Convert(CString   str,   int   sourceCodepage,   int   targetCodepage)  
      {  
      int   len=str.GetLength();  
       
      int   unicodeLen=MultiByteToWideChar(sourceCodepage,0,str,-1,NULL,0);  
       
      wchar_t *   pUnicode;  
      pUnicode=new   wchar_t[unicodeLen+1];  
       
      memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));  
       
       
      MultiByteToWideChar(sourceCodepage,0,str,-1,(LPWSTR)pUnicode,unicodeLen);  
       
      BYTE   *   pTargetData;  
      int   targetLen=WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char   *)pTargetData,0,NULL,NULL);  
       
      pTargetData=new   BYTE[targetLen+1];  
      memset(pTargetData,0,targetLen+1);  
       
      WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char   *)pTargetData,targetLen,NULL,NULL);  
       
      CString   rt;  
      rt.Format("%s",pTargetData);  
       
      delete   []pUnicode;  
      delete   []pTargetData;  
      return   rt;  
       
      }   
      

  3.   

    body.name = Convert( name, CP_ACP,  CP_UTF8 ); ;  
      

  4.   

    CString与C不兼容呢。
    告警
    warning C4190: '<Unknown>' has C-linkage specified, but returns UDT 'CString' which is incompatible with C
    而extern"C"是外部传参数程序要求的优化举措。:(
      

  5.   

    你的不是 MFC 程序? 如果不是 ,就改成返回char* 了
    另外,你把这个函数作为dll 的内部函数, 不要 export
      

  6.   

    将之放在“extern "C" _declspec(dllexport)”之前 int Convert(char *str, int sourceCodepage, int targetCodepage)  
    {  
    int  len=str.GetLength();  
    int  unicodeLen=MultiByteToWideChar(sourceCodepage,0,str,-1,NULL,0);  
    wchar_t *  pUnicode;  
    pUnicode=new  wchar_t[unicodeLen+1];   memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));   MultiByteToWideChar(sourceCodepage,0,str,-1,(LPWSTR)pUnicode,unicodeLen);   BYTE  *  pTargetData;  
    int  targetLen=WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char  *)pTargetData,0,NULL,NULL);   pTargetData=new  BYTE[targetLen+1];  
    memset(pTargetData,0,targetLen+1);   WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char  *)pTargetData,targetLen,NULL,NULL);   char rt;  
    rt.Format("%s",pTargetData);   delete  []pUnicode;  
    delete  []pTargetData;  
    return  rt;  
    }  extern "C" _declspec(dllexport) int SendParamter( char *IDNum, char *name )编译 D:\aaa\ZY_code\PhonePayment\PPcode\payment.cpp(34) : error C2228: left of '.GetLength' must have class/struct/union type
    D:\aaa\ZY_code\PhonePayment\PPcode\payment.cpp(52) : error C2228: left of '.Format' must have class/struct/union type
    D:\aaa\ZY_code\PhonePayment\PPcode\payment.cpp(92) : error C2440: '=' : cannot convert from 'int' to 'char *'请问如何修改呀。菜菜麻烦方家了。str和rt都定义过呀……
      

  7.   


    帮你改了一下//
    BYTE *pBuffer = NULL;
    int nLength = 0;
    Convert( "12345你好fg", strlen("12345你好fg"), pBuffer, &nLength,CP_ACP,  CP_UTF8 ); // 先传递一个空指针进去, 然函数返回所需的缓冲区大小
    pBuffer = new BYTE[nLength]; // 根据返回的 nLength 分配内存
    Convert( "12345你好fg", strlen("12345你好fg"), pBuffer, &nLength,CP_ACP,  CP_UTF8 ); // pBuffer中就是 UTF-8了
    //
      

  8.   


    HRESULT Convert(char *strSource, int nSourceLength , byte *strTarget, int* pTargetLength, int sourceCodepage, int targetCodepage)  
    {  
    HRESULT hr = E_FAIL;
    int  len= nSourceLength;  
    int  unicodeLen=MultiByteToWideChar(sourceCodepage,0,strSource,-1,NULL,0);  
    wchar_t *  pUnicode;  
    pUnicode=new  wchar_t[unicodeLen+1];   memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));   MultiByteToWideChar(sourceCodepage,0, strSource,-1,(LPWSTR)pUnicode,unicodeLen);   int  targetLen=WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char  *)strTarget,0,NULL,NULL);  
    if ( strTarget != NULL && ( targetLen + 1 <= *pTargetLength ) )
    {
    memset(strTarget,0,*pTargetLength);  
    WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char  *)strTarget,targetLen,NULL,NULL);  
    hr = S_OK;
    }
    else
    {
    *pTargetLength = targetLen + 1;
    }
    delete  []pUnicode;   return hr;}
      

  9.   

    在应用转换的时候,pBuffer的内容是直接通过memcpy给body.name么?
    能编译通过,但是第三方调用该dll的时候异常退出。怀疑内存操作是不是出了问题了。请方家们帮忙看看    BYTE *pBuffer = NULL;
        int nLength = 0;
    HRESULT hr=Convert( name, strlen(name), pBuffer, &nLength, CP_ACP, CP_UTF8 ); // 先传递一个空指针进去, 然函数返回所需的缓冲区大小
        pBuffer = new BYTE[nLength]; // 根据返回的 nLength 分配内存
        hr = Convert( name, strlen(name), pBuffer, &nLength,CP_ACP, CP_UTF8 ); // pBuffer中就是 UTF-8了
        //
    char *pName = new char[256]; 
    memcpy (&pName, pBuffer, sizeof(pName));
    body.name = pName;
    //......//
    delete []pName;
    delete []pBuffer;
      

  10.   

    char *pName = new char[256]; 
    分配的空间是否足够大
      

  11.   

    LZ既然用了GSOAP为什么不直接用那个proxy类, 只需要加个构造参数就解决了乱码问题.
      

  12.   

    那个构造实际上里面调用的是如下函数
    soap_imode
    soap_omode
    看名字就能猜到了. 祝你好运. 
      

  13.   

    如何使用呀~方家请边解惑,我边百度google之...thx yo.oy