如何捕获windows9x/2k热插拔硬件的消息?欢迎参与,一律给分!(300分)

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

zzutrain

Unregistered / Unconfirmed
GUEST, unregistred user!
如上,可不可以知道热插拔的是什么硬件设备?给出原代码者,得250分,不够可以再加!
 
我来拣分!<br>发Email至NeutronBoy@sohu.com<br>我给你Demo.源代码+相关说明!<br>不是我小气,这里贴出来不好看,还有谁要给我Email就行,免费!
 
刚好前两天在做这个东西,把源代码贴上来吧<br>注意,测试还不充分,请自己测试后再使用。<br>下面是我插上USB硬盘后的记录:<br>添加驱动器R:/<br>SHCNE_DRIVEADDGUI<br>添加驱动器S:/<br>SHCNE_DRIVEADDGUI<br>添加驱动器T:/<br>SHCNE_DRIVEADDGUI<br>移去驱动器R:/<br>移去驱动器S:/<br>移去驱动器T:/<br><br>下面为源代码:<br>
代码:
<br>unit Main;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, shlobj, Activex;<br><br>type<br>&nbsp; PSHNOTIFYSTRUCT = ^SHNOTIFYSTRUCT;<br>&nbsp; SHNOTIFYSTRUCT = record<br>&nbsp; &nbsp; dwItem1: PItemIDList;<br>&nbsp; &nbsp; dwItem2: PItemIDList;<br>&nbsp; end;<br>type<br>&nbsp; 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>type<br>&nbsp; PIDLSTRUCT = ^IDLSTRUCT;<br>&nbsp; _IDLSTRUCT = record<br>&nbsp; &nbsp; pidl: PItemIDList;<br>&nbsp; &nbsp; bWatchSubFolders: Boolean;<br>&nbsp; end;<br>&nbsp; IDLSTRUCT = _IDLSTRUCT;<br><br>type<br>&nbsp; TFormMain = 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_NOTIFY;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; 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>&nbsp; (HWND, uFlags, dwEventID, uMsg, cItems: LongWord;<br>&nbsp; lpps: PIDLSTRUCT): Integer; stdcall;<br>external 'Shell32.dll' Index 2;<br>{$WARN SYMBOL_PLATFORM ON}<br><br>var<br>&nbsp; FormMain: TFormMain;<br>&nbsp; m_hSHNotify: Integer;<br>&nbsp; m_pidlDesktop: PItemIDList;<br>&nbsp; ps: PIDLSTRUCT;<br><br>implementation<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_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);<br>&nbsp; end;<br>&nbsp; Result := sEvent;<br>end;<br><br>function SHNotify_Register(HWND: Integer): BOOL;<br>begin<br>&nbsp; {$R-}<br>&nbsp; Result := False;<br>&nbsp; if m_hSHNotify = 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; //获取桌面文件夹的Pidl<br>&nbsp; &nbsp; if SHGetSpecialFolderLocation(HWND, CSIDL_DESKTOP, m_pidlDesktop) &lt;&gt; NOERROR then<br>&nbsp; &nbsp; &nbsp; RaiseLastOSError;<br>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; if Boolean(m_pidlDesktop) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; New(ps);<br>&nbsp; &nbsp; &nbsp; ps.bWatchSubFolders := True;<br>&nbsp; &nbsp; &nbsp; ps.pidl := m_pidlDesktop;<br>&nbsp; &nbsp; &nbsp; // 利用SHChangeNotifyRegister函数注册系统消息处理<br>&nbsp; &nbsp; &nbsp; m_hSHNotify := SHChangeNotifyRegister(HWND, (SHCNF_TYPE or SHCNF_IDLIST),<br>&nbsp; &nbsp; &nbsp; &nbsp; (SHCNE_ALLEVENTS or SHCNE_INTERRUPT),<br>&nbsp; &nbsp; &nbsp; &nbsp; WM_NOTIFY, 1, ps);<br>&nbsp; &nbsp; &nbsp; Result := Boolean(m_hSHNotify);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; // 如果出现错误就使用 CoTaskMemFree函数来释放句柄<br>&nbsp; &nbsp; &nbsp; CoTaskMemFree(m_pidlDesktop);<br>&nbsp; end;<br>&nbsp; {$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>&nbsp; &nbsp; &nbsp; {$R-}<br>&nbsp; &nbsp; &nbsp; m_hSHNotify := 0;<br>&nbsp; &nbsp; &nbsp; Dispose(ps);<br>&nbsp; &nbsp; &nbsp; CoTaskMemFree(m_pidlDesktop);<br>&nbsp; &nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; &nbsp; {$R-}<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure TFormMain.WMShellReg(var Message: TMessage); //系统消息处理函数<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>&nbsp; Memo1.Lines.Add(SHEvEntName(strPath1, strPath2, Message.LPARAM));<br>end;<br><br>procedure TFormMain.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 TFormMain.Button1Click(Sender: TObject); //Button1的Click消息<br>begin<br>&nbsp; m_hSHNotify := 0;<br>&nbsp; if SHNotify_Register(Handle) then<br>&nbsp; begin //注册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 TFormMain.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Button1.Caption := '打开监视';<br>end;<br><br>end.<br>
 
message WM_DEVICECHANGE
 
谢谢楼上的兄弟,我回去试试,回来给分.
 
多人接受答案了。
 
请给一份源码吧? gxlzbig@263.net
 
后退
顶部