void GetXPathExprValue(CComQIPtr<IXMLDOMDocument2>  &spResponseXMLDoc, 
   LPCTSTR lpszXPathExpr, 
   LPSTR lpszResultValue)
{
USES_CONVERSION; CComPtr <IXMLDOMNode> spResultNode;
spResultNode = spResponseXMLDoc->selectSingleNode(_bstr_t(lpszXPathExpr));
if(spResultNode.p != NULL)
_tcscpy((wchar_t *)(wchar_t)lpszResultValue, (wchar_t *)(wchar_t)W2A(_bstr_t(spResultNode->nodeTypedValue))); 
}
红色标记部分进行了强制类型转换,无错。但是用(wchar_t *)lpszResultValue进行转换,出错can't convert from 'LPSTR' to 'const wchar_t *'。
为什么上面转换两次的没错?而用(wchar_t *)lpszResultValue怎么就不能转换呢?

解决方案 »

  1.   

    LPSTR是char*,等于先转换成wchar的字符,然后再转成wchar_t*,应该先转换成宽字符串
    (wchar_t *)((CA2W)lpszResultValue)
      

  2.   

    我也感觉奇怪,你不是由char*转wchar_t*么,怎么报的错误是const wchar_t* ?(wchar_t *)(wchar_t)lpszResultValue这样转换虽然编译没有报错,但是使用中会有错误,首先转成(wchar_t),而wchar_t是2个字节,一个指针是4个字节,这样造成了指针的截断。再说,你都用W2A了,已经是const char*了,干吗要用_tcscpy(你是unicode工程,对应wcscpy)?直接用strcpy(lpszResultValue, W2A(_bstr_t(spResultNode->nodeTypedValue)));不就行了?
      

  3.   

    2楼说的对,无需转得那么辛苦,直接用strcpy就是了。另外,真的很奇怪。(wchar_t *)lpszResultValue 怎么看也是convert from 'LPSTR' to 'wchar_t *'吧,怎么会是 'const wchar_t *' 的呢。