C#15 May 2010 04:52 pm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices
;
namespace WindowsApplication16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_VOLUME
{
public int dbcv_size;
public int dbcv_devicetype;
public int dbcv_reserved;
public int dbcv_unitmask;
}
protected override void WndProc(ref Message m)
{
// 发生设备变动
const int WM_DEVICECHANGE = 0×0219;
// 系统检测到一个新设备
const int DBT_DEVICEARRIVAL = 0×8000;
// 系统完成移除一个设备
const int DBT_DEVICEREMOVECOMPLETE = 0×8001;
// 逻辑卷标
const int DBT_DEVTYP_VOLUME = 0×00000002;
switch (m.Msg)
{
case WM_DEVICECHANGE:
switch (m.WParam.ToInt32())
{
case DBT_DEVICEARRIVAL:
int devType = Marshal.ReadInt32(m.LParam, 4);
if (devType == DBT_DEVTYP_VOLUME)
{
DEV_BROADCAST_VOLUME vol;
vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(
m.LParam, typeof(DEV_BROADCAST_VOLUME));
MessageBox.Show(vol.dbcv_unitmask.ToString(“x”));
}
break;
case DBT_DEVICEREMOVECOMPLETE:
MessageBox.Show(“Removal”);
break;
}
break;
}
base.WndProc(ref m);
}
}
}

Trackback this Post | Feed on comments to this Post

Leave a Reply