2

I have a working code utilizing SHFileOperation for copying one directory into another. In this case this is Pascal code, but I also have been using the same function in C++, and the problem seems related to Windows core, not a specific programming language.

According to MSDN, I want to specify the following combination of flags:

FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI

That is, I do not need a progress bar, I suppress all possible questions about files and directories by implied 'yes' answers, and I don't want any error message in GUI (dialog boxes).

With this combination of flags, the function returns error 0x4C7 (cancelled by user, which is not true). If I remove the FOF_NOERRORUI it works ok on the same input parameters and filesystem state.

Unfortunately, I need to suppress error messages as well, and FOF_NOERRORUI flag is required.

Does someone know how this combination of flags (and may be other prerequisites) should be adjusted to meet my needs?

Here is the source code for those who may think some bugs are there:

function CopyDirectory(WindowHandle: HWND; FilenameFrom: string; FilenameTo: string): Boolean;
var
  SH: TSHFILEOPSTRUCT;
begin
  FillChar(SH, SizeOf(SH), 0);
  with SH do
  begin
    Wnd := WindowHandle;
    wFunc := FO_COPY;
    pFrom := PChar(FilenameFrom + #0);
    pTo := PChar(FilenameTo + #0);
    fFlags := FOF_NOCONFIRMMKDIR or FOF_NOCONFIRMATION or FOF_SILENT or FOF_NOERRORUI;
  end;
  Result := SHFileOperation(SH) = 0;
  Result := Result and (not SH.fAnyOperationsAborted);
end;
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
Stan
  • 8,683
  • 9
  • 58
  • 102
  • 1
    @TLama, I don't think `Delphi` and `Pascal` tags are appropriate, because the problem lies in the shell API and is reproducable with the same code in C++ (as I mentioned in the question). Tagging with `Delphi` may narrow the audience, eliminating someone familiar with the shell but added `Delphi` and/or `Pascal` in his ignore list. – Stan Oct 23 '13 at 10:05

1 Answers1

4

0x4C7 is actually:

"The operation was canceled by the user, or silently canceled if the appropriate flags were supplied to SHFileOperation."

If you turn off all the flags and let the operation run, what sort of questions are you asked? My guess is that one of those questions is being answered as "No" because the safe option is to do that.

Update

Have you thought of using the CopyFile() API function? No UI suppression necessary. The documentation is here.

Nat
  • 5,414
  • 26
  • 38
  • Alas, there are no questions in a normal (flagless) situation. But if, for example, one of the files is locked with other application, I got an error message. I need to suppress such UI stuff. Which combination of flags do you suggest to use for my requirements? What is "the safe option" you mentioned above? – Stan Mar 13 '12 at 18:05