检测到USB时,获取USB名称和DEVINST dnDevInst参数(以在以后删除USB) [英] Get USB Name and DEVINST dnDevInst parameter (to remove USB later) when USB Detected

查看:302
本文介绍了检测到USB时,获取USB名称和DEVINST dnDevInst参数(以在以后删除USB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我对ASP.NET更加熟悉,因此我对此没有太多经验.我确实阅读并检查了CodeProject和其他网站(包括此网站)上的所有示例,草编的野生动物园书籍也阅读了我在Google上找到的100多个链接,但我却做不到 得到它的工作.到目前为止,插入和卸下USB驱动器后,我都能检测到它们, 但我也需要USB名称.

i don't have so much experience with this since i'm more familiar with ASP.NET. I did read and examine all samples from CodeProject and other sites (including this), serched safari books also read more than a 100 links i found on google, but i just can't get it to work. So far i'm able to detect the USB drives when they're inserted and removed, but i need the USB name too.

我还想从C#代码中安全删除USB.我发现 CM_Request_Device_Eject函数可以做到这一点,但是我不知道如何获取DEVINST dnDevInst 必需的参数.

I'd also like to safely remove USB from C# code. I found that CM_Request_Device_Eject Function does that, but i don't know how to get the DEVINST dnDevInst parameter which is required.

代码示例:

void OnDeviceChange(ref Message msg)
{
  int wParam = (int)msg.WParam;
  if (wParam == Win32.DBT_DEVICEARRIVAL) {
    char c;
    Win32.DEV_BROADCAST_VOLUME vol;
    vol = (Win32.DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, typeof(Win32.DEV_BROADCAST_VOLUME));
    c = DriveMaskToLetter(vol.dbcv_unitmask);
    label1.Text = "detected drive:" + c;
    //Need to get USB NAME
    //Need to get dnDevInst
  }
  else if (wParam == Win32.DBT_DEVICEREMOVECOMPLETE)
  {
    char c;
    Win32.DEV_BROADCAST_VOLUME vol;
    vol = (Win32.DEV_BROADCAST_VOLUME)
      Marshal.PtrToStructure(msg.LParam, typeof(Win32.DEV_BROADCAST_VOLUME));
    c = DriveMaskToLetter(vol.dbcv_unitmask);
    label1.Text = "drive " + c + " was removed";
  }
}

StringBuilder sb = new StringBuilder(1024);

Win32.PNP_VETO_TYPE veto;
int hr = Win32.CM_Request_Device_Eject(device.InstanceHandle, out veto, sb, sb.Capacity, 0);
if (hr != 0)
  throw new Win32Exception(hr);

if (veto != Win32.PNP_VETO_TYPE.Ok)
  label1.Text = veto.ToString();

推荐答案

您可以为我们提供更详细的演示以进行测试吗?我想帮忙.您可以直接将演示上传到 http://skydrive.live.com .

Can you provide us with more detailed demo to test?   I'd like to help.  You may directly upload the demo to http://skydrive.live.com.

美好的一天!

谢谢


这篇关于检测到USB时,获取USB名称和DEVINST dnDevInst参数(以在以后删除USB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆