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

Home Posts Topics Members FAQ

USB Stick Plug in and Plug out (USB Harddrive)

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)
{
case Win32.WM_DEVICE CHANGE: OnDeviceChange( ref m); break;
}

base.WndProc (ref m);
}

void OnDeviceChange( ref Message msg)
{
int wParam = (int)msg.WParam ;

if(wParam == Win32.DBT_DEVIC EARRIVAL)
{
statusBar.Text = "Hardware attached";
frmLockScreen.C lose();
}
else if(wParam == Win32.DBT_DEVIC EREMOVECOMPLETE )
{
statusBar.Text = "Hardware entfernt";

frmLockScreen.S how();
}
}
void RegisterHidNoti fication()
{
Win32.DEV_BROAD CAST_DEVICEINTE RFACE dbi = new
Win32.DEV_BROAD CAST_DEVICEINTE RFACE();
int size = Marshal.SizeOf( dbi);
dbi.dbcc_size = size;
dbi.dbcc_device type = Win32.DBT_DEVTY P_DEVICEINTERFA CE;
dbi.dbcc_reserv ed = 0;
dbi.dbcc_classg uid = Win32.GUID_DEVI NTERFACE_HID;
dbi.dbcc_name = 0;
IntPtr buffer = Marshal.AllocHG lobal(size);
Marshal.Structu reToPtr(dbi, buffer, true);
IntPtr r = Win32.RegisterD eviceNotificati on(Handle, buffer,
Win32.DEVICE_NO TIFY_WINDOW_HAN DLE);
if(r == IntPtr.Zero)
Console.WriteLi ne(Win32.GetLas tError().ToStri ng());
}

class Win32
{
public const int
WM_DEVICECHANGE = 0x0219;
public const int
DBT_DEVICEARRIV AL = 0x8000,
DBT_DEVICEREMOV ECOMPLETE = 0x8004;
public const int
DEVICE_NOTIFY_W INDOW_HANDLE = 0,
DEVICE_NOTIFY_S ERVICE_HANDLE = 1;
public const int
DBT_DEVTYP_DEVI CEINTERFACE = 5;
public static Guid
GUID_DEVINTERFA CE_HID = new
Guid("4D1E55B2-F16F-11CF-88CB-001111000030");

[StructLayout(La youtKind.Sequen tial)]
public class DEV_BROADCAST_D EVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype ;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}

[DllImport("user 32.dll", SetLastError=tr ue)]
public static extern IntPtr RegisterDeviceN otification(
IntPtr hRecipient,
IntPtr NotificationFil ter,
Int32 Flags);

[DllImport("kern el32.dll")]
public static extern int GetLastError();
}
Thanks for any hints,
daniel
Nov 15 '05 #1
1 2650
Use the Management classes to register Diskdrive event notifications, here's
a sample.
// This code demonstrates how to monitor the 'Win32_DiskDriv e' 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 = "__InstanceOper ationEvent";
q.WithinInterva l = new TimeSpan(0,0,3) ;
q.Condition = @"TargetInstanc e ISA 'Win32_DiskDriv e' ";

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(); // block main thread for test purposes
}
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) {
ManagementBaseO bject mbo = null;
if(( mbo = pd.Value as ManagementBaseO bject) != null) {
foreach(Propert yData prop in mbo.Properties)
Console.WriteLi ne("{0} - {1}", prop.Name, prop.Value);
}
}
}

}

Willy.
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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)
2
1412
by: ellisydjogra | last post by:
i have down loaded net framework 1.1 but i still can not get my usb2.0 menory stick to work on my computer? any idea's pls?
0
1420
by: Stijn | last post by:
I wrote an application where the user needs an USB memory stick. When the user closes the program, some data is written to the stick. Can I write some code so my application is closing the usb stick whithout an action of the user? When I do not implement this, the user have to click on an icon in the taskbar for removing the usb stick safely. But i want to keep it simpel for the user ;)
10
3719
by: Joe Macarony | last post by:
Hi to theForcesOfCpp!I'm just wondering if there is any simple way to read the serial number of an memory stick connected to an USB port? Something like reading the vendor number of a hard disk... OS: Windows XPEnvironment: Borland C++BuilderX Personal or MS Visual Studio 2005 ExpressAny help / sample would be great...Thanx in advance,Joe
5
2102
by: Baron Samedi | last post by:
I prefer Zend, but I really want something that I can carry with me on a 4gB USB stick, along with my code. I tend to work on multiple computers, so this seems best. So, I am looking for a good PHP IDE which runs under MS Windows, but doesn't need any registry entries, or DLLs in the system directory, etc. I am willing to pay, if need be, but certainly won't turn up my nose at something free. The feature set is the thing, particularly...
12
1979
by: hufaunder | last post by:
I often find myself in the situation where at a customers site I have to do some quick debugging or program changes. Obvioiusly, I do not want to install VS2005 on their system. Is there a ways to install VS2005 on a big and fast USB stick, plug it into a computer without VS2005 and start using it there? If not are there any alternatives. Thanks
1
1389
by: flamingskullz | last post by:
Hi all, firstly i apologise if this has appearred before, i did search for it, honest :) i have a sandisk memory stick, which works great at work, but as soon as i plug it into my home machine, it registers as device unknown in all my USB ports. I have seen this happen before with various other Memory sticks but i have got around it by using a different port. Anybody able to shine some light on this? My OS is Windows XP home, the work one...
1
7484
by: =?Utf-8?B?Y2FsZGVyYXJh?= | last post by:
Dear all, For one on my customer application I would like to implement the following facilities: As soon as USB stick is inserted I need to run a small application which will take care of copiing predifined folders and store then at a particular place on the USB key. For that I need to:
2
3115
by: ashishchauhan | last post by:
I am using a linux machine with an USB port and I have to communicate with a temperature controller which has an UART port and for this I am using a USB to UART Serial Adapter. port name on my machine is /dev/ttyUSB0 $port = "/dev/ttyUSB0"; open(SERIALPORT,"+>$port") || die("ERROR SENDING DATA\n"); SERIALPORT->autoflush(); ## in between I am sending data print $FILEHANDLE $data;
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
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...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.