Delphi-PRAXiS
Seite 1 von 3  1 23   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi CopyFileEx und Codeoptimierung XE5 (https://www.delphipraxis.net/179923-copyfileex-und-codeoptimierung-xe5.html)

wurzelzwerg 10. Apr 2014 23:58


CopyFileEx und Codeoptimierung XE5
 
Moin zusammen,

ich benutze DelphiXE5. CopyFileEx funktioniert da nicht wenn ich Codeoptimierung aktiv habe. :shock:
D.h. es kommt der Fehler "Die Anforderung wurde abgebrochen"
Delphi-Bug oder gibts dafür eine andere Erklärung?

Danke

Union 11. Apr 2014 02:23

AW: CopyFileEx und Codeoptimierung XE5
 
Die irrsinnige Menge des von Dir geposteten Sourcecodes hat mich fast zur Verzweiflung getrieben :roll:

wurzelzwerg 11. Apr 2014 09:09

AW: CopyFileEx und Codeoptimierung XE5
 
Wozu sourcecode? Der ist ja kaum falsch wenn es ohne Optimierung funktioniert.
Ausserdem ist der älter, d.h. in 2006 gabs auch mit Optimierung keine Fehler.

Delphi-Quellcode:
[B]
function TFileCopier.DoFolderFiles( const ASourcePath, ATargetPath: string;
                                    const Op: TFolderOp): Int64;

var
  StrName,
  MySearchPath,
  MyTargetPath,
  MySourcePath: string;
  FindRec: TSearchRec;
  i: Integer;
  Cancelled: Boolean;
  Attrs: integer;
  CopyIt: boolean;
begin
  Result := 0;
  Cancelled := False;
  MyTargetPath := AddBackSlash(ATargetPath);
  MySourcePath := AddBackSlash(ASourcePath);
  MySearchPath := AddBackSlash(ASourcePath) + '*.*';
  i := FindFirst(MySearchPath, 0 , FindRec);

  try
    while (i = 0) and (Result <> -1) do
    begin
      try
      case op of
       foCopy: begin
          StrName := MySourcePath + FindRec.Name;
          CopyIt:= CopyAll or (FindRec.Attr and faArchive = faArchive);
          if CopyIt then
          begin
            //Cancelled:= false;
            if CopyFileEx(PWideChar(StrName), PWideChar(MyTargetPath + FindRec.Name), @fCallBack, Pointer(fHandle), @Cancelled, 0) then
            begin
              Attrs := FileGetAttr(StrName);
              FileSetAttr(StrName, Attrs and not faArchive);
              inc(Result);
              inc(fCopyCount);
              TUtils.WriteLogFile(eData, StrName + ' saved to ' + MyTargetPath + FindRec.Name);
              Sleep(50);
            end
            else
            begin
              Result := -1;
              TUtils.WriteLogFile(eError, StrName + ' not saved' + #13#10 + SysErrorMessage (GetLastError));
            end;
          end;
        end;
       foCount:
       begin
         Inc(Result);
         Inc(fFileCount);
       end;
       foSize:
       begin
         Result := Result + FindRec.Size;
         fFileSize := fFileSize + FindRec.Size;
       end;
      end; // case
      except
        Result := -1;
      end;
      i := FindNext(FindRec);
    end;
  finally
    FindClose(FindRec);
  end;

end;

[/B]

Union 11. Apr 2014 09:36

AW: CopyFileEx und Codeoptimierung XE5
 
Diese Zuweisungen werden wegoptimiert, weil die Variable aus Sicht des Compilers später nicht mehr angesprochen wird:
Delphi-Quellcode:
Cancelled:= false;
Wenn Du das Flag später nochmal abfragst könnte es gehen.

wurzelzwerg 11. Apr 2014 09:50

AW: CopyFileEx und Codeoptimierung XE5
 
Die Zeile gehört eigentlich nicht rein, war nur um das zu finden.
Das setzt man auf true um während des kopierens abzubrechen. So weit kommt man aber erst garnicht.

Union 11. Apr 2014 09:53

AW: CopyFileEx und Codeoptimierung XE5
 
Liegt der Fehler evtl. im Callback?

Der schöne Günther 11. Apr 2014 09:55

AW: CopyFileEx und Codeoptimierung XE5
 
Laut API-Dokumentation ist aber doch genau das der Grund wenn er mit
Delphi-Quellcode:
ERROR_REQUEST_ABORTED
rausgeht.

Der andere wäre, wenn dein
Delphi-Quellcode:
fCallBack
Delphi-Quellcode:
PROGRESS_STOP
zurückgibt, aber über das Ding wissen wir nichts.

wurzelzwerg 11. Apr 2014 09:59

AW: CopyFileEx und Codeoptimierung XE5
 
Nein, ohne callback(nil) das gleiche Problem.

Am Cancel liegt das nicht, das war vorher auch dort nicht drin, brauch ich auch nicht.
Die Funktion wird sofort abgebrochen. Aber eben nur wenn Optimierung an ist.

pbCancel [in, optional]
If this flag is set to TRUE during the copy operation, the operation is canceled. Otherwise, the copy operation will continue to completion.


Callback sendet nur Messages, aber wie gesagt ohne Callback gehts auch nicht.
Delphi-Quellcode:
function CopyFileProgress(TotalFileSize, TotalBytesTransferred, StreamSize,
  StreamBytesTransferred: LARGE_INTEGER; dwStreamNumber, dwCallbackReason,
  hSourceFile, hDestinationFile: DWORD; lpData: Pointer): DWORD; stdcall;
begin
  if CancelCopy = True then
  begin
    SendMessage(THandle(lpData), CEXM_CANCEL, 0, 0);
    result:= PROGRESS_CANCEL;
    exit;
  end;
  case dwCallbackReason of
    CALLBACK_CHUNK_FINISHED:
      begin
        SendMessage(THandle(lpData), CEXM_CONTINUE, TotalBytesTransferred.LowPart, TotalBytesTransferred.HighPart);
        result:= PROGRESS_CONTINUE;
      end;
    CALLBACK_STREAM_SWITCH:
      begin
        SendMessage(THandle(lpData), CEXM_MAXBYTES, TotalFileSize.LowPart, TotalFileSize.HighPart);
        result:= PROGRESS_CONTINUE;
      end;
  else
    result:= PROGRESS_CONTINUE;
  end;
end;

himitsu 11. Apr 2014 10:15

AW: CopyFileEx und Codeoptimierung XE5
 
Einen Fehle seh ich auf die Schnelle auch noch nicht,

aber im Notfall könntest du ja die Optimierung für die eine einfach Funktion deaktivieren.

Der schöne Günther 11. Apr 2014 10:28

AW: CopyFileEx und Codeoptimierung XE5
 
Du meinst also, ein reines
Delphi-Quellcode:
   if not CopyFileEx(
      'd:\Neues Textdokument.txt',
      'd:\Neues Textdokument (Kopie).txt',
      nil, nil, nil,
      COPY_FILE_NO_BUFFERING
   ) then RaiseLastOSError();
fliegt schon raus?


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:06 Uhr.
Seite 1 von 3  1 23   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz