请教各位前辈UNICODE下CString转换为LPCWSTR,试过网上的方式,要么编译无法通过,要么就依然还是乱码。VS2008(VC++),实现的基本代码如下:LPCWSTR func()
{
   CString s1 =_T("err.c,");
   CString s2 =_T("**** time out");
   
   s1 +=s2;
   return s1;   //返回的总是乱码
}
请各位帮忙解决,谢谢!

解决方案 »

  1.   

    s1,s2是局部变量,返回以后就被释放了,LPCWSTR指针访问的时候数据已经不存在了,你需要放到堆上
      

  2.   


    CString str = _T("123");
    LPCWSTR lpcwstr = str;直接就行,主要是LPCWSTR是一个指针,指向地内存为UNICODE字符串。
    但你程序中函数返回的是s1内部保存UNICODE字符串的地址,没有错,但是函数一返回,s1的生命周期也就结束了,它原本保存字符串的内存就可能被别的什么占用,重新写入值了。
      

  3.   

    最好不要返回LPCWSTR指针类型,返回CString行吧?
    如果非要返回LPCWSTR,那就这样:LPCWSTR func1()
    {
    CString str = _T("123");
    LPCWSTR lpcwstr = str.AllocSysString();
    return lpcwstr;
    }调用:LPCWSTR lpcwstr = func1();
    ...
    ::SysFreeString((unsigned short *)lpcwstr);
      

  4.   

    LPCWSTR str = ws.operator LPCWSTR();
      

  5.   

    第一你如果真要用 LPCWSTR 當 RETURN
    你的宣告本身就是有問題,依使用者控制資源的原則,正常應是 int func( LPCWSTR pBuffer ,int maxlen )
    要度回傳string 長度就夠了,而不是回傳一個stack point 才對如果真要如此那也只能先用 malloc 先配好string 大小再傳回去

    LPCWSTR func()
    {
       LPCWSTR pBUFF;
       CString s1 =_T("err.c,");
       CString s2 =_T("**** time out");
        
       s1 +=s2;
       if( (pBUFF = new WCHAR[s1.Length()+1]) )
       {      
           strcpy(pBUFF,s1);
           pBUFF[s1.Length()] = WCHAR('\0');
           return pBUFF;
       }
       return NULL;
    }
      

  6.   

    不要误导了人,函数申明返回LPCWSTR,而实际返回的是CString,隐含了自动转换的。
    CString的长度是这样得到的CString::GetLength(),而且,UNICODE末尾是两个0。你以为4楼的做法如何呢?
      

  7.   

    4楼 的用方是用 BSTR ,要 free 要用 COM 的方式尺 free 如果你是準備用在 OLE/COM 當然是可以
    但是以通用來看 如果是 c++ nwe/delete ,C malloc/free 才是正確
    而WCHAR('\0'); 是 unicode 是 2 個 byte 的 0
      

  8.   

    有一點補充一下在 VC 中 LPWSTR 和 LPOLESTR 和 BSTR 都是 unicode 所以都可以視為 LPWSTR
    但其實真正的 BSTR 不一定是非 unicode 不可就算是 unicode 也不一定是 c type string format
    ,也有可以是 pascal type string format ,VB 的就是一例
      

  9.   

    LZ:这个问题这段时间我也是经常遇到,还好我们都解决了,没报错显示也没有乱码。很OK的!我先给你一个类型之间得转换代码,你看下先,不行得话在具体问题具体讨论。
    CString,char*,const char *,LPCTSTR 的转换如何将CString类型的变量赋给char*类型的变量
    1、GetBuffer函数:
    使用CString::GetBuffer函数。
    char *p;
    CString str="hello";
    p=str.GetBuffer(str.GetLength());
    str.ReleaseBuffer();
    将CString转换成char * 时
    CString str("aaaaaaa");
    strcpy(str.GetBuffer(10),"aa");
    str.ReleaseBuffer();
    当我们需要字符数组时调用GetBuffer(int n),其中n为我们需要的字符数组的长度.使用完成后一定要马上调用ReleaseBuffer();
    还有很重要的一点就是,在能使用const char *的地方,就不要使用char *
    2、memcpy: 
    CString mCS=_T("cxl");
    char mch[20];
    memcpy(mch,mCS,20);
    3、用LPCTSTR强制转换: 尽量不使用
    char *ch;
    CString str;
    ch=(LPSTR)(LPCTSTR)str;
    CString str = "good";
    char *tmp;
    sprintf(tmp,"%s",(LPTSTR)(LPCTSTR)str);
    4、
    CString Msg;
    Msg=Msg+"abc";
    LPTSTR lpsz;
    lpsz = new TCHAR[Msg.GetLength()+1];
    _tcscpy(lpsz, Msg);
    char * psz;
    strcpy(psz,lpsz);CString类向const char *转换
    char a[100];
    CString str("aaaaaa");
    strncpy(a,(LPCTSTR)str,sizeof(a));
    或者如下:
    strncpy(a,str,sizeof(a));
    以上两种用法都是正确地. 因为strncpy的第二个参数类型为const char *.所以编译器会自动将CString类转换成const char *.
    CString转LPCTSTR (const char *)
    CString cStr;
    const char *lpctStr=(LPCTSTR)cStr;
    LPCTSTR转CString
    LPCTSTR lpctStr;
    CString cStr=lpctStr;
    将char*类型的变量赋给CString型的变量
    可以直接赋值,如: 
    CString myString = "This is a test";
    也可以利用构造函数,如: 
    CString s1("Tom");
    将CString类型的变量赋给char []类型(字符串)的变量
    1、sprintf()函数
    CString str = "good";
    char tmp[200] ;
    sprintf(tmp, "%s",(LPCSTR)str);  
    (LPCSTR)str这种强制转换相当于(LPTSTR)(LPCTSTR)str
    CString类的变量需要转换为(char*)的时,使用(LPTSTR)(LPCTSTR)str
    然而,LPCTSTR是const char *,也就是说,得到的字符串是不可写的!将其强制转换成LPTSTR去掉const,是极为危险的!
    一不留神就会完蛋!要得到char *,应该用GetBuffer()或GetBufferSetLength(),用完后再调用ReleaseBuffer()。
    2、strcpy()函数
    CString str;
    char c[256];
    strcpy(c, str);
    char mychar[1024];
    CString source="Hello";
    strcpy((char*)&mychar,(LPCTSTR)source);
      

  10.   

    CStringW str = L"哈哈";
    LPCWSTR ws = str.operator LPCWSTR();
    wprintf_s(L"%s",ws);
    我就是这么用的,有什么错。不试试别乱说。
      

  11.   

    我没说这个用法有错,而是你的这个用法与楼主的直接返回CString是一用的。
    你明白我的意思?
    LPCWSTR ws = str.operator LPCWSTR();
    那你试试这样行不行呢:
    LPCWSTR ws = str;
    如果可行,那这两种用法等同,与楼主那样用有什么区别?能解决楼主问题?
      

  12.   

    void func(CString &s1)
    {
       CString s2 =_T("**** time out");
       s1 +=s2;
    }
      

  13.   

    1.CString不是有GetBuffer的么?
    2.接着你要什么类型的自己转#include <atlconv.h>USES_CONVERSION然后调用
    A2W  ANSI转Unicode
    W2A  Unicode转ANSI屡试不爽