Z
zzutrain
Unregistered / Unconfirmed
GUEST, unregistred user!
如上,可不可以知道热插拔的是什么硬件设备?给出原代码者,得250分,不够可以再加!
<br>unit Main;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, shlobj, Activex;<br><br>type<br> PSHNOTIFYSTRUCT = ^SHNOTIFYSTRUCT;<br> SHNOTIFYSTRUCT = record<br> dwItem1: PItemIDList;<br> dwItem2: PItemIDList;<br> end;<br>type<br> PSHFileInfoByte = ^SHFileInfoByte;<br> _SHFileInfoByte = record<br> HICON: Integer;<br> iIcon: Integer;<br> dwAttributes: Integer;<br> szDisplayName: array[0..259] of Char;<br> szTypeName: array[0..79] of Char;<br> end;<br> SHFileInfoByte = _SHFileInfoByte;<br>type<br> PIDLSTRUCT = ^IDLSTRUCT;<br> _IDLSTRUCT = record<br> pidl: PItemIDList;<br> bWatchSubFolders: Boolean;<br> end;<br> IDLSTRUCT = _IDLSTRUCT;<br><br>type<br> TFormMain = class(TForm)<br> Button1: TButton;<br> Memo1: TMemo;<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure Button1Click(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> procedure WMShellReg(var Message: TMessage); message WM_NOTIFY;<br> public<br> { Public declarations }<br> end;<br><br>function SHNotify_Register(HWND: Integer): BOOL;<br>function SHNotify_UnRegister: BOOL;<br>function SHEventName(strPath1, strPath2: string; LPARAM: Integer): string;<br><br>{$WARN SYMBOL_PLATFORM OFF}<br>function SHChangeNotifyDeregister(hNotify: Integer): Integer; stdcall;<br>external 'Shell32.dll' Index 4;<br><br>function SHChangeNotifyRegister<br> (HWND, uFlags, dwEventID, uMsg, cItems: LongWord;<br> lpps: PIDLSTRUCT): Integer; stdcall;<br>external 'Shell32.dll' Index 2;<br>{$WARN SYMBOL_PLATFORM ON}<br><br>var<br> FormMain: TFormMain;<br> m_hSHNotify: Integer;<br> m_pidlDesktop: PItemIDList;<br> ps: PIDLSTRUCT;<br><br>implementation<br>{$R *.DFM}<br><br>function SHEventName(strPath1, strPath2: string; LPARAM: Integer): string;<br>var<br> sEvent: string;<br>begin<br> case LPARAM of //根据参数设置提示消息<br> SHCNE_RENAMEITEM: sEvent := '重命名文件' + strPath1 + '为' + strpath2;<br> SHCNE_CREATE: sEvent := '建立文件 文件名:' + strPath1;<br> SHCNE_DELETE: sEvent := '删除文件 文件名:' + strPath1;<br> SHCNE_MKDIR: sEvent := '新建目录 目录名:' + strPath1;<br> SHCNE_RMDIR: sEvent := '删除目录 目录名:' + strPath1;<br> SHCNE_MEDIAINSERTED: sEvent := strPath1 + '中插入可移动存储介质';<br> SHCNE_MEDIAREMOVED: sEvent := strPath1 + '中移去可移动存储介质' + strPath1 + ' ' + strpath2;<br> SHCNE_DRIVEREMOVED: sEvent := '移去驱动器' + strPath1;<br> SHCNE_DRIVEADD: sEvent := '添加驱动器' + strPath1;<br> SHCNE_NETSHARE: sEvent := '改变目录' + strPath1 + '的共享属性';<br> SHCNE_ATTRIBUTES: sEvent := '改变文件目录属性 文件名' + strPath1;<br> SHCNE_UPDATEDIR: sEvent := '更新目录' + strPath1;<br> SHCNE_UPDATEITEM: sEvent := '更新文件 文件名:' + strPath1;<br> SHCNE_SERVERDISCONNECT: sEvent := '断开与服务器的连接' + strPath1 + ' ' + strpath2;<br> SHCNE_UPDATEIMAGE: sEvent := 'SHCNE_UPDATEIMAGE';<br> SHCNE_DRIVEADDGUI: sEvent := 'SHCNE_DRIVEADDGUI';<br> SHCNE_RENAMEFOLDER: sEvent := '重命名文件夹' + strPath1 + '为' + strpath2;<br> SHCNE_FREESPACE: sEvent := '磁盘空间大小改变';<br> SHCNE_ASSOCCHANGED: sEvent := '改变文件关联';<br> else<br> sEvent := '未知操作' + IntToStr(LPARAM);<br> end;<br> Result := sEvent;<br>end;<br><br>function SHNotify_Register(HWND: Integer): BOOL;<br>begin<br> {$R-}<br> Result := False;<br> if m_hSHNotify = 0 then<br> begin<br> //获取桌面文件夹的Pidl<br> if SHGetSpecialFolderLocation(HWND, CSIDL_DESKTOP, m_pidlDesktop) <> NOERROR then<br> RaiseLastOSError;<br> <br> if Boolean(m_pidlDesktop) then<br> begin<br> New(ps);<br> ps.bWatchSubFolders := True;<br> ps.pidl := m_pidlDesktop;<br> // 利用SHChangeNotifyRegister函数注册系统消息处理<br> m_hSHNotify := SHChangeNotifyRegister(HWND, (SHCNF_TYPE or SHCNF_IDLIST),<br> (SHCNE_ALLEVENTS or SHCNE_INTERRUPT),<br> WM_NOTIFY, 1, ps);<br> Result := Boolean(m_hSHNotify);<br> end<br> else<br> // 如果出现错误就使用 CoTaskMemFree函数来释放句柄<br> CoTaskMemFree(m_pidlDesktop);<br> end;<br> {$R+}<br>end;<br><br>function SHNotify_UnRegister: BOOL;<br>begin<br> Result := False;<br> if Boolean(m_hSHNotify) then<br> //取消系统消息监视,同时释放桌面的Pidl<br> if Boolean(SHChangeNotifyDeregister(m_hSHNotify)) then<br> begin<br> {$R-}<br> m_hSHNotify := 0;<br> Dispose(ps);<br> CoTaskMemFree(m_pidlDesktop);<br> Result := True;<br> {$R-}<br> end;<br>end;<br><br>procedure TFormMain.WMShellReg(var Message: TMessage); //系统消息处理函数<br>var<br> strPath1, strPath2: string;<br> charPath: array[0..259] of Char;<br> pidlItem: PSHNOTIFYSTRUCT;<br>begin<br> pidlItem := PSHNOTIFYSTRUCT(Message.WPARAM);<br> //获得系统消息相关得路径<br> SHGetPathFromIDList(pidlItem.dwItem1, charPath);<br> strPath1 := charPath;<br> SHGetPathFromIDList(pidlItem.dwItem2, charPath);<br> strPath2 := charPath;<br> Memo1.Lines.Add(SHEvEntName(strPath1, strPath2, Message.LPARAM));<br>end;<br><br>procedure TFormMain.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> //在程序退出的同时删除监视<br> if Boolean(m_pidlDesktop) then<br> SHNotify_Unregister;<br>end;<br><br>procedure TFormMain.Button1Click(Sender: TObject); //Button1的Click消息<br>begin<br> m_hSHNotify := 0;<br> if SHNotify_Register(Handle) then<br> begin //注册Shell监视<br> ShowMessage('Shell监视程序成功注册');<br> Button1.Enabled := False;<br> end<br> else<br> ShowMessage('Shell监视程序注册失败');<br>end;<br><br>procedure TFormMain.FormCreate(Sender: TObject);<br>begin<br> Button1.Caption := '打开监视';<br>end;<br><br>end.<br>