473,659 Members | 2,540 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_DeviceCha ngeEvent 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 2974
pootle
68 New Member
Hi,

You could try using the Win32 function RegisterDeviceN otification 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_RegisterD eviceNotificati on 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
1384
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
2152
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, &(whdr), sizeof(WAVEHDR));
1
1434
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). I tried to install the printer to the server directly and it works fine (I use PrinterSettings.PrinterName to set the printer). But when I set it up on another computer it doesn't work. I have installed the networked printer's driver to the server...
4
2237
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 control is supported in .Net. Any suggestion? TIA Brad
5
2872
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 print it off and ring round and hurry them up a bit. I have my quote table which has the supplier ID, Site ID and an ID for each quote.(I enter each quote and specify which supplier and site it relates to. I've tried an unmatched query but can't...
7
5650
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 established between the client application and the connection manager) and *Inactive* (indicates a disconnection, or disconnected state, between the desktop computer and the Windows CE-based device) events, but I don't know if I can use them: "the...
6
2318
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 lists affected?
4
1765
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: myList.insert(myList.begin()+2500,1000); i got the following error: no operator "+" matches these operands operand types are: std::list<int, std::allocator<int>>::iterator +int myList.insert(myList.begin()+2500,1000); i think the error...
1
2522
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. The card reader is a multi-format (CF/SD/MMC, etc) reader connected to the machine by USB. I want the service to recieve a notification of the card insertion, so it can grab the files, read the EXIF/IPTC data, and process the files (cataloging,...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4176
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
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 we have to send another system
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.