Solved

CopyFileEx problems

Posted on 2000-03-02
5
728 Views
Last Modified: 2011-05-16
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
Comment
Question by:hatecapletters
5 Comments
 
LVL 2

Expert Comment

by:craig_capel
ID: 2576957
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2576968
CopyFileEx is AFAIK only supported on NT.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2576992
If this is not a problem show us what you have...
0
 
LVL 17

Accepted Solution

by:
inthe earned 100 total points
ID: 2577265
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
 

Author Comment

by:hatecapletters
ID: 2577737
Excellent, allthough you forgot to write FUNCTION before the copycallback declaration ;-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now