470,594 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

Query related to Insertion and removal of Plug and Play Devices

Hello,

Is it possible to detect the insertion and removal of Plug and play devices (for example : keyboard , mouse etc) while the computer is still ON. We wanted to do the above in C#. We tried a couple of WMI classes like win32_Keyboard class, but after manually removing the Keyboard, there was no change in the Property "status" of win32_Keyboard. it showed that the status was OK.
We also tried a piece of code which uses win32_DeviceChangeEvent but that also doesnot seem to work for plug and play devices.
Plaese can u suggest some Solution to this problem.,
Aug 23 '08 #1
6 2735
pootle
68
Hi,

You could try using the Win32 function RegisterDeviceNotification and test for WM_DEVICECHANGE in WndProc. There are probably some examples of using this in C# - try googling...


HTH.
Aug 24 '08 #2
Plater
7,872 Expert 4TB
I tested the following WMI code with:
keyboards
mice
usb->Serial converters
card readers

All worked correctly, firing events.
Expand|Select|Wrap|Line Numbers
  1. //Taken from WMICodeCreator
  2. using System;
  3. using System.Management;
  4. using System.Windows.Forms;
  5.  
  6. namespace WMISample
  7. {
  8.     public class WMIReceiveEvent
  9.     {
  10.         public WMIReceiveEvent()
  11.         {
  12.             try
  13.             {
  14.                 WqlEventQuery query = new WqlEventQuery(
  15.                     "SELECT * FROM Win32_DeviceChangeEvent");
  16.  
  17.                 ManagementEventWatcher watcher = new ManagementEventWatcher(query);
  18.                 Console.WriteLine("Waiting for an event...");
  19.  
  20.                 watcher.EventArrived += 
  21.                     new EventArrivedEventHandler(
  22.                     HandleEvent);
  23.  
  24.                 // Start listening for events
  25.                 watcher.Start();
  26.  
  27.                 // Do something while waiting for events
  28.                 System.Threading.Thread.Sleep(10000);
  29.  
  30.                 // Stop listening for events
  31.                 watcher.Stop();
  32.                 return;
  33.             }
  34.             catch(ManagementException err)
  35.             {
  36.                 MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
  37.             }
  38.         }
  39.  
  40.         private void HandleEvent(object sender,
  41.             EventArrivedEventArgs e)
  42.         {
  43.             Console.WriteLine("Win32_DeviceChangeEvent event occurred.");
  44.         }
  45.  
  46.         public static void Main()
  47.         {
  48.             WMIReceiveEvent receiveEvent = new WMIReceiveEvent();
  49.             return;
  50.         }
  51.  
  52.     }
  53. }
  54.  
Aug 25 '08 #3
hi....
The Above code only works for USB devices like Pendrives, we tried unplugging the Keyboard but no event is fired.. .
thanks.
.
Aug 26 '08 #4
Plater
7,872 Expert 4TB
Oh... huh. You still use a non-usb keyboard. Hmm.
There is probably some Win32_API for that. Although I highly doubt they bothered to keep track of if things were plugged into a ps2, serial or din port.
Although I guess the bios was always able to tell if there was a keyboard or not.
Aug 26 '08 #5
thanks for the solution , but please can u give some Code in C# which uses Win32_RegisterDeviceNotification method to register a device like Keyboard
Aug 27 '08 #6
kenobewan
4,871 Expert 4TB
Please trying writing the code first. The experts on this site are not here to do your work for you. If in doubt, read the posting guidelines. Thanks.

MODERATOR
Aug 27 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by JC | last post: by
4 posts views Thread by Brad Markisohn | last post: by
5 posts views Thread by themastertaylor | last post: by
6 posts views Thread by barcaroller | last post: by
1 post views Thread by =?Utf-8?B?Sm9lIE1pbGxlcg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.