求助,要做一个监控硬件设备变动或使用的程序,如插入拨出U盘,设备禁用启用,在光驱中放入光盘等,有没有大侠做过类似的东东?请指教。(200分)

  • 主题发起人 主题发起人 kingbenz
  • 开始时间 开始时间
K

kingbenz

Unregistered / Unconfirmed
GUEST, unregistred user!
求助,要做一个监控硬件设备变动或使用的程序,如插入拨出U盘,设备禁用启用,在光驱中放入光盘等,有没有大侠做过类似的东东?请指教。
 
俺正好做过,wm_deivecchange就行了,否则的话就自己做驱动了
 
wm_deivecchange能知道是哪个设备变化的消息吗?可不可以贴一段代码出来参考一下。
 
给你个,这个是监视U盘变化的

//pas
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellApi;

type
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
OldWindowProc: TWndMethod;
procedure FormWndProc(var Message: TMessage);
public
{ Public declarations }
end;

var
Form1: TForm1;


implementation

{$R *.dfm}


const
DBT_DEVICEARRIVAL = $8000; // system detected a new device
DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone
DBT_DEVTYP_VOLUME = $00000002; // logical volume
DBTF_MEDIA = $0001; // media comings and goings
type
PDEV_BROADCAST_HDR = ^TDEV_BROADCAST_HDR;
TDEV_BROADCAST_HDR = packed record
dbch_size : DWORD;
dbch_devicetype : DWORD;
dbch_reserved : DWORD;
end;
PDEV_BROADCAST_VOLUME = ^TDEV_BROADCAST_VOLUME;
TDEV_BROADCAST_VOLUME = packed record
dbcv_size : DWORD;
dbcv_devicetype : DWORD;
dbcv_reserved : DWORD;
dbcv_unitmask : DWORD;
dbcv_flags : WORD;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
OldWindowProc:= WindowProc;
WindowProc := FormWndProc;
end;

procedure TForm1.FormWndProc(var Message: TMessage);
var
lpdb : PDEV_BROADCAST_HDR;
lpdbv : PDEV_BROADCAST_VOLUME;
unitmask:DWORD;
i: Integer;
begin
lpdb := PDEV_BROADCAST_HDR(Message.LParam);

OldWindowProc(Message);

if (Message.Msg=WM_DEVICECHANGE) then
begin
if (Message.WParam=DBT_DEVICEARRIVAL) then
begin
Memo1.Lines.Add('Usb Disk Insert!');
if lpdb.dbch_devicetype=DBT_DEVTYP_VOLUME then
begin
lpdbv := PDEV_BROADCAST_VOLUME(lpdb);
unitmask:=lpdbv.dbcv_unitmask;
for i:=0 to 25 do
begin
if Boolean(unitmask and $1)then
break;
unitmask := unitmask shr 1;
end;
Memo1.Lines.Add('USB No : ' + Char(Ord('A')+i) );
end;
end;

if (Message.WParam=DBT_DEVICEREMOVECOMPLETE) then
begin
Memo1.Lines.Add('Usb Disk 退出!');
end;
end;
end;

end.
//dfm

object Form1: TForm1
Left = 192
Top = 107
Width = 382
Height = 295
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 8
Top = 8
Width = 361
Height = 249
Lines.Strings = (
'Memo1')
TabOrder = 0
end
end
 
感谢apiao,从消息中能识别出是插入光盘还是插入U盘等设备吗?
 
用GetDriveType()函数
将得到的盘符代入,即可得到磁盘类型
U只能识别成可移动磁盘
你看看在“我的电脑”中,显示的U盘标记就是可移动磁盘

//声明:
GetDriveType(
lpRootPathName: PChar {磁盘根目录}
): UINT;

//返回值:
DRIVE_UNKNOWN = 0; {未知}
DRIVE_NO_ROOT_DIR = 1; {可移动磁盘}
DRIVE_REMOVABLE = 2; {软盘}
DRIVE_FIXED = 3; {本地硬盘}
DRIVE_REMOTE = 4; {网络磁盘}
DRIVE_CDROM = 5; {CD-ROM}
DRIVE_RAMDISK = 6; {RAM 磁盘
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部