C++MFC中CString转为BSTR

    xiaoxiao2025-08-11  4

    ↓↓↓↓↓↓↓↓↓想知道方法的直接看最后↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    博主在做MFC的时候,需要从配置文件读取字符串,存为CString,因为用的CDHTMLDialog,显示时需转换为BSTR类型,在查阅资料时,发现几乎所有答案都是如下的方式:

    CString str("This is a test"); BSTR bstrText = str.AllocSysString(); … SysFreeString(bstrText); // 用完释放 我根据这个方法用在我的程序里,方法如下:

    app = ((MainApp *)AfxGetApp()); CString str = app->phone; BSTR bstrText = str.AllocSysString(); SysFreeString(<span style="font-family: Arial, Helvetica, sans-serif;">bstrText</span>);

    奇怪的是,我用AllocSysString()转换得到的结果始终是空,而当我使用 CString str("This is a test");

    定义CSstring时能获取到值,目前还没找到问题的原因,还望哪位大神帮忙指出。

    但是既然有问题就要解决,我采用了另一种方法成功的进行了转换:

    BSTR phone = ::SysAllocString(app->phone); //在定义BSTR时直接用CString进行赋值 //Do something. SysFreeString(phone); //使用完进行释放

    转载请注明原文地址: https://ju.6miu.com/read-1301638.html
    最新回复(0)