我用下面的程序能监控到光驱、USB设备的插入与拔出,但不知道具体是光驱还是USB设备?怎么半????
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure WMDEVICECHANGE(var msgx: Tmessage); message WM_DEVICECHANGE;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}procedure Tform1.WMDEVICECHANGE(var msgx: Tmessage);
const
  DBT_DEVICEARRIVAL = $8000;
  DBT_DEVICEREMOVECOMPLETE = $8004;
begin
  inherited;
  case msgx.WParam of
    DBT_DEVICEARRIVAL:
      Caption := '有了!';
    DBT_DEVICEREMOVECOMPLETE:
      Caption := '取走了';
  end;
end;end.

解决方案 »

  1.   

    呵呵……现在的电脑百分百都有usb哩。
      

  2.   

    用GetDriveType 过滤一下
    Value Meaning 
    DRIVE_UNKNOWN The drive type cannot be determined. 
    DRIVE_NO_ROOT_DIR The root directory does not exist. 
    DRIVE_REMOVABLE The disk can be removed from the drive. 
    DRIVE_FIXED The disk cannot be removed from the drive. 
    DRIVE_REMOTE The drive is a remote (network) drive. 
    DRIVE_CDROM The drive is a CD-ROM drive. 
    DRIVE_RAMDISK The drive is a RAM disk.
      

  3.   

    列举所有磁盘类型procedure TForm1.Button1Click(Sender: TObject);
    var
     Drivetype:Integer;
     root:String;
     i:Integer;
    begin  for i :=65  to 91 do
      begin
         root:=char(i)+':\';
         Drivetype:=GetDriveType(pChar(root));
         case Drivetype of
          DRIVE_UNKNOWN : CheckListBox1.Items.Add(root+'未知');
          DRIVE_NO_ROOT_DIR:;  //不存在     
          DRIVE_REMOVABLE:CheckListBox1.Items.Add(root+'移动磁盘(包括软盘和U盘等)');
          DRIVE_FIXED:CheckListBox1.Items.Add(root+'硬盘');
          DRIVE_REMOTE:CheckListBox1.Items.Add(root+'网络磁盘');
          DRIVE_CDROM:CheckListBox1.Items.Add(root+'光驱');
          DRIVE_RAMDISK:CheckListBox1.Items.Add(root+'虚拟磁盘');
        end;
      end;
    end;
      

  4.   

    对 yangfan369(扬帆远行) 举例的说明
     我用你的程序,测试的U盘显示的是硬盘类型,根本无法判断是不是U盘
      

  5.   

    没办法,我在本机上测试通过,而且一直在用。
    环境WinXP+D6
      

  6.   

    //我在程序中获取U盘的函数,和大家讨论一下,
    //如果哪位试验成功,不要忘了在这儿告知小弟
    function TfrmMain.GetLastMoveDrive: String;
    var
     Drivetype:Integer;
     root:String;
     i:Integer;
    begin
      Result:='';
      for i :=67  to 91 do   //从C:盘开始到Z:盘
      begin
         root:=char(i)+':\';
         Drivetype:=GetDriveType(pChar(root));
         if Drivetype=DRIVE_REMOVABLE then
         begin
           Result:= root;
           Break;
         end;
      end;
    end;
      

  7.   

    我的环境是win2000 advance server + D6 .测试结果未 U盘的Drivertype=DRIVE_FIXED,yangfan369兄说在xp上是可以的,
    但我在朋友的xp上测试也是硬盘类型
      

  8.   

    UNIT DBT_H;INTERFACE  USES
        Windows;  // UNIT, DWORD  CONST
        // Events of WM_DEVICECHANGE (wParam)
        DBT_DEVICEARRIVAL           = $8000;  // system detected a new device
        DBT_DEVICEQUERYREMOVE       = $8001;  // wants to remove, may fail
        DBT_DEVICEQUERYREMOVEFAILED = $8002;  // removal aborted
        DBT_DEVICEREMOVEPENDING     = $8003;  // about to remove, still avail
        DBT_DEVICEREMOVECOMPLETE    = $8004;  // device is gone
        DBT_DEVICETYPESPECIFIC      = $8005;  // type specific event
        DBT_CONFIGCHANGED           = $0018;    // type of device in DEV_BROADCAST_HDR
        DBT_DEVTYP_OEM              = $00000000;  // OEM- or IHV-defined
        DBT_DEVTYP_DEVNODE          = $00000001;  // Devnode number
        DBT_DEVTYP_VOLUME           = $00000002;  // Logical volume
        DBT_DEVTYP_PORT             = $00000003;  // Port (serial or parallel
        DBT_DEVTYP_NET              = $00000004;  // Network resource    // media types in DBT_DEVTYP_VOLUME
        DBTF_MEDIA                  = $0001;      // change affects media in drive
        DBTF_NET                    = $0002;      // logical volume is network volume
      TYPE
        // Instead of TMessage, use this:
        TWMDeviceChange =
          RECORD
            Msg    :  Cardinal;
            Event  :  UINT;
            dwData :  pointer;
            Result :  LongInt;
          END;    // In a DBT_DEVICECHANGE or DBT_DEVICEREMOVECOMPLETE event, dwData contains an
        // address of a DEV_BROADCAST_HDR structure identifying the device inserted.      TDevBroadcastHDR =
            PACKED RECORD
              dbch_size      :  DWORD;
              dbch_devicetype:  DWORD;
              dbch_reserved  :  DWORD;
            END;
          pDevBroadcastHDR = ^TDevBroadcastHDR;    // When the device is of type volume, then we can get some device specific
        // information, namely specific information about a logical volume.
          TDevBroadcastVolume =
          PACKED RECORD
            dbcv_size      :  DWORD;
            dbcv_devicetype:  DWORD;
            dbcv_reserved  :  DWORD;
            dbcv_unitmask  :  DWORD;
            dbcv_flags     :  WORD;
          END;
          pDevBroadcastVolume = ^TDevBroadcastVolume;IMPLEMENTATIONEND.硬件设置发生变化时,系统将会向所有应用程序发送一条WM_DEVICECHANGE消息.当有设备被禁止或激活时(比如我们插入光盘,从光驱中取出光盘,或者在系统设备中删除或禁止打印端口),该消息的wParam参数分别被指定为DBT_DEVICEARRIVAL和DBT_DEVICEREMOVECOMPLETE.此时,lParam参数指向一个DEV_BROADCAST_HDR结构,该结构中的dbch_devicetype成员表识设备的类型,当该值为DBT_DEVTYP_VOLUME时,表明一个带有卷标名的驱动器发生了变化,一般情况下该驱动器既为光驱.
      

  9.   

    http://www.56kc.com/web/commwatch.aspxhttp://www.56kc.com/web/technology/chuangkou.htm
    http://soft.56kc.com/ ->delphi原码-》硬件控制串口重发事例 jpyc 9 2004-8-19 
    串口通讯协议分析事例 jpyc 45 2004-7-6 
    USB访问的源代码 www 801 2004-6-18 
    鼠标键盘动作记录及播放程序源代码 www 99 2004-2-27 
    并口控制(98/2000/xp)