//更改列头CHeaderCtrl文字
STDMETHODIMP CAxTreeView::SetHeaderItem(int nCol, BSTR sText)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here HDITEM hdi = {0};
hdi.mask = HDI_TEXT;
hdi.cchTextMax = 255;
hdi.pszText = LPSTR(sText);     //有问题的地方。可以编译执行,但是设置不了列头
m_TreeView.m_Header.SetItem(nCol, &hdi);

return S_OK;
}c++中字符串这么多,转换的时候也有很多问题。上面的代码,我该怎么把BSTR的sText赋值给LPSTR的pszText?

解决方案 »

  1.   

    搞定了 不过是从网上copy的代码,不知道有没有错===============================================
    // Description:  Function takes a BSTR argument and returns
    // the char* representation of it
    char *bstr2a(const BSTR cmd)
    {
        int n, i;
        char *buf;
        n = SysStringLen(cmd); // length of input
        buf = (char *) malloc(n+3);
        for (i = 0; i < n; i++) // wide to narrow
        { buf[i] = (char) cmd[i];}
        buf[i] = 0;
        return buf;
    }。
    hdi.pszText = bstr2a(sText);
      

  2.   

    强转不行吗?要不先转成CString!第二种搞法内存怎么处理?
      

  3.   

    呵呵因为我不会vc,所以上面的问题是大体小作了。W2T
      

  4.   

    com提供了一个很好的字符串处理类:
    _bstr_t 可以很好的自动实现ANSI和UNICODE之间的转换