Пример #1
0
        public static DeviceData GetDevice(IntPtr lParam)
        {
            DeviceData itemData = new DeviceData();

            if ((int)lParam == 0)
            {
                return(itemData);
            }

            DevBroadcastHdr hdr = new DevBroadcastHdr();

            Marshal.PtrToStructure(lParam, hdr);
            itemData.DeviceType = ((DbtDeviceType)hdr.dbch_devicetype);
            switch ((DbtDeviceType)hdr.dbch_devicetype)
            {
            case DbtDeviceType.DeviceInterface:
                DevBroadcastDeviceinterfaceVariable devIf = new DevBroadcastDeviceinterfaceVariable();

                // Convert lparam to DEV_BROADCAST_DEVICEINTERFACE structure
                Marshal.PtrToStructure(lParam, devIf);

                // Get the device path from the broadcast message
                itemData.Data = new string(devIf.dbcc_name);

                // Remove null-terminated data from the string
                int pos = itemData.Data.IndexOf((char)0);
                if (pos != -1)
                {
                    itemData.Data = itemData.Data.Substring(0, pos);
                }
                break;

            case DbtDeviceType.Port:
                DevBroadcastPortVariable devPort = new DevBroadcastPortVariable();

                // Convert lparam to DEV_BROADCAST_DEVICEINTERFACE structure
                Marshal.PtrToStructure(lParam, devPort);

                // Get the device path from the broadcast message
                itemData.Data = new string(devPort.dbcc_name);

                // Remove null-terminated data from the string
                pos = itemData.Data.IndexOf((char)0);
                if (pos != -1)
                {
                    itemData.Data = itemData.Data.Substring(0, pos);
                }
                break;

            case DbtDeviceType.Volume:
                DevBroadcastVolume volume = new DevBroadcastVolume();
                Marshal.PtrToStructure(lParam, volume);
                itemData.Data = $"{((volume.dbcv_flags & (int) Dbtf.Media) == 1 ? "Media" : "Net")} {FirstDriveFromMask(volume.dbcv_unitmask)}";
                break;
            }
            return(itemData);
        }
 /// <summary>
 /// Get the DevBroadcastVolume
 /// </summary>
 /// <param name="devBroadcastVolume">out DevBroadcastVolume</param>
 /// <returns>bool true the value could be converted</returns>
 public bool TryGetDevBroadcastVolume(out DevBroadcastVolume devBroadcastVolume)
 {
     if (_devBroadcastHeader.DeviceType != DeviceBroadcastDeviceType.Volume)
     {
         devBroadcastVolume = default;
         return(false);
     }
     devBroadcastVolume = Marshal.PtrToStructure <DevBroadcastVolume>(_deviceBroadcastPtr);
     return(true);
 }
Пример #3
0
        /// <summary>
        /// 等待 USB 存储设备接入。
        /// </summary>
        /// <returns>接入的 USB 设备盘符。</returns>
        public static char WaitForUsbDevice()
        {
            DevBroadcastVolume vol = WndProcReceiverForm.WaitForSingleWndProc(
                m =>
                m.Msg == WM_DEVICECHANGE &&
                (int)m.WParam == DBT_DEVICEARRIVAL &&
                Marshal.ReadInt32(m.LParam, 4) == DBT_DEVTYP_VOLUME,
                m =>
                Marshal.PtrToStructure <DevBroadcastVolume>(m.LParam)).Item2;

            BitArray bv     = new BitArray(new int[] { vol.Mask });
            char     letter = '\0';

            for (int i = 0; i < bv.Length; i++)
            {
                if (bv[i])
                {
                    letter = (char)('A' + i);
                }
            }

            return(letter);
        }
Пример #4
0
        private void DeviceChangeNotifier_DeviceNotifyVolume(System.Windows.Forms.Message msg, DevBroadcastVolume vol)
        {
            string result = string.Empty;

            result += "DeviceType:: " + vol.DeviceType + "\n";
            result += "Flags:: " + vol.Flags + "\n";
            result += "Mask:: " + vol.Mask + "\n";
            result += "Reserved:: " + vol.Reserved + "\n";
            result += "Size:: " + vol.Size + "\n";

            eventLog1.WriteEntry(result, EventLogEntryType.SuccessAudit);
        }