DELPHI通过ACTIVESYNC连接到移动设备,并进行文件的复制,删除等操作!

来源:互联网 发布:讯飞tts软件 编辑:程序博客网 时间:2024/04/30 08:13
  1. unit Unit1;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, RzButton, StdCtrls, AutoSink, RzStatus, ExtCtrls, RzPanel,
  6.   ImgList;
  7. type
  8.   TRapiInit = record
  9.       cbSize:DWORD;
  10.       heRapiInit:THandle;
  11.       hrRapiInit:HResult;
  12.   end;
  13.   TForm1 = class(TForm)
  14.     RzBitBtn1: TRzBitBtn;
  15.     Label1: TLabel;
  16.     RzStatusBar1: TRzStatusBar;
  17.     GlyphStatus: TRzGlyphStatus;
  18.     ImageList1: TImageList;
  19.     Label2: TLabel;
  20.     procedure RzBitBtn1Click(Sender: TObject);
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.   public
  25.     procedure CreateParams(var Params: TCreateParams);override;
  26.     procedure ChangeActif(Sender: TObject);
  27.     { Public declarations }
  28.   end;
  29. var
  30.   Form1: TForm1;
  31.   DccMan: TDccMan;
  32.   function copyFileFromDevice(LocalFile: string; RemoteFile: string):bool;
  33.   function CeRapiInit():integer; stdcall; external 'RAPI.DLL';
  34.   function CeRapiUninit():integer; stdcall; external 'RAPI.DLL';
  35.   function CeDeleteFile(local: WideString): Boolean;stdcall;external 'RAPI.DLL';
  36.   function CeRapiInitEx(var rapiinit: TRapiInit):Integer;stdcall;external 'RAPI.DLL';
  37.   function CeCreateFile(lpFileName: WideString; dwDesiredAccess: cardinal; dwShareMode: cardinal; lpSecurityAttributes: pSecurityAttributes; dwCreationDisposition: cardinal; dwFlagsAndAttributes: cardinal; hTemplateFile: cardinal):cardinal; stdcall;external 'RAPI.DLL';
  38.   function CeReadFile(hFile: cardinal;var lpBuffer; nNumberOfBytesToRead: cardinalvar lpNumberOfBytesRead: cardinal; lpOverlapped: poverlapped):bool; stdcall; external 'RAPI.DLL';
  39.   function CeWriteFile(hFile: cardinal;const lpBuffer; nNumberOfBytesToWrite: cardinalvar lpNumberOfBytesWritten: cardinal; lpOverlapped: poverlapped):bool; stdcall; external 'RAPI.DLL';
  40.   function CeCloseHandle(IntPtr: thandle):integer; stdcall; external 'RAPI.DLL';
  41. implementation
  42. {$R *.dfm}
  43. function VoirSiConnect(attente : integer) : boolean;
  44. var
  45.   ri: TRapiInit;
  46.   hRes: HRESULT;
  47.   dwRet: HRESULT;
  48. begin
  49.   ri.cbSize :=  sizeof(ri);
  50.   hRes := CeRapiInitEx(ri);
  51.   if hRes = S_OK then
  52.   begin
  53.     dwRet := WaitForSingleObject(ri.heRapiInit, attente);
  54.     if ((dwRet <> WAIT_OBJECT_0) OR (ri.hrRapiInit <> S_OK))then
  55.       begin
  56.         CeRapiUninit;
  57.         Result := false;
  58.       end
  59.     else
  60.       begin
  61.         Result := true;
  62.       end;
  63.   end
  64.   else
  65.   begin
  66.     Result := false;
  67.   end;
  68. end;
  69. procedure CloreConnection;
  70. begin
  71.   CeRapiUninit;
  72. end;
  73. procedure TForm1.ChangeActif(Sender: TObject);
  74. var
  75.   LogEtat : string;
  76.   SActifIPAS, STermineAS, SActifAS, SInactifAS, SReponseAS: string;
  77.   SListenAS, SDeconnexionAS, SErreur,SDeconnectAS: string;
  78.   Connect: Boolean;
  79.   i: integer;
  80. begin
  81.   SActifIPAS := '设备已连接';
  82.   STermineAS := '设备关闭';
  83.   SActifAS := '设备活动中';
  84.   SInactifAS := '非连接状态';
  85.   SReponseAS := '响映中...';
  86.   SListenAS := '连接中...';
  87.   SDeconnexionAS := '连接断开';
  88.   SErreur := '连接错误';
  89.   SDeconnectAS := '关闭连接';
  90.   Connect := False;
  91.   case DccMan.IndexOnLog of
  92.     1 :
  93.     begin
  94.       LogEtat := SActifIPAS;
  95.       if copyFileFromDevice('e:/mp3.txt','mp3.txt'then
  96.         Label2.Caption := '文件复制完成!';
  97.       GlyphStatus.ImageIndex := 1;
  98.     end;
  99.     2 : LogEtat := STermineAS;
  100.     3 : LogEtat := SActifAS;
  101.     4 : LogEtat := SInactifAS;
  102.     5 :
  103.     begin
  104.       LogEtat := SReponseAS;
  105.     end;
  106.     6 :
  107.     begin
  108.       LogEtat := SListenAS;
  109.       //GlyphStatus.ImageIndex := 2;
  110.     end;
  111.     7 :
  112.     begin
  113.       LogEtat := SDeconnexionAS;
  114.       GlyphStatus.ImageIndex := 2;
  115.     end;
  116.     8 :
  117.     begin
  118.       LogEtat := SErreur;
  119.       GlyphStatus.ImageIndex := 3;
  120.     end;
  121.   end;
  122.   Label1.Caption := LogEtat;
  123.   GlyphStatus.Caption := LogEtat;
  124.   if (DccMan.IndexOnLog=6or (DccMan.IndexOnLog=5then
  125.   begin
  126.     for i:=9 to 23 do
  127.       GlyphStatus.ImageIndex := i;
  128.   end;
  129.   if not Connect and (DccMan.IndexOnLog = 1then
  130.     begin
  131.       Connect := VoirSiConnect(5000);
  132.     end;
  133.   if Connect and (DccMan.IndexOnLog <> 1then
  134.     begin
  135.       Connect := false;
  136.       CloreConnection;
  137.       MessageBox(Handle, PChar(SDeconnectAS), '提示:', MB_OK or MB_ICONWARNING);
  138.     end;
  139. end;
  140. procedure TForm1.CreateParams(var Params: TCreateParams);
  141. begin
  142.   inherited;
  143.   Params.Style := WS_SYSMENU;
  144. end;
  145. {----------从移动设备复制文件到PC机中----------}  
  146. function  copyFileFromDevice(LocalFile: string;RemoteFile: string):bool;
  147. var
  148.   fs: TFileStream;
  149.   ahandle: thandle;
  150.   FTempBuffer: array[0..$1000of byte;
  151.   nwrite,nRead: dword;
  152.   ini: integer;
  153. begin
  154.   result := true;
  155.   ini := CeRapiInit();
  156.   if (ini<>0then
  157.   begin
  158.     exit;
  159.   end;
  160.   ahandle := CeCreateFile(RemoteFile, $80000000003$800);
  161.   if ahandle=-1 then
  162.   begin
  163.     //showmessage('在设备上创建文件失败!');
  164.     MessageBox(0,'下载文件失败,请检查设备是否正确连接!','提示:',MB_OK OR MB_ICONWARNING);
  165.     CeRapiUninit();
  166.     result := false;
  167.     exit;
  168.   end;   
  169.   
  170.   if fileexists(localfile) then
  171.     deletefile(localfile);
  172.   fs:=tfileStream.Create(localfile,fmCreate);
  173.   CeReadFile(ahandle,FtempBuffer,sizeof(fTempBuffer),nRead,0);
  174.   if nRead<=0 then
  175.   begin
  176.     MessageBox(0,'数据采集器中没有barcode.txt文件,无法导出!','错误提示:',MB_OK OR MB_ICONWARNING);
  177.     fs.Free;
  178.     CeRapiUninit();
  179.     Exit;
  180.   end;
  181.   while nRead>0 do   
  182.   begin   
  183.     fs.Write(fTempBuffer,nRead);
  184.     if not CeReadFile(ahandle,FtempBuffer,sizeof(fTempBuffer),nRead,0then
  185.     begin   
  186.       result:=false;
  187.       break;
  188.     end;   
  189.   end;   
  190.   CeCloseHandle(ahandle);
  191.   fs.Free;   
  192.   CeRapiUninit();
  193. end;
  194. procedure TForm1.RzBitBtn1Click(Sender: TObject);
  195. var
  196.   ri: TRapiInit;
  197.   hRes: HRESULT;
  198.   dwRet: DWORD;
  199.   fpath, f: string;
  200.   sa: SECURITY_ATTRIBUTES;
  201.   Msg: string;
  202. begin
  203.   Msg := '1、请检查设备是否正确连接!'+#13+'2、系统中是否安装通讯软件Microsoft ActiveSync V4.5!';
  204.   fpath := 'C:/ShineJit/';
  205.   f := 'barcode.txt';
  206.   if not DirectoryExists(fpath) then
  207.   begin
  208.     sa.nLength := SizeOf(sa);
  209.     sa.lpSecurityDescriptor := 0;
  210.     sa.bInheritHandle := True;
  211.     CreateDirectory(PChar(fpath),sa.lpSecurityDescriptor);
  212.   end;
  213.   ri.cbSize := sizeof(ri);
  214.   hRes := CeRapiInitEx(ri);
  215.   dwRet := WaitForSingleObject(ri.heRapiInit, 2000);
  216.   if ((dwRet <> WAIT_OBJECT_0) or (SUCCEEDED(ri.hrRapiInit) = FALSE)) then
  217.     begin
  218.         // Could not initialize Rapi
  219.     MessageBox(0,PChar(Msg),'导出失败:',MB_OK OR MB_ICONWARNING);
  220.         CeRapiUninit;
  221.     Exit;
  222.     end;
  223.   if copyFileFromDevice(fpath+'barcode.txt','barcode.txt'and CeDeleteFile(f) then
  224.     MessageBox(Handle,'数据下载成功!','提示:',MB_OK OR MB_ICONINFORMATION);
  225. end;
  226. procedure TForm1.FormCreate(Sender: TObject);
  227. begin
  228.   DccMan := TDccMan.Create;
  229.   DccMan.OnChange := ChangeActif;
  230.   DccMan.Advise;
  231. end;
  232. end.

其中需要创建一个AutoSink单元,代码暂不提供!呵呵!!!

原创粉丝点击