2 ms·
Wow, that Win32 OpenFile function is truly cursed: - If you don't specify a directory, Windows will look for the file in a bunch of directories, but the curren
by moefh 4y ago
Wow, that Win32 OpenFile function is truly cursed:
- If you don't specify a directory, Windows will look for the file in a bunch of directories, but the current directory is not the first place searched.
- The lpReOpenBuff is annotated as [out], but is an input when uStyle contains the OF_REOPEN flag.
- The function is named OpenFile, but can be used to delete a file. Even better, the documentation is not clear what to do with the return value on success: is it a valid file handle that must be passed to CloseHandle, or just a dummy non-error file handle?
- You can also use it to check the existence of the file -- the documentation says it will open and then close the file. What's the return value in that case? Is it a stale file handle, a dummy, or do you still have to close it?
- Someone 4y agoYou’re forgetting this one: you can specify a flag that makes the call display a dialog box if a requested file does not exist. Yes, this call to open a file can run an event loop that lets the user create directories, rename files, possibly even format floppy disks, etc. If you want a prime example of a layer violation, this is it. Its replacement “CreateFile” (https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea https://learn.microsoft.com/en-us/windows/win32/api/fileapi/...) is a lot saner, but still has this: “If you rename or delete a file and then restore it shortly afterward, the system searches the cache for file information to restore. Cached information includes its short/long name pair and creation time.” That’s intended to make it easier for “Save” operations in document-based applications to keep the short name of the file, creation time and file flags around.