我写的wince文件读写的代码,编译通过的 function copyFileToDevice(LocalFile: string; RemoteFile: string; OverWrite: bool):bool;function copyFileFromDevice(LocalFile: string; RemoteFile: string):bool; 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): bool; stdcall; external 'RAPI.DLL';function CeWriteFile(hFile: cardinal; const lpBuffer; nNumberOfBytesToWrite: cardinal; var lpNumberOfBytesWritten: cardinal; lpOverlapped: poverlapped): bool; stdcall; external 'RAPI.DLL';function CeCloseHandle(IntPtr: thandle): integer; stdcall; external 'RAPI.DLL';function copyFileToDevice(LocalFile: string; RemoteFile: string; OverWrite: bool):bool; 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; ahandle := CeCreateFile(RemoteFile, $40000000, 0, 0, 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; try while fs.Position < fs.Size do begin 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):bool; var fs: TFileStream; ahandle: thandle; FTempBuffer: array[0..$1000] of byte; nwrite,nRead: dword; begin result := true; if (CeRapiInit() <> 0) then exit; ahandle := 0; ahandle := CeCreateFile(RemoteFile, $80000000, 0, 0, 3, $80, 0); if ahandle = -1 then begin showmessage('在设备上创建文件失败!'); CeRapiUninit(); result := false; exit; end; if fileexists(localfile) then deletefile(localfile); fs:=tfileStream.Create(localfile,fmCreate); CeReadFile(ahandle,FtempBuffer,sizeof(fTempBuffer),nRead,0); while nRead>0 do begin fs.Write(fTempBuffer,nRead); if not CeReadFile(ahandle,FtempBuffer,sizeof(fTempBuffer),nRead,0) then begin result:=false; break; end; end; CeCloseHandle(ahandle); fs.Free; CeRapiUninit(); end;
我的Blog:
http://kevin-lu.blogspot.com/
function copyFileToDevice(LocalFile: string; RemoteFile: string; OverWrite: bool):bool;function copyFileFromDevice(LocalFile: string; RemoteFile: string):bool;
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): bool; stdcall; external 'RAPI.DLL';function CeWriteFile(hFile: cardinal; const lpBuffer; nNumberOfBytesToWrite: cardinal;
var lpNumberOfBytesWritten: cardinal; lpOverlapped: poverlapped): bool; stdcall; external 'RAPI.DLL';function CeCloseHandle(IntPtr: thandle): integer; stdcall; external 'RAPI.DLL';function copyFileToDevice(LocalFile: string; RemoteFile: string; OverWrite: bool):bool;
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; ahandle := CeCreateFile(RemoteFile, $40000000, 0, 0, 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;
try
while fs.Position < fs.Size do
begin
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):bool;
var fs: TFileStream;
ahandle: thandle;
FTempBuffer: array[0..$1000] of byte;
nwrite,nRead: dword;
begin
result := true; if (CeRapiInit() <> 0) then
exit;
ahandle := 0; ahandle := CeCreateFile(RemoteFile, $80000000, 0, 0, 3, $80, 0); if ahandle = -1 then
begin
showmessage('在设备上创建文件失败!');
CeRapiUninit();
result := false;
exit;
end; if fileexists(localfile) then
deletefile(localfile); fs:=tfileStream.Create(localfile,fmCreate); CeReadFile(ahandle,FtempBuffer,sizeof(fTempBuffer),nRead,0);
while nRead>0 do
begin
fs.Write(fTempBuffer,nRead);
if not CeReadFile(ahandle,FtempBuffer,sizeof(fTempBuffer),nRead,0) then
begin
result:=false;
break;
end;
end; CeCloseHandle(ahandle);
fs.Free;
CeRapiUninit();
end;
很多方式,红外,蓝牙,串口,一般都有通用或专用的通信协议