在.NET中探测U盘的插入/拔出(C#实现)
原文地址:http://www.cnblogs.com/AndyHai/archive/2007/07/25/830224.html
有同学向我问这个问题,于是就Google了一下找到答案,不过是C下的,我将其改编成了C#的。
当设备被插入/拔出的时候,WINDOWS会向每个窗体发送WM_DEVICECHANGE 消息,当消息的wParam 值等于 DBT_DEVICEARRIVAL 时,表示Media设备被插入并且已经可用;如果wParam值等于DBT_DEVICEREMOVECOMPLETE,表示Media设备已经被移出。
它们的lParam都指向一个 DEV_BROADCAST_HDR结构体,其原形如下:
typedef
struct
_DEV_BROADCAST_HDR
{
DWORD dbch_size;
DWORD dbch_devicetype;
DWORD dbch_reserved;
} DEV_BROADCAST_HDR, * PDEV_BROADCAST_HDR;
{
DWORD dbch_size;
DWORD dbch_devicetype;
DWORD dbch_reserved;
} DEV_BROADCAST_HDR, * PDEV_BROADCAST_HDR;
这个结构体仅仅是一个“头”(HDR),其后还有附加数据,dbch_size表示结构体实例的字节数,当其中的dbch_devicetype字段值等于 DBT_DEVTYP_VOLUME时,表示当前设备是逻辑驱动器,且lParam实际上指向的应该是 DEV_BROADCAST_VOLUME 结构体实例(真佩服这种逻辑), DEV_BROADCAST_VOLUME 结构体原形如下:
typedef
struct
_DEV_BROADCAST_VOLUME
{
DWORD dbcv_size;
DWORD dbcv_devicetype;
DWORD dbcv_reserved;
DWORD dbcv_unitmask;
WORD dbcv_flags;
} DEV_BROADCAST_VOLUME, * PDEV_BROADCAST_VOLUME;
{
DWORD dbcv_size;
DWORD dbcv_devicetype;
DWORD dbcv_reserved;
DWORD dbcv_unitmask;
WORD dbcv_flags;
} DEV_BROADCAST_VOLUME, * PDEV_BROADCAST_VOLUME;
其中 dbcv_unitmask 字段表示当前改变的驱动器掩码,第一位表示驱动器号A,第二位表示驱动器号B,第三位表示驱动器号C,以此类推…… dbcv_flags 表示驱动器的类别,如果等于1,则是光盘驱动器;如果是2,则是网络驱动器;如果是硬盘、U盘则都等于0
所以,我只需要在程序中捕捉 WM_DEVICECHANGE 消息,然后根据具体情况去处理即可,下面是我的测试代码:
using
System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace UDiskDetect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_HDR
{
public UInt32 dbch_size;
public UInt32 dbch_devicetype;
public UInt32 dbch_reserved;
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_VOLUME
{
public UInt32 dbcv_size;
public UInt32 dbcv_devicetype;
public UInt32 dbcv_reserved;
public UInt32 dbcv_unitmask;
public UInt16 dbcv_flags;
}
protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x0219)//WM_DEVICECHANGE
{
switch (m.WParam.ToInt32())
{
case 0x8000://DBT_DEVICEARRIVAL
{
DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME
{
DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
if (dbv.dbcv_flags == 0)
AddVolumes(GetVolumes(dbv.dbcv_unitmask));
}
break;
}
case 0x8004://DBT_DEVICEREMOVECOMPLETE
{
DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME
{
DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
if (dbv.dbcv_flags == 0)
RemoveVolumes(GetVolumes(dbv.dbcv_unitmask));
}
break;
}
}
}
base.DefWndProc(ref m);
}
/**//**//**//// <summary>
/// 根据驱动器掩码返回驱动器号数组
/// </summary>
/// <param name="Mask">掩码</param>
/// <returns>返回驱动器号数组</returns>
public static char[] GetVolumes(UInt32 Mask)
{
List<char> Volumes = new List<char>();
for (int i = 0; i < 32; i++)
{
uint p = (uint)Math.Pow(2, i);
if ((p | Mask) == p)
{
Volumes.Add((char)('A' + i));
}
}
return Volumes.ToArray();
}
public void AddVolumes(char[] Volumes)
{
foreach (char volume in Volumes)
listBox1.Items.Add(volume);
}
public void RemoveVolumes(char[] Volumes)
{
foreach (char volume in Volumes)
listBox1.Items.Remove(volume);
}
}
}
ps:Retrieving dbcc_name string from a DEV_BROADCAST_DEVICEINTERFACE structure
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace UDiskDetect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_HDR
{
public UInt32 dbch_size;
public UInt32 dbch_devicetype;
public UInt32 dbch_reserved;
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_VOLUME
{
public UInt32 dbcv_size;
public UInt32 dbcv_devicetype;
public UInt32 dbcv_reserved;
public UInt32 dbcv_unitmask;
public UInt16 dbcv_flags;
}
protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x0219)//WM_DEVICECHANGE
{
switch (m.WParam.ToInt32())
{
case 0x8000://DBT_DEVICEARRIVAL
{
DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME
{
DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
if (dbv.dbcv_flags == 0)
AddVolumes(GetVolumes(dbv.dbcv_unitmask));
}
break;
}
case 0x8004://DBT_DEVICEREMOVECOMPLETE
{
DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME
{
DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
if (dbv.dbcv_flags == 0)
RemoveVolumes(GetVolumes(dbv.dbcv_unitmask));
}
break;
}
}
}
base.DefWndProc(ref m);
}
/**//**//**//// <summary>
/// 根据驱动器掩码返回驱动器号数组
/// </summary>
/// <param name="Mask">掩码</param>
/// <returns>返回驱动器号数组</returns>
public static char[] GetVolumes(UInt32 Mask)
{
List<char> Volumes = new List<char>();
for (int i = 0; i < 32; i++)
{
uint p = (uint)Math.Pow(2, i);
if ((p | Mask) == p)
{
Volumes.Add((char)('A' + i));
}
}
return Volumes.ToArray();
}
public void AddVolumes(char[] Volumes)
{
foreach (char volume in Volumes)
listBox1.Items.Add(volume);
}
public void RemoveVolumes(char[] Volumes)
{
foreach (char volume in Volumes)
listBox1.Items.Remove(volume);
}
}
}
原文地址: http://bytes.com/topic/c-sharp/answers/244829-retrieving-dbcc_name-string-dev_broadcast_deviceinterface-structure