Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 795
  • Last Modified:

CopyFileEx problems

I'm trying to set up a std. procedure which will copy a file from one location to another. It HAS to be with CopyFileEx (don't ask me why :-).

I'm having some problems setting up the right types for the various variables, especially the LPVOID ones. If somebody could post an example of how to use the CopyFileEx xall in combination with the CopyProgressRoutine I would really appreciate it

take care
hatecapletters
0
hatecapletters
Asked:
hatecapletters
1 Solution
 
craig_capelCommented:
WHAT U SAY? YOU HATE CAP LETTERS, YEAH ME TOO!!!... hehe

lol... sorry, just playing, go on... why not use copyfile api?... do you have a bet on withsome or what?....


Craig C. :)

0
 
EpsylonCommented:
CopyFileEx is AFAIK only supported on NT.
0
 
EpsylonCommented:
If this is not a problem show us what you have...
0
 
intheCommented:
found a couple of exapmples that may help:

 type
   TProgressForm = class(TForm)
     AbortButton: TButton;
     ProgressBar: TProgressBar;
     procedure AbortButtonClick(Sender: TObject);
   private
     { Private declarations }
   public
     { Public declarations }
     FCancel: BOOL;
   end;
   // form has fsStayOnTop formstyle!
 
 implementation
 
 {$R *.DFM}
 
 procedure TProgressForm.AbortButtonClick(Sender: TObject);
 begin
   FCancel := True;
 end;
 
 // Note: could use int64 instead of COMP, but that would make this D4 specific Function
 CopyCallback( TotalFileSize, TotalBytesTransferred,
                       StreamSize, StreamBytesTransferred: COMP;                      
 dwStreamNumber, dwCallbackReason : DWORD;                       hSourceFile,
 hDestinationFile: THandle;
                       progressform: TProgressForm ): DWORD; stdcall; Var
   newpos: Integer;
 Begin
   Result := PROCESS_CONTINUE;
   If dwCallbackReason = CALLBACK_CHUNK_FINISHED Then Begin
     newpos := Round( TotalBytesTransferred / TotalFileSize * 100 );     With
 progressform.Progressbar Do
       if newpos <> Position then
         Position := newpos;
     Application.ProcessMessages;    
   End; { If }
 End; { CopyCallback }
 
 Function DoFilecopy( Const source, target: String ): Boolean;
 Var
   progressform: TProgressForm;
 Begin
   progressform := TProgressform.Create;
   try
     progressform.Show;
     Application.ProcessMessages;
     Result := CopyFileEx(
                 PChar( source ),
                 PChar( target ),
                 @CopyCallback,
                 Pointer( progressform ),
                 @progressform.FCancel,
                 0 );
 finally
   progressform.Hide;
   progressform.free;
 end;
 end; { DoFileCopy }







another example:

type
   LARGE_INTEGER = record
     LowPart : DWORD;
     HighPart : LongInt;
   end;
 
 function CopyProgressRoutine(TotalFileSize : LARGE_INTEGER;
   TotalBytesTransferred : LARGE_INTEGER; StreamSize : LARGE_INTEGER;   StreamBytesTransferred
 : LARGE_INTEGER; StreamNumber : DWORD;
   CallbackReason : DWORD; SourceFile : THandle; DestinationFile : THandle;   Data : pointer):
 DWORD; stdcall;
 begin
   Result := PROGRESS_CONTINUE;
 
   if CallbackReason = CALLBACK_CHUNK_FINISHED then begin
     Form1.Label1.Caption := Format('%f%% complete,',
                                   [(TotalBytesTransferred.LowPart / TotalFileSize.LowPart) *
 100]);
     Form1.Refresh;
   end;
 end;
 
 procedure TForm1.Button1Click(Sender: TObject);
 var
   Cancel : BOOL;
 begin
   Cancel := FALSE;
 
   if not CopyFileEx('C:\WINNT\IE4BAK.DAT',
                   'C:\TEMP\IE4BAK.DAT',
                   @CopyProgressRoutine,
                   nil,
                   @Cancel,
                   COPY_FILE_FAIL_IF_EXISTS)  then
     ShowMessage(SysErrorMessage(GetLastError));
 
 end;

Regards Barry
0
 
hatecaplettersAuthor Commented:
Excellent, allthough you forgot to write FUNCTION before the copycallback declaration ;-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now