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;