开发者

What happens when "file_with_invalid_chars:<?>.txt" is passed to the MFC CFile class?

开发者 https://www.devze.com 2023-02-21 23:02 出处:网络
Note: Needed to edit the title and question, as initially I thought the problem is with ::CreateFile[W] - but it turns out, the error only happens on XP when I use MFC\'s Cfile class.

Note: Needed to edit the title and question, as initially I thought the problem is with ::CreateFile[W] - but it turns out, the error only happens on XP when I use MFC's Cfile class.

I am slightly confused by the behaviour of CFile::Open(...) (on Win7/64bit) when I pass the filename file_with_invalid_chars:<?>.txt to this function to request creation of the file.

On my Windows XP box, the call fails.

开发者_如何学Python

On my Win7/64 box, the call succeeds, creating a file named file_with_invalid_chars.

What's happening?


I'm guessing it's creating an alternate data stream. Seems odd that it would fail on XP, though. The documentation says that characters legal for a file name are also legal for a stream name, though I guess that doesn't necessarily mean the inverse is true. Maybe Windows 7 supports < and > in stream names and Windows XP doesn't.


It turns out that this is due to a bugfix(?) in shlwapi.dll -

MFC's CFile class does some internal processing on the filename, and happens to call the PathStripToRoot Function.

This function resides in Shlwapi.dll and Win7 is shipped with a newer version than Windows XP.

The Version in Windows XP apparently does not work correctly with file-paths containing ADS colon separated stream names.

Example:

  • On XP PathStripToRoot(L"C:\\temp\\file.txt:stream");yields the string C:\temp\file.txt: which is incorrect and leads to an error return from CFile::Open
  • On Win7, the same call yields the actual root C:\.
0

精彩评论

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