我正在尝试调整以下项目http://www.codeproject.com/Articles/32026/Capturing-Device-Events-in-aC-Windows-Service以检测 USB 磁盘并将其弹出或停止安装基于在标识字符串上。首先,当到达这部分代码时,项目无法正常运行: if (hdr.dbcc_devicetype == Win32.DBT_DEVTYP_DEVICEINTERFACE) {
Win32.DEV_BROADCAST_DEVICEINTERFACE deviceInterface;
deviceInterface = (Win32.DEV_BROADCAST_DEVICEINTERFACE)
Marshal.PtrToStructure(eventData, typeof(Win32.DEV_BROADCAST_DEVICEINTERFACE));
string name = new string(deviceInterface.dbcc_name);
name = name.Substring(0, name.IndexOf('\0')) + "\\";
StringBuilder stringBuilder = new StringBuilder();
Win32.GetVolumeNameForVolumeMountPoint(name, stringBuilder, 100);
uint stringReturnLength = 0;
string driveLetter = "";
Win32.GetVolumePathNamesForVolumeNameW(stringBuilder.ToString(), driveLetter, (uint) driveLetter.Length, ref stringReturnLength);
if (stringReturnLength == 0)
{
// TODO handle error
}
driveLetter = new string(new char[stringReturnLength]);
if (!Win32.GetVolumePathNamesForVolumeNameW(stringBuilder.ToString(), driveLetter, stringReturnLength, ref stringReturnLength))
{
//// TODO handle error
}
RegisterForHandle(driveLetter[0]);
....} 它永远不会获得驱动器号,驱动器号字符串始终为空白。stringBuilder = ôu<¬ë6 并且名称变量变为 = \?\USBSTOR#Disk&Ven_&Prod_USB_DISK_2.0&Rev_PMAP#07A512076EB115FA&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}\
它出什么问题了?或者关于我想要做什么的任何想法?