如何关闭使用 SHCreateStreamOnFile 打开的文件句柄

发布于 2024-11-11 04:24:47 字数 811 浏览 1 评论 0原文

由于我需要使用 MSXML 格式化“漂亮”XML,因此我使用 此处引用的技巧为我的代码添加缩进。由于我只想保存文件,因此我使用 SHCreateStreamOnFile() 打开一个 IStream。文件已保存,我可以在文本编辑器中打开它,XML 内容就在那里。然后,我在 IStream 接口上调用 Release(),以便关闭 IStream 和文件句柄。

或者我是这么认为的,但是 Process Explorer 告诉我,一旦我的保存功能退出(尽管已发布),我的进程仍然会引用我的文件。我尝试第二次调用 Release(),但仍然没有结果,句柄没有关闭。 Google 和 MSDN 能告诉我的就是我应该调用 Release...但不清楚 Release() 是否仅释放 COM 对象或文件句柄。

那么,有没有一种特殊的方法来强制关闭由SHCreateStreamOnFile()创建的文件句柄呢?或者是否有更好的方法来获取文件上的 IStream ?我是否需要按特定顺序调用 IMXWriterISAXXMLReaderIStream 上的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风也等你 2024-11-18 04:24:47

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.

唱一曲作罢 2024-11-18 04:24:47

我以前遇到过这个问题:

CComPtr<IStream> spStream;
HRESULT hr = SHCreateStreamOnFileEx(L"my.xml",
    STGM_READWRITE | STGM_SHARE_DENY_WRITE | STGM_FAILIFTHERE,
    FILE_ATTRIBUTE_NORMAL|FILE_ATTRIBUTE_HIDDEN,
    TRUE, NULL, &spStream);

SOMSXML::IXMLDOMDocumentPtr pXMLDoc;
hr = pXMLDoc.CreateInstance(__uuidof(SOMSXML::DOMDocument));
pXMLDoc->async = false;
hr = pXMLDoc->load(spStream.p);

你必须等待pXMLDoc和spStream.Release()的析构函数执行; ,文件上的句柄将被关闭。

I met this problem before:

CComPtr<IStream> spStream;
HRESULT hr = SHCreateStreamOnFileEx(L"my.xml",
    STGM_READWRITE | STGM_SHARE_DENY_WRITE | STGM_FAILIFTHERE,
    FILE_ATTRIBUTE_NORMAL|FILE_ATTRIBUTE_HIDDEN,
    TRUE, NULL, &spStream);

SOMSXML::IXMLDOMDocumentPtr pXMLDoc;
hr = pXMLDoc.CreateInstance(__uuidof(SOMSXML::DOMDocument));
pXMLDoc->async = false;
hr = pXMLDoc->load(spStream.p);

You must wait execution on the destructor of pXMLDoc and spStream.Release(); , the handle on the file will be closed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文