Sascha
Sascha

Reputation: 71

Using SHFileOperation: What errors are occuring

I am using the function SHFileOperation() to send a file to the recycling bin and I am getting 2 errors that I do not know what they mean because with this function the error codes are not GetLastError() values.

When the function SHFileOperation() fails the return values are 0x57 (decimal 87) and 0x2 (decimal 2). Can anyone help me discover the definitions of these errors (especially when you consider with this function, the errors are not part of the GetLastError() codes).

Some important information:

.

 BOOL result;
 SHFILEOPSTRUCT fileStruct;
 fileStruct.hwnd = hwnd; 
 fileStruct.wFunc = FO_DELETE;
 fileStruct.pFrom = dest.c_str();
 fileStruct.fFlags = FOF_FILESONLY; // FOF_ALLOWUNDO
 fileStruct.fAnyOperationsAborted = result;

 // Call operation(delete file)
 int success = SHFileOperation( &fileStruct );

 // if delete was successful
 if ( success != 0 ) 
 {
    printf( "%s \t %X %d \n", dest.c_str(), success, success );
    cout << result << endl;

    MessageBox( hwnd, "Failed to delete file", "Error", MB_OK|MB_ICONERROR );
    return;
 }

Upvotes: 7

Views: 14362

Answers (4)

8bitwide
8bitwide

Reputation: 2071

Some serious problems with this snippit, I'd suggest reading SHFILEOPSTRUCT structure

  1. `pFrom must be doubly null terminated. I don't know what type dest is but it almost certainly isn't returning a doubly null terminated string.

  2. pTo parameter must be set to NULL if it is not used. Wildcard characters are not allowed. Their use will lead to unpredictable results.

  3. Safe practice would be to call zero memory on the struct before you fill it:

SHFILEOPSTRUCT shStruct;    
ZeroMemory (&shStruct, sizeof(SHFILEOPSTRUCT));
  1. fileStruct.fAnyOperationsAborted = result;won't put the value the function returns into result. TheSHFILEOPSTRUCT` exists on the stack and can simply be read from after the call.

These issues by themselves explain the error codes your getting.

Upvotes: 5

When working with wide strings with SHFileOperation, not only do you need to double-null terminate the string, but because pFrom is a list of strings, you must double-double-null terminate your path string :

So , In fileStruct.pFrom = dest.c_str(); statement there's no guarantee that will give you a double null.

You must do this before assignment :

WCHAR wszFrom[MAX_PATH] = { 0 };
StrCpyW(wszFrom, dest.c_str());
CopyMemory(wszFrom + lstrlenW(wszFrom), "\0\0", 2);

Then assign the double-double-null string :

fileStruct.pFrom = wszFrom;

You can prevent of this error :

ERROR_FILE_NOT_FOUND 2 (0x2) The system cannot find the file specified.

Upvotes: 1

Bradley Grainger
Bradley Grainger

Reputation: 28162

As shf301 said, error code 87 is ERROR_INVALID_PARAMETER. This probably occurs because you're not initialising the contents of the unused SHFILEOPSTRUCT fields, so they will contain random values. The documentation says that pTo "must be set to NULL if it is not used"; the OS might be checking the value of this field and returning an error if it's non-NULL.

First set everything to zero by either doing:

SHFILEOPSTRUCT fileStruct = { 0 };

or

SHFILEOPSTRUCT fileStruct;
ZeroMemory(&fileStruct, sizeof(SHFILEOPSTRUCT));

Error code 2 is ERROR_FILE_NOT_FOUND; it's probably safe to ignore this error code if it's possible that the file doesn't exist, or that the user has deleted it before your program tries to delete it. (Note that even if you check that the file exists before you try to delete it, another process could have deleted it in the meantime, so you will always need to handle this error.)

Upvotes: 5

shf301
shf301

Reputation: 31394

According to the SHFileOperation documentation, those errors map to the standard Windows error codes from WinError.h you can lookup those error codes at this page on MSDN.

Your errors would appear to be:

ERROR_FILE_NOT_FOUND 2 (0x2) The system cannot find the file specified.

and

ERROR_INVALID_PARAMETER 87 (0x57) The parameter is incorrect.

Upvotes: 2

Related Questions