有关U盘的操作!!(50分)

  • 主题发起人 主题发起人 hjiaming
  • 开始时间 开始时间
H

hjiaming

Unregistered / Unconfirmed
GUEST, unregistred user!
如何才能检测到当前已经插入的U盘,并返回其盘符(在驱动已经安装的情况下)?
 
通过SHELL编程可以实现,请看如下代码:<br>unit Unit1;<br><br>interface <br><br>uses <br>Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, <br>StdCtrls,shlobj,Activex;//shlobj2,ShlExt,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; // LPITEMIDLIST<br>&nbsp; SHCNF_PATHA = $1; // path name<br>&nbsp; SHCNF_PRINTERA = $2; // printer friendly name<br>&nbsp; SHCNF_DWORD = $3; // DWORD<br>&nbsp; SHCNF_PATHW = $5; // path name<br>&nbsp; SHCNF_PRINTERW = $6; // 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; Button1: TButton;<br>&nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; { Private declarations }<br>&nbsp; procedure WMShellReg(var Message:TMessage);message WM_SHNOTIFY;<br>&nbsp; public<br>&nbsp; function SHNotify_Register(hWnd : Integer) : Bool;<br>&nbsp; function SHNotify_UnRegister:Bool;<br>&nbsp; { Public declarations }<br>end;<br><br>type PSHNOTIFYSTRUCT=^SHNOTIFYSTRUCT;<br>SHNOTIFYSTRUCT = record<br>&nbsp; dwItem1 : PItemIDList;<br>&nbsp; dwItem2 : PItemIDList;<br>end;<br><br>Type PSHFileInfoByte=^SHFileInfoByte;<br>_SHFileInfoByte = record<br>&nbsp; hIcon :Integer;<br>&nbsp; iIcon :Integer;<br>&nbsp; dwAttributes : Integer;<br>&nbsp; szDisplayName : array [0..259] of char;<br>&nbsp; szTypeName : array [0..79] of char;<br>end;<br>SHFileInfoByte=_SHFileInfoByte;<br><br>Type PIDLSTRUCT = ^IDLSTRUCT;<br>_IDLSTRUCT = record<br>&nbsp; pidl : PItemIDList;<br>&nbsp; bWatchSubFolders : Integer;<br>end;<br>IDLSTRUCT =_IDLSTRUCT;<br><br><br><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>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>&nbsp; external 'Shell32.dll' name 'SHGetFileInfoA';<br><br>var<br>Form1: TForm1;<br>m_hSHNotify:Integer;<br>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 //file://根据参数设置提示消息<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><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>&nbsp; else<br>&nbsp; &nbsp; sEvent:='未知操作'+IntToStr(lParam)+strPath1+' '+strpath2;;<br>&nbsp; end;<br>&nbsp; Result:=sEvent;<br>end;<br><br>function TForm1.SHNotify_Register(hWnd : Integer) : Bool;<br>var<br>&nbsp; ps:PIDLSTRUCT;<br>&nbsp; Comp: TComponent;<br>&nbsp; handle : THandle;<br>begin<br>&nbsp; {$R-}<br><br>&nbsp; Result:=False;<br>&nbsp; Comp := self.Owner;<br>&nbsp; //handle := (Comp as TForm).handle;<br>&nbsp; //handle := Comp.handle;<br>&nbsp; If m_hSHNotify = 0 then begin<br>&nbsp; &nbsp; //file://获取桌面文件夹的Pidl<br>&nbsp; &nbsp; if SHGetSpecialFolderLocation(self.Handle, CSIDL_DESKTOP,m_pidlDesktop)&lt;&gt; NOERROR then begin<br><br>&nbsp; &nbsp; &nbsp; Form1.close;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; //showmessage(m_pidlDesktop.<br>&nbsp; if Boolean(m_pidlDesktop) then begin<br>&nbsp; &nbsp; showmessage('ok');<br>&nbsp; &nbsp; ps.bWatchSubFolders := 1;<br>&nbsp; &nbsp; ps.pidl := m_pidlDesktop;<br><br>&nbsp; &nbsp; &nbsp;//利用SHChangeNotifyRegister函数注册系统消息处理<br>&nbsp; &nbsp; m_hSHNotify := SHChangeNotifyRegister(hWnd, (SHCNF_TYPE Or SHCNF_IDLIST),<br>&nbsp; &nbsp; (SHCNE_ALLEVENTS Or SHCNE_INTERRUPT),<br>&nbsp; &nbsp; WM_SHNOTIFY, 1, ps);<br>&nbsp; &nbsp; Result := Boolean(m_hSHNotify);<br>&nbsp; end<br>&nbsp; Else<br>&nbsp; &nbsp; // 如果出现错误就使用 CoTaskMemFree函数来释放句柄<br>&nbsp; &nbsp; CoTaskMemFree(m_pidlDesktop);<br>&nbsp; End;<br>&nbsp; {$R+}<br>end;<br><br>function TForm1.SHNotify_UnRegister:Bool;<br>begin<br>&nbsp; Result:=False;<br>&nbsp; If Boolean(m_hSHNotify) Then<br>&nbsp; //file://取消系统消息监视,同时释放桌面的Pidl<br>&nbsp; If Boolean(SHChangeNotifyDeregister(m_hSHNotify)) Then begin<br>&nbsp; &nbsp; {$R-}<br>&nbsp; &nbsp; m_hSHNotify := 0;<br>&nbsp; &nbsp; CoTaskMemFree(m_pidlDesktop);<br>&nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; {$R-}<br>&nbsp; End;<br>end;<br><br>procedure TForm1.WMShellReg(var Message:TMessage); //file://系统消息处理函数<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>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; //在程序退出的同时删除监视<br>&nbsp; if Boolean(m_pidlDesktop) then<br>&nbsp; SHNotify_Unregister;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);// file://Button1的Click消息<br>begin<br>&nbsp; m_hSHNotify:=0;<br>&nbsp; if SHNotify_Register(Form1.Handle) then begin //file://注册Shell监视<br>&nbsp; &nbsp; ShowMessage('Shell监视程序成功注册');<br>&nbsp; &nbsp; Button1.Enabled := False;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; ShowMessage('Shell监视程序注册失败');<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Button1.Caption := '打开监视';<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; memo1.Lines.Clear ;<br>end;<br><br>end.<br><br>
 
处理一下WM_DEVICECHANGE消息看看行不行
 
这是完全的代码吗???<br>我做了一个hid的但总有问题出现。能给我一个完全的吗???我想看看高手是怎么写的!!!<br>谢谢了!!!<br>discoveryer3000@yahoo.com.cn<br>qq:121144437
 
看不明白前面的代码,那些常数定义是如何可知的????<br>菜鸟学习中………
 
to 凌未风:<br>这是一个完整的代码,我做的一个测试程序,感觉不错。在win2000,d6下调试通过!
 
后退
顶部