Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.150 Beiträge
 
Delphi 12 Athens
 
#7

Re: CreateFile + VirtualAlloc -> Datei kopieren (geht/nic

  Alt 11. Nov 2008, 17:06
Delphi-Quellcode:
if ( hDestFile = INVALID_HANDLE_VALUE ) then
begin
  CloseHandle( hSrcFile );
  DoNotifyError( SysErrorMessage( GetLastError ) );
  Exit;
end;
PS: hSrcFile ist und bleibt bei einem Fehler hier ebenfalls geöffnet

Eine Lösung wäre zwar sowas, aber übersichtlich ist's nicht gerade.
Delphi-Quellcode:
hSrcFile := CreateFile( PAnsiChar(aSrcFile), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0 );
if ( hSrcFile = INVALID_HANDLE_VALUE ) then
begin
  DoNotifyError( SysErrorMessage( GetLastError ) );
  Exit;
end;

hDestFile := CreateFile( PAnsiChar(aDestFile), GENERIC_WRITE, FILE_SHARE_READ, nil, CREATE_ALWAYS, 0, 0 );
if ( hDestFile = INVALID_HANDLE_VALUE ) then
begin
  CloseHandle(hSrcFile); // <<<<<<<<<<<<<<<<<<<<<
  CloseHandle( hSrcFile );
  DoNotifyError( SysErrorMessage( GetLastError ) );
  Exit;
end;

Size := ( UpSize shl 16 or LoSize ); Cardinal ist 32 Bit.

if ( Size.QuadPart > 0 ) then Schau dir bitte dringend mal die Definition der Rückgabewerte an!
> MSDN-Library durchsuchenGetFileSize


Mein Weg sähe wohl etwa so aus:
Delphi-Quellcode:
var
  Size : LARGE_INTEGER;
  Read ,
  Written ,
  GLE : Cardinal;
  IO : Pointer;
  hSrcFile ,
  hDestFile: THandle;
begin
  Result := False;
  hSrcFile := CreateFile( PAnsiChar(aSrcFile),
                    GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0 );
  hDestFile := CreateFile( PAnsiChar(aDestFile),
                    GENERIC_WRITE, FILE_SHARE_READ, nil, CREATE_ALWAYS, 0, 0 );
  Size.LowPart := GetFileSize( hSrcFile, @Size.HighPart);
  GLE := GetLastError;
  IO := VirtualAlloc( nil, 2048, MEM_COMMIT, PAGE_READWRITE );

  if ( hSrcFile <> INVALID_HANDLE_VALUE )
    and ( hDestFile <> INVALID_HANDLE_VALUE )
    and (( Size.QuadPart <> $ffffffff ) or (GLE = NO_ERROR))
    and Assigned( IO ) then
  begin
    Result := True;
    Read := 0;
    Written := 0;
   
    while ( Result and ( Read = Written ) and ( Size > 0 ) ) do
    begin
      Result := ReadFile( hSrcFile, IO^, 2048, Read, nil );
   
      if Result and ( Read > 0 ) then
      begin
        Result := WriteFile( hDestFile, IO^, Read, Written, nil );
        Size := Size - Written;
      end;
    end;
   
    if not Result then DoNotifyError( SysErrorMessage( GetLastError ) );
  end else
  begin
    DoNotifyError( SysErrorMessage( GLE ) );
  end;
 
  VirtualFree( IO, 0, MEM_RELEASE );
  CloseHandle( hSrcFile );
  CloseHandle( hDestFile );
end;
Was macht eigentlich DoNotifyError?
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat