大家请看下面一段代码:
CString str;
BROWSEINFO bi;
char path[MAX_PATH];
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner = GetSafeHwnd();
bi.pszDisplayName = path;
bi.lpszTitle = _T("请选择GDC2000系统路径...");
bi.ulFlags = BIF_EDITBOX;
LPITEMIDLIST idl = SHBrowseForFolder(&bi);
if(idl == NULL)
return;
SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH));
str.ReleaseBuffer();
m_dir = str;
CString a = str.GetAt(str.GetLength()-1);
if(a != "\\")
m_dir += "\\";
UpdateData(FALSE);
在VC6.0中编译没有一点问题,但在VS2005中不能通过,具体错误为:
错误 1 error C2440: '=' : cannot convert from 'char [260]' to 'LPWSTR'(是指“bi.pszDisplayName = path”这一句)
错误 2 error C2440: 'initializing' : cannot convert from 'wchar_t' to 'ATL::CStringT<BaseType,StringTraits>'(是指“CString a = str.GetAt(str.GetLength()-1)”这一句)
  请问如何转换char类型为LPWSTR类型,以及char *类型为LPCWSTR类型?
  这二条错误怎么解决?

解决方案 »

  1.   

    Method 1:
    Modify project setting -> not use Unicode
    Method 2:
    TCHAR path[MAX_PATH];

    TCHAR a = str.GetAt(str.GetLength()-1);
    if(a != _T("\\"))
    m_dir += _T("\\");
      

  2.   

    to pkrobbie(pkrobbie):  按照您Method 1的方法,把Unicode支持去掉,第一个错误不见了,然后按Method 2的方法,改了之后,◆if(a != _T("\\"))◆不能通过,错误为:
    错误 1 error C2446: '!=' : no conversion from 'const char *' to 'int'
    错误 2 error C2040: '!=' : 'int' differs in levels of indirection from 'const char [2]'
      

  3.   

    我已经改了为以下代码,同时打开unicode支持,编译可以通过了
      
        TCHAR path[MAX_PATH];
        bi.lpszTitle=_T("请选择查找目录:");
        v_SourceDIR=CString(path);  谢谢pkrobbie(pkrobbie)!
      
      编译问题是解决了,但对于char类型转换为LPWSTR类型,以及char *类型转换为LPCWSTR类型还不是明白,请各位大哥指点一下,谢谢!
      

  4.   

    这是MSDN中的一段
    CString theString( "This is a test" );
    LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
    _tcscpy(lpsz, theString);