|
发表于 2012-2-2 16:10:09
|
显示全部楼层
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ComCtrls,windows;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
ProgressBar1: TProgressBar;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
function copyFileToDevice(LocalFile: string; RemoteFile: string; OverWrite: Boolean):boolean;
function copyFileFromDevice(LocalFile: string; RemoteFile: string):Boolean;
function CeRapiInit(): integer; stdcall; external 'RAPI.DLL';
function CeRapiUninit(): integer; stdcall; external 'RAPI.DLL';
function CeCreateFile(lpFileName: WideString; dwDesiredAccess: cardinal; dwShareMode: cardinal; lpSecurityAttributes: pSecurityAttributes;
dwCreationDisposition: cardinal; dwFlagsAndAttributes: cardinal; hTemplateFile: cardinal): cardinal; stdcall; external 'RAPI.DLL';
function CeReadFile(hFile: cardinal; var lpBuffer; nNumberOfBytesToRead: cardinal;
var lpNumberOfBytesRead: cardinal; lpOverlapped: poverlapped): Boolean; stdcall; external 'RAPI.DLL';
function CeWriteFile(hFile: cardinal; const lpBuffer; nNumberOfBytesToWrite: cardinal;
var lpNumberOfBytesWritten: cardinal; lpOverlapped: poverlapped): Boolean; stdcall; external 'RAPI.DLL';
function CeGetFileSize(hFile:cardinal;var lpFileSizeHigh:dword ):integer;stdcall;external 'RAPI.DLL';
function CeCloseHandle(IntPtr: thandle): integer; stdcall; external 'RAPI.DLL';
implementation
{$R *.lfm}
{ TForm1 }
function copyFileToDevice(LocalFile: string; RemoteFile: string; OverWrite: boolean):boolean;
var fs: TFileStream;
ahandle: thandle;
FTempBuffer: array[0..$1000] of byte;
n, m: integer;
nwrite: dword;
IsOverWrite: integer;
begin
result := false;
if (CeRapiInit() <> 0) then
exit;
ahandle := 0;
if OverWrite then
IsOverWrite := 2 else
IsOverWrite := 1;
// function CeCreateFile(lpFileName: WideString;
// dwDesiredAccess: cardinal; dwShareMode: cardinal; lpSecurityAttributes: pSecurityAttributes;
// dwCreationDisposition: cardinal; dwFlagsAndAttributes: cardinal; hTemplateFile: cardinal):
// cardinal;
// $40000000 2
//CeCreateFile(PWideChar(ToFile), GENERIC_WRITE, FILE_SHARE_WRITE, nil, CREATE_NEW, 0, 0);
ahandle := CeCreateFile(RemoteFile, $40000000, 2, nil, IsOverWrite, $80, 0);
if ahandle = -1 then
begin
result:=false;
CeRapiUninit();
exit;
end;
if fileexists(localfile) then
fs := tfileStream.Create(localfile, fmOpenRead) else
exit;
n := sizeof(FTempBuffer);
m := fs.Size;
form1.ProgressBar1.Max :=m;
try
while fs.Position < fs.Size do
begin
form1.ProgressBar1.Position :=fs.Position;
if m < n then
n := m;
fs.Read(FTempBuffer, n);
if not CeWriteFile(aHandle, FTempBuffer, n, nwrite, nil) then
break;
m := m - n;
end;
finally
CeCloseHandle(ahandle);
end;
if fs.Position = fs.Size then
result := true;
fs.Free;
CeRapiUninit();
end;
function copyFileFromDevice(LocalFile: string; RemoteFile: string):boolean;
var fs: TFileStream;
ahandle: thandle;
FTempBuffer: array[0..$1000] of byte;
nwrite,nRead,filelength: dword;
begin
result := true;
if (CeRapiInit() <> 0) then
exit;
ahandle := 0;
ahandle := CeCreateFile(RemoteFile, $80000000, 0, nil, 3, $80, 0);
if ahandle = -1 then
begin
showmessage('在设备上创建文件失败!');
CeRapiUninit();
result := false;
exit;
end;
form1.ProgressBar1.Max :=CeGetFileSize(ahandle,filelength);
if fileexists(localfile) then
deletefile(PChar(localfile));
fs:=tfileStream.Create(localfile,fmCreate);
//
CeReadFile(ahandle,FtempBuffer,sizeof(fTempBuffer),nRead,nil);
while nRead>0 do
begin
application.ProcessMessages;
form1.ProgressBar1.Position := form1.ProgressBar1.Position +nRead;
fs.Write(fTempBuffer,nRead);
if not CeReadFile(ahandle,FtempBuffer,sizeof(fTempBuffer),nRead,nil) then
begin
result:=false;
break;
end;
end;
CeCloseHandle(ahandle);
fs.Free;
CeRapiUninit();
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
copyFileFromDevice(edit1.text,edit2.text);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
copyFileToDevice(edit1.text,edit2.text,true);
showmessage('操作成功!');
end;
end.
---------------------------------------------
参考他人代码,转帖网上的。
加了点自己琢磨的。 |
|