开发者

How to close a file handle opened with SHCreateStreamOnFile

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. Th开发者_Python百科en 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜