Solved

CopyFileEx problems

Posted on 2000-03-02
5
734 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

18 Experts available now in Live!

Get 1:1 Help Now