BSTR 型是WHAT,有什么细节与众不同? BSTR 型是WHAT,有什么细节与众不同? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一般情况下就是宽字符串,就是LPWSTR 不过在定义 OLEANSI 宏的时候就成了 LPSTR 当创建 BSTR 并在 COM 对象之间传递它们时,必须小心地处理它们所使用的内存以避免内存泄漏。当 BSTR 停留在接口中时,在完成其使用后必须释放出它的内存。但是,如果 BSTR 传递出了接口,那么接收对象将负责它的内存管理。一般情况下,分配和释放分配给 BSTR 的内存的规则如下: 当调用进来一个需要 BSTR 参数的函数时,必须在调用之前为 BSTR 分配内存,并且在完成操作之后将其释放。例如: HRESULT IWebBrowser2::put_StatusText( BSTR bstr );// shows using the Win32 function // to allocate memory for the string: BSTR bstrStatus = ::SysAllocString( L"Some text" );if (bstrStatus == NULL) return E_OUTOFMEMORY;pBrowser->put_StatusText( bstrStatus );// Free the string:::SysFreeString( bstrStatus );//...当调用进来一个返回 BSTR 的函数时,必须自己来释放字符串。例如: HRESULT IWebBrowser2::get_StatusText( BSTR FAR* pbstr ); //...BSTR bstrStatus;pBrowser->get_StatusText( &bstrStatus );// shows using the Win32 function // to freee the memory for the string: ::SysFreeString( bstrStatus );当实现返回 BSTR 的函数时,请分配字符串,但不要释放它。接收函数会释放内存。例如: // Example shows using MFC's // CString::AllocSysString//...HRESULT CMyClass::get_StatusText( BSTR * pbstr ){ try { //m_str is a CString in your class *pbstr = m_str.AllocSysString( ); } catch (...) { return E_OUTOFMEMORY; }// The client is now responsible for freeing pbstr.return( S_OK );}//... CSerial类 在哪里可以下载到? SEH: try-except 和 try-finally 嵌套問題 求:VC编写的聊天室程序,下载地址也可以!谢谢各位! ado 调用存储过程不能得到记录集 树视的消息响应? 关于内存作图重叠问题 请问:如何读取当前ie窗口的网页源文件,并进行分析? 关于动态分配! ocx返回int型数组 VC中如何调用 LabView产生的dll 下午2点结帖: 硕士生惭愧请教: 在不卸载Adobet前提下,如何取消IE或Netscape直接打开PDF文件的关联?只想让它save as. 谁能告诉我?
HRESULT IWebBrowser2::put_StatusText( BSTR bstr );// shows using the Win32 function
// to allocate memory for the string:
BSTR bstrStatus = ::SysAllocString( L"Some text" );
if (bstrStatus == NULL)
return E_OUTOFMEMORY;pBrowser->put_StatusText( bstrStatus );
// Free the string:
::SysFreeString( bstrStatus );
//...
当调用进来一个返回 BSTR 的函数时,必须自己来释放字符串。例如:
HRESULT IWebBrowser2::get_StatusText( BSTR FAR* pbstr );
//...
BSTR bstrStatus;
pBrowser->get_StatusText( &bstrStatus );// shows using the Win32 function
// to freee the memory for the string:
::SysFreeString( bstrStatus );
当实现返回 BSTR 的函数时,请分配字符串,但不要释放它。接收函数会释放内存。例如:
// Example shows using MFC's
// CString::AllocSysString//...
HRESULT CMyClass::get_StatusText( BSTR * pbstr )
{ try
{
//m_str is a CString in your class
*pbstr = m_str.AllocSysString( );
}
catch (...)
{
return E_OUTOFMEMORY;
}// The client is now responsible for freeing pbstr.
return( S_OK );
}
//...