locked
Drivers RRS feed

  • Question

  • Hi,

     

    is it possible to detect pugged by USB devices on windows mobile?? have i to write application to detect it or change configuration??

    Monday, October 11, 2010 6:50 AM

All replies

  • You can use the standard MSI interfaces and events for that.
     
    Below you find a little helper object that we use. Maybe it's of use to you. You will probably have to tweak it a bit (especially of you don't use CodeSite)
     
    Hope it helps.
     
    Bye,
    Bart
     
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Management;
    using System.Diagnostics;
    using Raize.CodeSiteLogging;
    using System.Windows.Threading;
     
    namespace Sevensteps.Surface.Controls
    {
     public class USBDetective
     {
      public delegate void USBEventHandler(object sender, USBEventArgs e);
     
      private ManagementEventWatcher _watcher = null;
     
      public USBDetective()
      {
       // TODO: case 7818
       InitDetection();
      }
     
      public void Stop()
      {
       if (_watcher != null)
       {
        _watcher.Stop();
        _watcher.Dispose();
        _watcher = null;
       }
      }
     
      private void InitDetection()
      {
       WqlEventQuery query = new WqlEventQuery();
       ManagementScope scope = new ManagementScope("root\\CIMV2");
       scope.Options.EnablePrivileges = true;
     
       try
       {
        query = new WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk'");
        _watcher = new ManagementEventWatcher(scope, query);
     
        _watcher.EventArrived += USBDetected;
        _watcher.Start();
       }
     
       catch (Exception e)
       {
        Console.WriteLine(e.Message);
        if (_watcher != null)
        {
         _watcher.Stop();
        }
       }
      }
     
      #region USBInserted
     
      /// <summary>
      /// A description of the event.
      /// </summary>
      public event USBEventHandler Inserted;
     
      private void RaiseUSBInserted(string deviceID)
      {
       CodeSite.Send("RaiseUSBInserted");
       if (Inserted != null)
       {
        Inserted(this, new USBEventArgs(deviceID));
       }
      }
      #endregion
     

      #region USBRemoved
     
      /// <summary>
      /// A description of the event.
      /// </summary>
      public event USBEventHandler Removed;
      private void RaiseUSBRemoved(string deviceID)
      {
       if (Removed != null)
       {
        Removed(this, new USBEventArgs(deviceID));
       }
      }
     
      #endregion
     
      public void USBDetected(object sender, EventArrivedEventArgs e)
      {
       try
       {
        ManagementBaseObject mbo = (ManagementBaseObject)e.NewEvent;
        ManagementBaseObject obj = (ManagementBaseObject)mbo["TargetInstance"];
     
        switch (mbo.ClassPath.ClassName)
        {
         case "__InstanceCreationEvent":
          foreach (PropertyData prop in obj.Properties)
           Debug.WriteLine(prop.Name + "="+ prop.Value);
     
          RaiseUSBInserted(obj["DeviceID"]..ToString());
          break;
         case "__InstanceDeletionEvent":
          RaiseUSBRemoved(obj["DeviceID"].ToString());
          break;
        }
       }
       catch (Exception ex)
       {
        CodeSite.SendException(ex);
        throw ex;
       }
      }
     
      string GetDriveLetterFromDisk(string Name)
      {
       ObjectQuery oq_part;
       ObjectQuery oq_disk;
       ManagementObjectSearcher mos_part;
       ManagementObjectSearcher mos_disk;
       string ans = "";
     
       Name = Name.Replace(@"\", @"\\");
     
       oq_part = new ObjectQuery(String.Format("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=\"{0}\"} WHERE AssocClass = Win32_DiskDriveToDiskPartition", Name));
       mos_part = new ManagementObjectSearcher(oq_part);
       foreach (ManagementObject obj_part in mos_part.Get())
       {
        oq_disk = new ObjectQuery(String.Format("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=\"{0}\"} WHERE AssocClass = Win32_LogicalDiskToPartition", obj_part["DeviceID"].ToString()));
        mos_disk = new ManagementObjectSearcher(oq_disk);
        foreach (ManagementObject obj_disk in mos_disk.Get())
         ans += obj_disk["Name"].ToString() + ",";
       }
     
       return ans;
      }
     
      public class USBEventArgs : EventArgs
      {
       public USBEventArgs(string deviceId)
       {
        DeviceID = deviceId;
       }
       public string DeviceID { get; set; }
      }
     }
     
    }

    --
    Bart Roozendaal, Sevensteps
    "Waterinius" schreef in bericht news:caac6f61-2cde-453f-8ba6-f155eb235141...

    Hi,

     

    is it possible to detect pugged by USB devices on windows mobile?? have i to write application to detect it or change configuration??


    Bart Roozendaal, Sevensteps
    Monday, October 11, 2010 4:29 PM