Hints for using SHFileOperation D2 D3 C1 |
There are a few things that the documentation fails to mention about using SHFileOperation. First, Microsoft has published a documentation correction about the pFrom member. It says that for multiple files, each filename is seperated by a NULL (#0) character, and the whole string is terminated by double NULLs. You need the double NULL characters at the end even if you are not passing multiple filenames. Sometimes it will work correctly without them, but often not. That's because sometimes you get lucky and the byte of memory following the end of your string has a NULL already there. To build a Delphi string with NULL characters, simply add it like you do any other hard-coded character, i.e. MyStr := Str1 + #0 + Str2 + #0; Second, in order to be able to undo any operation, you must give the complete file path for every file specified. Do not rely on the current directory, even if you change to it right before the call. The SHFileOperation API is not "smart" enough to use the current directory if one is not given for undo information. So, even if you give the FOF_ALLOWUNDO flag, it will not move deleted files to the recycle bin because it doesn't know what path they came from, and thus can't restore them to their original location from the recycle bin. It will simply delete the file from the current directory permanently. |