CComBSTR.WriteToStream问题,test一下结果中断了。

massice 2010-09-04 08:36:04
CComBSTR的WriteToStream方法将CComBSTR的内容写到流中,自己打算实践一下,代码如下:

int main()
{
cout << "Begin Test." << endl;
cout << "********************" << endl;

CComBSTR ccombstrTest(_T("this is test."));
string strPath = "D:\\Projects_VC_2005\\bstrfile.txt";
ifstream outfile(strPath.c_str());
if (!outfile)
{
cout << "file error." << endl;
return -1;
}
IStream* pStream = (IStream*)(&outfile);
ccombstrTest.WriteToStream(pStream); //执行到这里中断了

outfile.close();

cout << "********************" << endl;
cout << "End Test." << endl;

return 0;
}

哪位帮看看!!
...全文
226 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
massice 2010-09-07
  • 打赏
  • 举报
回复
多谢指导!
看来需要好好看看这方面的东西了。
logiciel 2010-09-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 massice 的回复:]
SHCreateStreamOnFile 打开的流指针如何显示的释放?
[/Quote]

CComPtr<IStream> spStream;
SHCreateStreamOnFile(..., &spStream);
// Do something with the stream
spStream.Release(); // Important: .Release, not ->Release

来源:http://social.msdn.microsoft.com/Forums/en-US/vcmfcatl/thread/8863657f-b4b2-4491-b667-26df4897e134
pengzhixi 2010-09-05
  • 打赏
  • 举报
回复
强制转换后通过编译和能否得到你想要的效果是两码事。
massice 2010-09-05
  • 打赏
  • 举报
回复
问题1: 为何用ifstream定义输出文件outfile?
CComBSTR中函数原型如下:
HRESULT WriteToStream(__inout IStream* pStream)
需要IStream*参数,ifstream继承了IStream啊。

问题2: IStream* pStream = (IStream*)(&outfile);//这样转换能行吗?
这个我也不确定,因为不强转的话都编译不过去。

刚刚学习CComBSTR,看到这个写文件的方法,想试试。请问,一般CComBSTR写入文件都是要先使用SHCreateStreamOnFileA方法来打开一个文件流么?
logiciel 2010-09-05
  • 打赏
  • 举报
回复
上面漏了一个(,应是:

CComBSTR ccombstrTest(_T("this is test."));
logiciel 2010-09-05
  • 打赏
  • 举报
回复
问题1: 为何用ifstream定义输出文件outfile?
问题2: IStream* pStream = (IStream*)(&outfile);//这样转换能行吗?

下面是我做的一个例子:
#include <atlbase.h>
#include <iostream>
using namespace std;

int main()
{
cout << "Begin Test." << endl;
cout << "********************" << endl;

CComBSTR ccombstrTest(_T"this is test."));
CoInitialize(NULL);
CComPtr<IStream> pStream = NULL;
SHCreateStreamOnFileA("D:\\bstrfile.txt", STGM_CREATE | STGM_WRITE | STGM_DIRECT | STGM_SHARE_EXCLUSIVE, &pStream);
ccombstrTest.WriteToStream(pStream);

cout << "********************" << endl;
cout << "End Test." << endl;

return 0;
}

massice 2010-09-05
  • 打赏
  • 举报
回复
SHCreateStreamOnFile 打开的流指针如何显示的释放?
massice 2010-09-05
  • 打赏
  • 举报
回复
如何不使用windows api,使用ifstream来实现写入文件呢?

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧