0

I have a question involving SHFileOperation:

    SHFILEOPSTRUCT sf2;
    memset(&sf2,0,sizeof(sf2));
    sf2.hwnd = NULL;
    sf2.wFunc = FO_DELETE;
    sf2.fFlags = FOF_NOCONFIRMATION;
    sf2.pFrom = pathSubDir; // where pathSubDir = ""

    //2. Delete temporary files
    int n = SHFileOperation(&sf2);

My expectation was that since pathSubDir was "", it wasn't deleting anything and that I should get n NOT equal to 0. But, that was not the case. I ended up with n = 0, which means that the operation was completed successfully. I was curious as to if someone could explain to me why my assumptions are wrong. Thank you.

JHowzer
  • 3,684
  • 4
  • 30
  • 36
  • 1
    How could the operation "delete nothing" ever fail? – K-ballo Jun 01 '12 at 19:53
  • 1
    Situations like this are REALLY HARD for the API designer to do the right thing in every situation. Someone had to decide: if the user deletes nothing, do I error (after all, nothing is already deleted, so technically it is deleted). Similarly, if the caller asks for "foo.bar" to be deleted, but there is no "foo.bar", then is that success, or failure? IMX, Delete() functions are usually better to assume success when their input is already deleted. It means less checking is necessary for the client of the function, and it's more up to the ultimate end-user to choose what they want deleted. – Mordachai Jun 01 '12 at 20:03

1 Answers1

1

The API requires pFrom to be double-null-terminated. A simple empty string like "" doesn't qualify. The API could be reading whatever memory comes after the first null character and successfully deleting those files.

Another explanation, if your string is really formatted correctly, is that the API has successfully deleted all the requested files, and encountered no failures along the way. It accomplished all the deletions you asked for, so it returns success.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • Can you write some simple example string format like. "C:\SomeFolder\SomeTXT.txt" Is this sting correct? – Oleksandr Fentsyk Jun 27 '12 at 10:26
  • No, that's not correct, @Sasha. If `""` isn't double-null-terminated, as I stated in my answer, then putting other non-null characters in it won't suddenly make it be. If you want more than the default number of null characters at the end of a C string (which is 1), then you need to put them there yourself. Please consult [the question about creating double-null-terminated strings](http://stackoverflow.com/q/4611237/33732). – Rob Kennedy Jun 27 '12 at 14:02