delphi怎么实现USB数据的传输控制 ( 积分: 100 )

  • 主题发起人 主题发起人 zglikai
  • 开始时间 开始时间
Z

zglikai

Unregistered / Unconfirmed
GUEST, unregistred user!
我们有一个机子,里面有一些大图片,想做一个通过USB接口,对每次从机子拷走的图片,进行统计:图片数,图片名称。超过一定数量,禁止USB数据的传输.的小程序。<br><br> 请个位仁兄,提拱宝贵资料,将十分感激。
 
我们有一个机子,里面有一些大图片,想做一个通过USB接口,对每次从机子拷走的图片,进行统计:图片数,图片名称。超过一定数量,禁止USB数据的传输.的小程序。<br><br> 请个位仁兄,提拱宝贵资料,将十分感激。
 
做个文件夹映射就可以了<br>不要你写驱动的
 
这段代码可以监控所有的设备操作,当你监控到USB设备有读写操作时,即可进行统计,自己修改吧。<br>能用了,最好是发一份给我。<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls, shlobj, Activex;<br><br>const<br> &nbsp;SHCNE_RENAMEITEM = $1;<br> &nbsp;SHCNE_CREATE = $2;<br> &nbsp;SHCNE_DELETE = $4;<br> &nbsp;SHCNE_MKDIR = $8;<br> &nbsp;SHCNE_RMDIR = $10;<br> &nbsp;SHCNE_MEDIAINSERTED = $20;<br> &nbsp;SHCNE_MEDIAREMOVED = $40;<br> &nbsp;SHCNE_DRIVEREMOVED = $80;<br> &nbsp;SHCNE_DRIVEADD = $100;<br> &nbsp;SHCNE_NETSHARE = $200;<br> &nbsp;SHCNE_NETUNSHARE = $400;<br> &nbsp;SHCNE_ATTRIBUTES = $800;<br> &nbsp;SHCNE_UPDATEDIR = $1000;<br> &nbsp;SHCNE_UPDATEITEM = $2000;<br> &nbsp;SHCNE_SERVERDISCONNECT = $4000;<br> &nbsp;SHCNE_UPDATEIMAGE = $8000;<br> &nbsp;SHCNE_DRIVEADDGUI = $10000;<br> &nbsp;SHCNE_RENAMEFOLDER = $20000;<br> &nbsp;SHCNE_FREESPACE = $40000;<br> &nbsp;SHCNE_ASSOCCHANGED = $8000000;<br> &nbsp;SHCNE_DISKEVENTS = $2381F;<br> &nbsp;SHCNE_GLOBALEVENTS = $C0581E0;<br> &nbsp;SHCNE_ALLEVENTS = $7FFFFFFF;<br> &nbsp;SHCNE_INTERRUPT = $80000000;<br><br> &nbsp;SHCNF_IDLIST = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;LPITEMIDLIST<br> &nbsp;SHCNF_PATHA = $1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // path name<br> &nbsp;SHCNF_PRINTERA = $2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// printer friendly name<br> &nbsp;SHCNF_DWORD = $3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // DWORD<br> &nbsp;SHCNF_PATHW = $5; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // path name<br> &nbsp;SHCNF_PRINTERW = $6; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// printer friendly name<br> &nbsp;SHCNF_TYPE = $FF;<br><br> &nbsp;SHCNF_FLUSH = $1000;<br><br> &nbsp;SHCNF_FLUSHNOWAIT = $2000;<br> &nbsp;SHCNF_PATH = SHCNF_PATHW;<br> &nbsp;SHCNF_PRINTER = SHCNF_PRINTERW;<br><br> &nbsp;WM_SHNOTIFY = $401;<br> &nbsp;NOERROR = 0;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Memo1: TMemo;<br> &nbsp; &nbsp;procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp; &nbsp;procedure WMShellReg(var Message: TMessage); message WM_SHNOTIFY;<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>type PSHNOTIFYSTRUCT = ^SHNOTIFYSTRUCT;<br> &nbsp;SHNOTIFYSTRUCT = record<br> &nbsp; &nbsp;dwItem1: PItemIDList;<br> &nbsp; &nbsp;dwItem2: PItemIDList;<br> &nbsp;end;<br><br>type PSHFileInfoByte = ^SHFileInfoByte;<br> &nbsp;_SHFileInfoByte = record<br> &nbsp; &nbsp;hIcon: Integer;<br> &nbsp; &nbsp;iIcon: Integer;<br> &nbsp; &nbsp;dwAttributes: Integer;<br> &nbsp; &nbsp;szDisplayName: array[0..259] of char;<br> &nbsp; &nbsp;szTypeName: array[0..79] of char;<br> &nbsp;end;<br> &nbsp;SHFileInfoByte = _SHFileInfoByte;<br><br>type PIDLSTRUCT = ^IDLSTRUCT;<br> &nbsp;_IDLSTRUCT = record<br> &nbsp; &nbsp;pidl: PItemIDList;<br> &nbsp; &nbsp;bWatchSubFolders: Integer;<br> &nbsp;end;<br> &nbsp;IDLSTRUCT = _IDLSTRUCT;<br><br>function SHNotify_Register(hWnd: Integer): Bool;<br>function SHNotify_UnRegister: Bool;<br>function SHEventName(strPath1, strPath2: string; lParam: Integer): string;<br><br>function SHChangeNotifyDeregister(hNotify: Integer): Integer; stdcall;<br>external 'Shell32.dll' index 4;<br>function SHChangeNotifyRegister(hWnd, uFlags, dwEventID, uMSG, cItems: longWord;<br> &nbsp;lpps: PIDLSTRUCT): Integer; stdcall; external 'Shell32.dll' index 2;<br>function SHGetFileInfoPidl(pidl: PItemIDList;<br> &nbsp;dwFileAttributes: Integer;<br> &nbsp;psfib: PSHFileInfoByte;<br> &nbsp;cbFileInfo: Integer;<br> &nbsp;uFlags: Integer): Integer; stdcall;<br>external 'Shell32.dll' name 'SHGetFileInfoA';<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;m_hSHNotify: Integer;<br> &nbsp;m_pidlDesktop: PItemIDList;<br><br>implementation<br><br>{$R *.DFM}<br><br>function SHEventName(strPath1, strPath2: string; lParam: Integer): string;<br>var<br> &nbsp;sEvent: string;<br>begin<br> &nbsp;case lParam of<br> &nbsp; &nbsp;SHCNE_RENAMEITEM: sEvent := '重命名文件' + strPath1 + '为' + strPath2;<br> &nbsp; &nbsp;SHCNE_CREATE: sEvent := '建立文件 文件名:' + strPath1;<br> &nbsp; &nbsp;SHCNE_DELETE: sEvent := '删除文件 文件名:' + strPath1;<br> &nbsp; &nbsp;SHCNE_MKDIR: sEvent := '新建目录 目录名:' + strPath1;<br> &nbsp; &nbsp;SHCNE_RMDIR: sEvent := '删除目录 目录名:' + strPath1;<br> &nbsp; &nbsp;SHCNE_MEDIAINSERTED: sEvent := strPath1 + '中插入可移动存储介质';<br> &nbsp; &nbsp;SHCNE_MEDIAREMOVED: sEvent := strPath1 + '中移去可移动存储介质' + strPath1 + ' ' + strPath2;<br> &nbsp; &nbsp;SHCNE_DRIVEREMOVED: sEvent := '移去驱动器' + strPath1;<br> &nbsp; &nbsp;SHCNE_DRIVEADD: sEvent := '添加驱动器' + strPath1;<br> &nbsp; &nbsp;SHCNE_NETSHARE: sEvent := '设置目录' + strPath1 + '的共享属性';<br> &nbsp; &nbsp;SHCNE_NETUNSHARE: sEvent := '取消目录' + strPath1 + '的共享属性';<br> &nbsp; &nbsp;SHCNF_PRINTERW: sEvent := '****打印*****';<br> &nbsp; &nbsp;SHCNE_ATTRIBUTES: sEvent := '改变文件目录属性 文件名' + strPath1;<br> &nbsp; &nbsp;SHCNE_UPDATEDIR: sEvent := '更新目录' + strPath1;<br> &nbsp; &nbsp;SHCNE_UPDATEITEM: sEvent := '更新文件 文件名:' + strPath1;<br> &nbsp; &nbsp;SHCNE_SERVERDISCONNECT: sEvent := '断开与服务器的连接' + strPath1 + ' ' + strPath2;<br> &nbsp; &nbsp;SHCNE_UPDATEIMAGE: sEvent := 'SHCNE_UPDATEIMAGE';<br> &nbsp; &nbsp;SHCNE_DRIVEADDGUI: sEvent := 'SHCNE_DRIVEADDGUI';<br> &nbsp; &nbsp;SHCNE_RENAMEFOLDER: sEvent := '重命名文件夹' + strPath1 + '为' + strPath2;<br> &nbsp; &nbsp;SHCNE_FREESPACE: sEvent := '磁盘空间大小改变';<br> &nbsp; &nbsp;SHCNE_ASSOCCHANGED: sEvent := '改变文件关联';<br><br> &nbsp;else<br> &nbsp; &nbsp;sEvent := '未知操作' + IntToStr(lParam);<br> &nbsp;end;<br> &nbsp;Result := sEvent;<br>end;<br><br>function SHNotify_Register(hWnd: Integer): Bool;<br>var<br> &nbsp;ps: PIDLSTRUCT;<br>begin<br>{$R-}<br> &nbsp;Result := False;<br> &nbsp;if m_hSHNotify = 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;if SHGetSpecialFolderLocation(0, CSIDL_DESKTOP,<br> &nbsp; &nbsp; &nbsp;m_pidlDesktop) &lt;&gt; NOERROR then<br> &nbsp; &nbsp; &nbsp;Form1.close;<br> &nbsp; &nbsp;if Boolean(m_pidlDesktop) then<br> &nbsp; &nbsp;begin<br>// &nbsp; &nbsp; &nbsp;MoveMemory(ps, nil, sizeof(ps));<br> &nbsp; &nbsp; &nbsp;ps := GetMemory(sizeof(ps));<br> &nbsp; &nbsp; &nbsp;ps.pidl := m_pidlDesktop;<br>// &nbsp; &nbsp; &nbsp;MoveMemory(ps.pidl, m_pidlDesktop, (m_pidlDesktop));<br> &nbsp; &nbsp; &nbsp;ps.bWatchSubFolders := 1;<br><br> &nbsp; &nbsp; &nbsp;// 利用SHChangeNotifyRegister函数注册系统消息处理<br> &nbsp; &nbsp; &nbsp;m_hSHNotify := SHChangeNotifyRegister(hWnd,<br> &nbsp; &nbsp; &nbsp; &nbsp;(SHCNF_TYPE or SHCNF_IDLIST),<br> &nbsp; &nbsp; &nbsp; &nbsp;(SHCNE_ALLEVENTS or SHCNE_INTERRUPT),<br> &nbsp; &nbsp; &nbsp; &nbsp;WM_SHNOTIFY, 1, ps);<br><br> &nbsp; &nbsp; &nbsp;Result := Boolean(m_hSHNotify);<br> &nbsp; &nbsp; &nbsp;FreeMemory(ps);<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;// 如果出现错误就使用 CoTaskMemFree函数来释放句柄<br> &nbsp; &nbsp; &nbsp;CoTaskMemFree(m_pidlDesktop);<br> &nbsp;end;<br>{$R+}<br>end;<br><br>function SHNotify_UnRegister: Bool;<br>begin<br> &nbsp;Result := False;<br> &nbsp;if Boolean(m_hSHNotify) then<br> &nbsp; &nbsp;//取消系统消息监视,同时释放桌面的Pidl<br> &nbsp; &nbsp;if Boolean(SHChangeNotifyDeregister(m_hSHNotify)) then<br> &nbsp; &nbsp;begin<br>{$R-}<br> &nbsp; &nbsp; &nbsp;m_hSHNotify := 0;<br> &nbsp; &nbsp; &nbsp;CoTaskMemFree(m_pidlDesktop);<br> &nbsp; &nbsp; &nbsp;Result := True;<br>{$R-}<br> &nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.WMShellReg(var Message: TMessage); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //系统消息处理函数<br>var<br> &nbsp;strPath1, strPath2: string;<br> &nbsp;charPath: array[0..259] of char;<br> &nbsp;pidlItem: PSHNOTIFYSTRUCT;<br>begin<br> &nbsp;pidlItem := PSHNOTIFYSTRUCT(Message.wParam);<br> &nbsp;//获得系统消息相关得路径<br> &nbsp;SHGetPathFromIDList(pidlItem.dwItem1, charPath);<br> &nbsp;strPath1 := charPath;<br> &nbsp;SHGetPathFromIDList(pidlItem.dwItem2, charPath);<br> &nbsp;strPath2 := charPath;<br><br> &nbsp;Memo1.Lines.Add(SHEventName(strPath1, strPath2, Message.lParam) + chr(13) + chr(10));<br> &nbsp;Application.ProcessMessages;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> &nbsp;//在程序退出的同时删除监视<br> &nbsp;if Boolean(m_pidlDesktop) then<br> &nbsp; &nbsp;SHNotify_UnRegister;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Button1的Click消息<br>begin<br> &nbsp;m_hSHNotify := 0;<br> &nbsp;if SHNotify_Register(Form1.Handle) then<br> &nbsp;begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //注册Shell监视<br> &nbsp; &nbsp;ShowMessage('Shelling...');<br> &nbsp; &nbsp;Button1.Enabled := False;<br> &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;ShowMessage('Shell Error');<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> &nbsp;Button1.Caption := 'Go!';<br>end;<br><br>end.
 
to vpanel<br>可以编译通过并运行,但是一些不准,比如:<br><br>打印方面,就识别为对文件属性的操作.但文件名是空的. 未识给出 LPTn &nbsp;USBn 等文件名.<br><br>U盘操作方面是正确的(文件系统操作正确).<br><br>不知各位用过的朋友用得如何??
 

Similar threads

回复
0
查看
978
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部