用ATL 7.0字符串转换类和宏替换T2OLE

Replacing T2OLE with a ATL 7.0 String conversion classes and Macros

本文关键字:替换 T2OLE 转换 ATL 字符串      更新时间:2023-10-16

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

我试着使用ATL7.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 

以同样的方式,我有另一个地方,它对_bstr_t进行cstring,当我在那里替换时,我得到了这个

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

有人能帮我解决这个问题吗?

_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和helperCA2W类之间可能缺少转换,您需要通过提供强制转换来提供帮助。

_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));