473,796 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

USB RegisterDeviceN otification

12 New Member
I've been doing lots of research on controlling USB devices in C# and ran across a few tutorials that seemed good until I started trying to code them. It seems like everyone seems to know how to use the function RegisterDeviceN otification to register the form to recieve WM_DEVICECHANGE messages.

I've gotten as far as importing the function from user32.dll and setting up the device filtering, but I don't really understand the concept of using RegisterDeviceN otification. Can anyone clarify what and where to get the parameters that its asking for?

Here is my code so far:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Windows;
  9. using System.Runtime.InteropServices;
  10. using DeviceManagementDeclarations;
  11.  
  12.  
  13. namespace USBDevice
  14. {
  15.  
  16.     public partial class Form1 : Form
  17.     {
  18.         public const int WM_DEVICECHANGE = 0x219;
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.             RegisterDeviceNotification(this.Handle, ?NotificationFilter?, ?Flags?);
  24.  
  25.         }
  26.         protected override void WndProc(ref Message m)
  27.         {
  28.             switch (m.Msg)
  29.             {
  30.                 case WM_DEVICECHANGE:
  31.                     {
  32.                         MessageBox.Show(m.ToString());
  33.                         break;
  34.                     }
  35.  
  36.             }
  37.  
  38.             base.WndProc(ref m);
  39.         }
  40.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  41.         static public extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, IntPtr NotificationFilter, int Flags);
  42.  
  43.     }
  44.  
  45. }
  46.  
Feb 15 '08 #1
5 13336
Plater
7,872 Recognized Expert Expert
Hmm, I was pretty sure I got that notification just fine without doing any registering.

I will check though.
EDIT:
Just checked, yup I get that message regardless of focus or anything and I didn't need to register for it.
Feb 15 '08 #2
bfoo75
12 New Member
Huh... well I feel stupid. How come all these tutorials make out that you have to register the device notification?

I think last time I had it compilable I was using a different filter for the message. Thanks for the clear-up tho!
Feb 15 '08 #3
Plater
7,872 Recognized Expert Expert
I should alert you that DeviceNotify isn't as usefull as you would hope. I only found it really usefull for when a USB->SerialPort was added/removed, or some kind of mass storage usb device was added/removed (i.e. thumbdrive, external harddrive, etc)
Feb 15 '08 #4
nuno1707
1 New Member
I've been doing the same (some research on monitoring added/removed of mass storage device in C# ) but sa new in C# I don't know what to do!

Could you clarify it or post you Code?
Mar 4 '09 #5
Plater
7,872 Recognized Expert Expert
Have a look here:
http://msdn.microsoft.com/en-us/libr...80(VS.85).aspx
Mar 4 '09 #6

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

Similar topics

0
8375
by: grutta | last post by:
I am writing a windows service that will recieve notification when a USB Device is insterted into the machine. I have used the RegisterDeviceNotification and the RegisterServiceCtrlHandlerEx with a handler. The handler portion seems to fail indicating that my parameters are invalid. I am getting an error code 126 when i try to register the handler and 28 when i register for event notification. Any ideas as to what the deal is? I am...
1
18362
by: Daniel Diehl | last post by:
Hi, I'm looking for a solution to detect if a USB Sticker (USB Drive) ist plugged in or plugged out. Currently Im able to detect Hardware Plugin and Plugout but don't get any information what type of device it is. Is there a way to get this information ? Currenty my solution work in that way (Also from this group): protected override void WndProc(ref Message m) { switch(m.Msg)
1
2650
by: Daniel Diehl | last post by:
Hi, I'm looking for a solution to detect if a USB Sticker (USB Drive) ist plugged in or plugged out. Currently Im able to detect Hardware Plugin and Plugout but don't get any information what type of device it is. Is there a way to get this information ? Currenty my solution work in that way (Also from this group): protected override void WndProc(ref Message m) { switch(m.Msg)
1
11255
by: Jeffrey B. Holtz | last post by:
I'm trying to get the Name of the USB device pluged in from the RegisterDeviceNotification that I've used P/Invoke to marshal. I have seen a similar posting on the VisualBasic newgroups but I do not know how to translate the ReDim that occurs there into C#. Or wither this will actually give me what I'm looking for. The code I'm posting seems to work although I don't know how to set the length of the name correctly in the...
0
2272
by: Russ A | last post by:
Hi, Im using the RegisterDeviceNotification API to track a PCMCIA Card insert/removal event But I cant find the dbcc_classguid defined for PCMCIA anywhere Can anyone help? Cheers
1
2755
by: Wolfgang | last post by:
Hello, Has someone an example how to use "RegisterDeviceNotification(hRecipient,NotificationFilter,Flags);" I wanted to register a Function which is called when an USB-Device is plugged in or disconnected. Maybe someone has written this part before ? Thanks, Wolfgang
3
13318
by: ladal | last post by:
Hi I just want to know how do I detect an usb drive when it's plug and retrieve his name, in vb.net Thanx
0
3644
by: [Joe] | last post by:
Hi, I'm writing a C# application, this application will display a message when USB device connected/disconnected(removed) , How can I do that ? I made some research and find something called "RegisterDeviceNotification" , But don't find how to use it in my C# since I should send to this API "IntPtr hRecipient", How can I do that ? static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient,IntPtr NotificationFilter, uint Flags);
9
2865
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i am a little stuck here, how do i convert this from C Structure to an structure in C# that can be marshalled: typedef struct _DEV_BROADCAST_DEVICEINTERFACE { DWORD dbcc_size; DWORD dbcc_devicetype; DWORD dbcc_reserved; GUID dbcc_classguid;
0
9685
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
9533
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
10239
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...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
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...
1
7555
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
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...
1
4122
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
3
2928
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.