procedure TForm1.Button1Click(Sender: TObject);
var
ErrorMessage: Pointer; // holds a system error string
ErrorCode: DWORD; // holds a system error code
begin
{blank out the status bar}
StatusBar1.SimpleText:='';
{attempt to copy the file}
if not CopyFile(PChar(Edit1.Text+'/'+ExtractFilename
(FileListBox1.FileName)),
PChar(Edit2.Text+'/'+ExtractFilename
(FileListBox1.FileName)),
not CheckBox1.Checked) then
begin
{if the file was not copied, display the error message}
ErrorCode := GetLastError;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or
FORMAT_MESSAGE_FROM_SYSTEM,
nil, ErrorCode, 0, @ErrorMessage, 0, nil);
StatusBar1.SimpleText:='Error Copying File: '+string(PChar
(ErrorMessage));
LocalFree(hlocal(ErrorMessage));
end;
end;