API function copyfileex (Delphi)

Source: Internet
Author: User

{Function prototype}
 
Function copyfileex (
Lpexistingfilename: pchar; // source file
Lpnewfilename: pchar; // target file
Lpprogressroutine: tfnprogressroutine; // callback function used to return information about a file
Lpdata: pointer; // parameters passed to the callback function
Pbcancel: pbool; // used to cancel copying in the middle. The function monitors the status of this value. If it is true, the copy is stopped.
Dwcopyflags: DWORD // option flag, see definition
): Bool; stdcall; // returns whether the copy is successful or fails.
 
 
 
{Possible return values of the copyfileex callback function}
 
Progress_continue = 0; // Let the copy process continue
Progress_cancel = 1; // terminate the copy process and delete the target file
Progress_stop = 2; // stop copying, but you can continue later
Progress_quiet = 3; // Let the copy process continue, but no callback
 
 
 
{Copyfileex callback function status}
 
Callback_stream_switch = $00000001; // The status of the first callback is callback_stream_switch.
Callback_chunk_finished = $00000000; // The subsequent status is callback_chunk_finished.
 
 
 
{Copyfileex option flag}
 
Copy_file_fail_if_exists = $00000001; // cancel copying if the file exists.
Copy_file_restartable = $00000002; // copy failed and can be started again
 
 
 
{Callback function prototype}
 
Tfarproc = pointer;
Tfnprogressroutine = tfarproc; // pointer type
 
Function copyprogress (
Totalfilesize: large_integer; // total file size, in bytes
Totalbytestransferred: large_integer; // the size of the copied file, in bytes.
Streamsize: large_integer; // The total number of bytes of the current stream
Streambytestransferred: large_integer; // number of bytes copied to the current stream
Dwstreamnumber: DWORD; // Number of the current stream
Dwcallbackreason: DWORD; // The status of the callback function, as defined in.
Hsourcefile: thandle; // source file handle
Hdestinationfile: thandle; // target file handle
Lpdata: parameters passed by the pointer // copyfileex Function
): DWORD; stdcall; // return value of the callback function, see definition
 
 
 
{Note}
 
This function copies an existing file to a new file. This function retains the extended attributes of the file, the OLE storage format, and the file attributes. File security properties are not copied to the new file.
If the target file exists and has a read-only attribute, function execution fails.
This function calls a callback function every time it copies 65536 bytes (that is, 64 KB.
When the callback function is called for the first time, the value of dwcallbackreason (the status of the callback function) is callback_stream_switch. At this time, no data is copied, but the callback function is told to Start copying data.
This function is synchronized by threads. That is to say, when this function is called in the main window, the window cannot respond to user operations until the file is copied.
This function cannot copy locked files, such as pagefile. sys. Just like copying pagefile. sys in the resource manager, the system will prompt that the file is being used by another program and cannot be copied. This function does not prompt an error. It only returns the logical value "no", indicating that the copy failed.
 
 
 
{===================================================== =================================}
{Example}
{===================================================== =================================}
 
Unit Main;
 
Interface
 
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;
 
Type
Tform2 = Class (tform)
Memo1: tmemo;
Button1: tbutton;
Button2: tbutton;
Procedure button2click (Sender: tobject );
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
 
VaR
Form2: tform2;
Cancel: Boolean;
Icount: integer;
 
Implementation
 
{$ R *. DFM}
 
Function copyprogress (
Totalfilesize: large_integer; // total file size, in bytes
Totalbytestransferred: large_integer; // the size of the copied file, in bytes.
Streamsize: large_integer; // The total number of bytes of the current stream
Streambytestransferred: large_integer; // number of bytes copied to the current stream
Dwstreamnumber: DWORD; // Number of the current stream
Dwcallbackreason: DWORD; // The status of the callback function, as defined in.
Hsourcefile: thandle; // source file handle
Hdestinationfile: thandle; // target file handle
Lpdata: parameters passed by the pointer // copyfileex Function
): DWORD; stdcall; // return value of the callback function, see definition
Begin
// If icount = 2 then begin
// Result: = progress_stop;
// Exit;
// End;
Application. processmessages;
Form2.memo1. Clear;
Form2.memo1. lines. Add (inttostr (totalfilesize. quadpart ));
Form2.memo1. lines. Add (inttostr (totalbytestransferred. quadpart ));
Form2.memo1. lines. Add (inttostr (streamsize. quadpart ));
Form2.memo1. lines. Add (inttostr (streambytestransferred. quadpart ));
Form2.memo1. lines. Add (inttostr (dwstreamnumber ));
Form2.memo1. lines. Add (inttostr (result ));
INC (icount );
End;
 
Procedure tform2.button1click (Sender: tobject );
VaR
SRC, DEST: string;
Begin
Icount: = 0;
Cancel: = false;
SRC: = 'd:/tools.rar ';
DeST: = 'e:/tools.rar ';

If not fileexists (SRC) then begin
Memo1.text: = 'file '+ SRC +' does not exist! ';
Exit;
End;
 
Copyfileex (
Pchar (SRC ),
Pchar (DEST ),
@ Copyprogress,
Nil,
@ Cancel,
Copy_file_restartable );
End;
 
Procedure tform2.button2click (Sender: tobject );
Begin
Cancel: = true;
End;
 
End.
 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/stevenldj/archive/2007/11/08/1872842.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.