Solved

CopyFileEx problems

Posted on 2000-03-02
5
762 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

615 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