How to detect a USB Device Being plugged-in or unplugged?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jack

    #1

    How to detect a USB Device Being plugged-in or unplugged?

    Hello,

    I'm trying to make a program what can let me know when a USB device is added
    or removed from a system. If anyone has any ideas or help that would be
    great!

    Thanks in advnace,

    Jack


  • Greg Ewing [MVP]

    #2
    Re: How to detect a USB Device Being plugged-in or unplugged?

    Jack, you are going to have to use P/Invoke and RegisterDeviceN otification.
    Here's a link to some code by Wei Mao at MS.

    Choosing the right web hosting company is one of the most important — and most consequential — decisions you’ll make when starting or growing your website.


    --
    Greg Ewing [MVP]
    IT support built exclusively for nonprofits. 25 years of managed IT, help desk, and cybersecurity - so your team can focus on your mission.


    "jack" <jack@mrolinux. com> wrote in message
    news:eMj80xMeDH A.2472@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Hello,
    >
    > I'm trying to make a program what can let me know when a USB device is[/color]
    added[color=blue]
    > or removed from a system. If anyone has any ideas or help that would be
    > great!
    >
    > Thanks in advnace,
    >
    > Jack
    >
    >[/color]


    Comment

    • jack

      #3
      Re: How to detect a USB Device Being plugged-in or unplugged?

      Hello,

      Great, any example in VB.NET?

      Thanks,

      Jack
      "Greg Ewing [MVP]" <gewing@_NO_SPA M_gewing.com> wrote in message
      news:%231cQjkNe DHA.1732@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > Jack, you are going to have to use P/Invoke and[/color]
      RegisterDeviceN otification.[color=blue]
      > Here's a link to some code by Wei Mao at MS.
      >
      > http://www.dotnet247.com/247referenc...32/164968.aspx
      >
      > --
      > Greg Ewing [MVP]
      > http://www.citidc.com
      >
      > "jack" <jack@mrolinux. com> wrote in message
      > news:eMj80xMeDH A.2472@TK2MSFTN GP11.phx.gbl...[color=green]
      > > Hello,
      > >
      > > I'm trying to make a program what can let me know when a USB device is[/color]
      > added[color=green]
      > > or removed from a system. If anyone has any ideas or help that would be
      > > great!
      > >
      > > Thanks in advnace,
      > >
      > > Jack
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • William Ryan

        #4
        Re: How to detect a USB Device Being plugged-in or unplugged?

        YOU ARE THE MAN!
        "Greg Ewing [MVP]" <gewing@_NO_SPA M_gewing.com> wrote in message
        news:#1cQjkNeDH A.1732@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Jack, you are going to have to use P/Invoke and[/color]
        RegisterDeviceN otification.[color=blue]
        > Here's a link to some code by Wei Mao at MS.
        >
        > http://www.dotnet247.com/247referenc...32/164968.aspx
        >
        > --
        > Greg Ewing [MVP]
        > http://www.citidc.com
        >
        > "jack" <jack@mrolinux. com> wrote in message
        > news:eMj80xMeDH A.2472@TK2MSFTN GP11.phx.gbl...[color=green]
        > > Hello,
        > >
        > > I'm trying to make a program what can let me know when a USB device is[/color]
        > added[color=green]
        > > or removed from a system. If anyone has any ideas or help that would be
        > > great!
        > >
        > > Thanks in advnace,
        > >
        > > Jack
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Willy Denoyette [MVP]

          #5
          Re: How to detect a USB Device Being plugged-in or unplugged?


          "jack" <jack@mrolinux. com> wrote in message news:eMj80xMeDH A.2472@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Hello,
          >
          > I'm trying to make a program what can let me know when a USB device is added
          > or removed from a system. If anyone has any ideas or help that would be
          > great!
          >
          > Thanks in advnace,
          >
          > Jack
          >
          >[/color]

          Here is a sample using the System.Manageme nt classes (and WMI).

          // This code demonstrates how to monitor the UsbControllerDe vice for
          // the arrival of creation/operation events
          using System;
          using System.Componen tModel;
          using System.Runtime. InteropServices ;
          using System.Manageme nt;
          class WMIEvent {
          public static void Main() {
          WMIEvent we = new WMIEvent();
          ManagementEvent Watcher w= null;
          WqlEventQuery q;
          ManagementOpera tionObserver observer = new ManagementOpera tionObserver();
          // Bind to local machine
          ManagementScope scope = new ManagementScope ("root\\CIMV2") ;
          scope.Options.E nablePrivileges = true; //sets required privilege
          try {
          q = new WqlEventQuery() ;
          q.EventClassNam e = "__InstanceCrea tionEvent";
          q.WithinInterva l = new TimeSpan(0,0,10 );

          q.Condition = @"TargetInstanc e ISA 'Win32_USBContr ollerDevice' ";
          Console.WriteLi ne(q.QueryStrin g);
          w = new ManagementEvent Watcher(scope, q);

          w.EventArrived += new EventArrivedEve ntHandler(we.Us bEventArrived);
          w.Start();
          Console.ReadLin e();
          }
          catch(Exception e) {
          Console.WriteLi ne(e.Message);
          }
          finally {
          w.Stop();
          }
          }
          public void UsbEventArrived (object sender, EventArrivedEve ntArgs e) {
          //Get the Event object and display it
          foreach(Propert yData pd in e.NewEvent.Prop erties) {
          Console.WriteLi ne("\n========= =============== ==============" );
          Console.WriteLi ne("{0},{1},{2} , {3}",pd.Name, pd.Type, pd.Value, pd.Origin);

          ManagementBaseO bject mbo = null;
          if(( mbo = pd.Value as ManagementBaseO bject) != null) {
          Console.WriteLi ne("--------------Properties------------------");
          foreach(Propert yData prop in mbo.Properties)
          Console.WriteLi ne("{0} - {1}", prop.Name, prop.Value);
          }
          }
          }

          }

          Willy.



          Comment

          Working...