473,508 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query related to Insertion and removal of Plug and Play Devices

3 New Member
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 2932
pootle
68 New Member
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 Recognized Expert Expert
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
elizabeth1986
3 New Member
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 Recognized Expert Expert
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
elizabeth1986
3 New Member
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 Recognized Expert Specialist
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

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

Similar topics

0
1362
by: Viper99 | last post by:
Need to detect (receive notification) for the insertion/removal of PCMCIA devices on Windows 2000/XP in a C# .Net application. Was able to do this in ATL COM app using CWnd::OnDeviceChange().
1
2139
by: JC | last post by:
Hi, I have to play a streaming audio. So in a thread I play audio as follow thread{ //some code for(;;){ //some code waveOutPrepareHeader(hwo, &(whdr), sizeof(WAVEHDR)); waveOutWrite(hwo,...
1
1430
by: Homa | last post by:
I have a web service that want to print something to a printer connected on another computer in the network. And that printer happens to be a non-plug and play printer (It's a thermal printer). ...
4
2227
by: Brad Markisohn | last post by:
Is there a way to determine, programmatically, when Plug-and-Play devices are connected or removed from the PC? In VB 6 I caught events from the SysInfo control, but I don't believe that this...
5
2861
by: themastertaylor | last post by:
I've got a system to manage various quotes for building materials for a number of sites. i want a query to produce a report that shows me who HASN'T quoted for which sites. basically so i can...
7
5644
by: BLUE | last post by:
Are there events that are fired when I insert the handheld into or I remove it from the cradle? *OpenNETCF.Desktop.Communication.ActiveSync* class has *Active* (indicates that a connection is...
6
2311
by: barcaroller | last post by:
If I insert/remove an element in a set, will an iterator to this set automatically become invalid? Does the position of the iterator before the insertion/removal matter? How are vectors and...
4
1758
by: stmfc | last post by:
list class is said to provide constant-time insertion and removal of elements at any location in the list. but i cannot insert an element to an arbitrary position in the list using:...
1
2512
by: =?Utf-8?B?Sm9lIE1pbGxlcg==?= | last post by:
I am a developer and a photographer, and I am trying to write a windows service that can recieve a notification that a compact flash card, or an SD card has been insterted into a card reader slot. ...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1558
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.