【问题标题】:Replacing T2OLE with a ATL 7.0 String conversion classes and Macros用 ATL 7.0 字符串转换类和宏替换 T2OLE
【发布时间】:2014-03-07 06:10:21
【问题描述】:

根据MSDN,在循环中使用 T2OLE 可能会导致堆栈溢出,我的应用程序在循环内的代码中很多地方使用 T2OLE 进行字符串转换。我发现使用ATL 7.0字符串转换类和宏有很多好处,它也解决了堆栈溢出问题,

我尝试使用 ATL 7.0,如下所示,

_bstr_T example("Hello world");
for(i=o; i<10000; i++)
{
 Callsomemethod(i,T2OLE(example)); //This is where I need to replace T2OLE
}
void Callsomemethod(int k, cstring y)
{
....
}

我发现 ATL 7.0 中的 CT2OLE 等同于 T2OLE,但是当我用 CT2OLE 替换 T2OLE 时,我遇到了这个问题

Error: No suitable user defined conversion from "ATL:CA2W" to Cstring exists 

以同样的方式,我在另一个地方将 cstring 转换为 _bstr_t,当我在那里替换时,我得到了这个

 Error: No suitable user defined conversion from "ATL:CA2W" to _bstr_t exists 

谁能帮我解决这个问题?

【问题讨论】:

    标签: c++ visual-c++ atl ole


    【解决方案1】:
    _bstr_t example("Hello world");
    void Callsomemethod(CString y);
    Callsomemethod(T2OLE(example)); //This is where I need to replace T2OLE
    

    CString 假定您正在传递 TCHAR* 兼容参数,而 x2OLE 宏为您提供 WCHAR* - 您正在使用相反方向的转换宏。 CString 和 helper CA2W 类之间可能缺少转换,您需要通过提供转换来提供帮助。

    _bstr_t example("Hello world");
    void Callsomemethod(CString y);
    //void Callsomemethod(LPCTSTR y);
    
    Callsomemethod(CString(example));
    Callsomemethod(CString((BSTR) example));
    Callsomemethod(OLE2CT(example));
    Callsomemethod((LPCTSTR) OLE2CT(example));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多