如何关闭使用 SHCreateStreamOnFile 打开的文件句柄
由于我需要使用 MSXML 格式化“漂亮”XML,因此我使用 此处引用的技巧为我的代码添加缩进。由于我只想保存文件,因此我使用 SHCreateStreamOnFile()
打开一个 IStream
。文件已保存,我可以在文本编辑器中打开它,XML 内容就在那里。然后,我在 IStream
接口上调用 Release()
,以便关闭 IStream
和文件句柄。
或者我是这么认为的,但是 Process Explorer 告诉我,一旦我的保存功能退出(尽管已发布),我的进程仍然会引用我的文件。我尝试第二次调用 Release()
,但仍然没有结果,句柄没有关闭。 Google 和 MSDN 能告诉我的就是我应该调用 Release
...但不清楚 Release()
是否仅释放 COM 对象或文件句柄。
那么,有没有一种特殊的方法来强制关闭由SHCreateStreamOnFile()创建的文件句柄呢?或者是否有更好的方法来获取文件上的 IStream
?我是否需要按特定顺序调用 IMXWriter
、ISAXXMLReader
和 IStream
上的 Release()
?
先感谢您。
Since I need to format "pretty" XML using MSXML, I am using the trick referenced here to add indentation to my code. Since all I want is to save a file, I open an IStream
using SHCreateStreamOnFile()
. The file gets saved, I can open it in a text editor and the XML content is there. Then I call Release()
on the IStream
interface so that the IStream
and the file handle get closed.
Or so I tought, however Process Explorer tell me that my process still has a reference to my file once my save function exits (despite the release). I tried calling Release()
a second time, but still no result, the handle doesn't get closed. All Google and MSDN could tell me was that I should call Release
... But it isn't clear if Release()
releases only the COM object or the file handle also.
So, is there a special way to force the close of a file handle created by SHCreateStreamOnFile()
? Or is there a better way to obtain an IStream
on a file? Do I need to call Release()
on the IMXWriter
, ISAXXMLReader
and IStream
in a specific order?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ildjarn 是对的,我将它传递给一个没有被删除的对象,因为我不知道 QueryInterface 增加了引用计数(我认为这是一种转换指针的“COM”方式)。释放该对象同时释放了该文件。
不习惯 COM 编程的初学者所犯的错误。
ildjarn was right, I was passing it to an object that didn't get deleted, because I didn't knew that QueryInterface incremented the reference count ( I tought it was kind of a "COM" way of casting a pointer ). Releasing that object released the file at the same time.
A beginner mistake from someone not used to COM programming.
我以前遇到过这个问题:
你必须等待pXMLDoc和spStream.Release()的析构函数执行; ,文件上的句柄将被关闭。
I met this problem before:
You must wait execution on the destructor of pXMLDoc and spStream.Release(); , the handle on the file will be closed.