V
vclsaga
Unregistered / Unconfirmed
GUEST, unregistred user!
问题同标题,我现在的程序能实现通过ActiveSync连接后将数据从移动设备中下载至PC,方法是,调用RAPI.DLL里的函数,代码如下:
type
{结构声明}
TRapiInit = record
cbSizeWORD;
heRapiInit:THandle;
hrRapiInit:HResult;
end;
TForm1 = class(TForm)
RzBitBtn1: TRzBitBtn;
procedure RzBitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
procedure CreateParams(var Params: TCreateParams);override;
{ Public declarations }
end;
var
Form1: TForm1;
{函数声明}
function copyFileFromDevice(LocalFile: string; RemoteFile: string):bool;
function CeRapiInit():integer; stdcall; external 'RAPI.DLL';
function CeRapiUninit():integer; stdcall; external 'RAPI.DLL';
function CeDeleteFile(local: WideString): Boolean;stdcall;external 'RAPI.DLL';
function CeRapiInitEx(var rapiinit: TRapiInit):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';
{----------从移动设备复制文件到PC机中----------}
function copyFileFromDevice(LocalFile: string;RemoteFile: string):bool;
var
fs: TFileStream;
ahandle: thandle;
FTempBuffer: array[0..$1000] of byte;
nwrite,nRead: dword;
ini: integer;
begin
result := true;
ini := CeRapiInit();
if (ini<>0) then
begin
exit;
end;
ahandle := CeCreateFile(RemoteFile, $80000000, 0, 0, 3, $80, 0);
if ahandle=-1 then
begin
//showmessage('在设备上创建文件失败!');
MessageBox(0,'下载文件失败,请检查设备是否正确连接!','提示:',MB_OK OR MB_ICONWARNING);
CeRapiUninit();
result := false;
exit;
end;
if fileexists(localfile) then
deletefile(localfile);
fs:=tfileStream.Create(localfile,fmCreate);
CeReadFile(ahandle,FtempBuffer,sizeof(fTempBuffer),nRead,0);
if nRead<=0 then
begin
MessageBox(0,'数据采集器中没有barcode.txt文件,无法导出!','错误提示:',MB_OK OR MB_ICONWARNING);
fs.Free;
CeRapiUninit();
Exit;
end;
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;
procedure TForm1.RzBitBtn1Click(Sender: TObject);
var
ri: TRapiInit;
hRes: HRESULT;
dwRet: DWORD;
fpath, f: string;
sa: SECURITY_ATTRIBUTES;
Msg: string;
begin
Msg := '1、请检查设备是否正确连接!'+#13+'2、系统中是否安装通讯软件Microsoft ActiveSync V4.5!';
fpath := 'C:/ShineJit/';
f := 'barcode.txt';
if not DirectoryExists(fpath) then
begin
sa.nLength := SizeOf(sa);
sa.lpSecurityDescriptor := 0;
sa.bInheritHandle := True;
CreateDirectory(PChar(fpath),sa.lpSecurityDescriptor);
end;
ri.cbSize := sizeof(ri);
hRes := CeRapiInitEx(ri);
dwRet := WaitForSingleObject(ri.heRapiInit, 2000);
if ((dwRet <> WAIT_OBJECT_0) or (SUCCEEDED(ri.hrRapiInit) = FALSE)) then
begin
// Could not initialize Rapi
MessageBox(0,PChar(Msg),'导出失败:',MB_OK OR MB_ICONWARNING);
CeRapiUninit;
Exit;
end;
if copyFileFromDevice(fpath+'barcode.txt','barcode.txt') and CeDeleteFile(f) then
MessageBox(Handle,'数据下载成功!','提示:',MB_OK OR MB_ICONINFORMATION);
end;
现提出问题是,我在连接设备后,程序如何判断出设备是处于连接状态还是断开状态?
查了相关资料有个方法是ActiveSync连接后发送一个消息WM_NETCONNECT,参数WParams=0,但我如何能够利用我的程序截取该消息,然后判断出ActiveSync的工作状态?
谢谢各位了!!!!
type
{结构声明}
TRapiInit = record
cbSizeWORD;
heRapiInit:THandle;
hrRapiInit:HResult;
end;
TForm1 = class(TForm)
RzBitBtn1: TRzBitBtn;
procedure RzBitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
procedure CreateParams(var Params: TCreateParams);override;
{ Public declarations }
end;
var
Form1: TForm1;
{函数声明}
function copyFileFromDevice(LocalFile: string; RemoteFile: string):bool;
function CeRapiInit():integer; stdcall; external 'RAPI.DLL';
function CeRapiUninit():integer; stdcall; external 'RAPI.DLL';
function CeDeleteFile(local: WideString): Boolean;stdcall;external 'RAPI.DLL';
function CeRapiInitEx(var rapiinit: TRapiInit):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';
{----------从移动设备复制文件到PC机中----------}
function copyFileFromDevice(LocalFile: string;RemoteFile: string):bool;
var
fs: TFileStream;
ahandle: thandle;
FTempBuffer: array[0..$1000] of byte;
nwrite,nRead: dword;
ini: integer;
begin
result := true;
ini := CeRapiInit();
if (ini<>0) then
begin
exit;
end;
ahandle := CeCreateFile(RemoteFile, $80000000, 0, 0, 3, $80, 0);
if ahandle=-1 then
begin
//showmessage('在设备上创建文件失败!');
MessageBox(0,'下载文件失败,请检查设备是否正确连接!','提示:',MB_OK OR MB_ICONWARNING);
CeRapiUninit();
result := false;
exit;
end;
if fileexists(localfile) then
deletefile(localfile);
fs:=tfileStream.Create(localfile,fmCreate);
CeReadFile(ahandle,FtempBuffer,sizeof(fTempBuffer),nRead,0);
if nRead<=0 then
begin
MessageBox(0,'数据采集器中没有barcode.txt文件,无法导出!','错误提示:',MB_OK OR MB_ICONWARNING);
fs.Free;
CeRapiUninit();
Exit;
end;
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;
procedure TForm1.RzBitBtn1Click(Sender: TObject);
var
ri: TRapiInit;
hRes: HRESULT;
dwRet: DWORD;
fpath, f: string;
sa: SECURITY_ATTRIBUTES;
Msg: string;
begin
Msg := '1、请检查设备是否正确连接!'+#13+'2、系统中是否安装通讯软件Microsoft ActiveSync V4.5!';
fpath := 'C:/ShineJit/';
f := 'barcode.txt';
if not DirectoryExists(fpath) then
begin
sa.nLength := SizeOf(sa);
sa.lpSecurityDescriptor := 0;
sa.bInheritHandle := True;
CreateDirectory(PChar(fpath),sa.lpSecurityDescriptor);
end;
ri.cbSize := sizeof(ri);
hRes := CeRapiInitEx(ri);
dwRet := WaitForSingleObject(ri.heRapiInit, 2000);
if ((dwRet <> WAIT_OBJECT_0) or (SUCCEEDED(ri.hrRapiInit) = FALSE)) then
begin
// Could not initialize Rapi
MessageBox(0,PChar(Msg),'导出失败:',MB_OK OR MB_ICONWARNING);
CeRapiUninit;
Exit;
end;
if copyFileFromDevice(fpath+'barcode.txt','barcode.txt') and CeDeleteFile(f) then
MessageBox(Handle,'数据下载成功!','提示:',MB_OK OR MB_ICONINFORMATION);
end;
现提出问题是,我在连接设备后,程序如何判断出设备是处于连接状态还是断开状态?
查了相关资料有个方法是ActiveSync连接后发送一个消息WM_NETCONNECT,参数WParams=0,但我如何能够利用我的程序截取该消息,然后判断出ActiveSync的工作状态?
谢谢各位了!!!!