Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to delete a folder with SHFileOperation

Tags:

winapi

I'm unable to delete a folder in Windows with the following code:

SHFILEOPSTRUCT shfo = {
    NULL,
    FO_DELETE,
    path,
    NULL,
    FOF_SILENT | FOF_NOERRORUI | FOF_NOCONFIRMATION,
    FALSE,
    NULL,
    NULL };

SHFileOperation(&shfo);

I need to use SHFileOperation instead of RemoveDirectory because I need to delete non-empty folders.

However, the function fails even if the value in path points to an empty local folder with full control of Everyone user, is double-null terminated (as requested by documentation), has no system, hidden or read only attribute...

Unfortunately the function does not return an error code (returns zero if successful, or nonzero otherwise) and calling GetLastError returns ERROR_SUCCESS...

Where is the error?

like image 740
lornova Avatar asked Nov 14 '10 23:11

lornova


1 Answers

Solved...

The path variable wasn't actually double-null terminated because I used wcscpy_s that fills the string buffer (that I had previously filled with zeros) with the 0xFD value after the null char...

like image 117
lornova Avatar answered Oct 03 '22 05:10

lornova