473,785 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WM_DEVICECHANGE

I need to load the contents of a file from a USB flash drive when it
inserted. Can someone please provide a sample of how this can be achieved?
Feb 9 '07 #1
3 9014
I wrote this code a few weeks ago

Imports System.Runtime. InteropServices

#Region "Constants"

Private Const WM_DEVICECHANGE As Integer = &H219
Private Const DBT_DEVICEARRIV AL As Integer = &H8000
Private Const DBT_DEVICEREMOV ECOMPLETE As Integer = &H8004
Private Const DBT_DEVTYP_VOLU ME As Integer = &H2

#End Region

#Region "Structures "

Private Structure DEV_BROADCAST_H DR
Public dbch_size As Int32
Public dbch_devicetype As Int32
Public dbch_reserved As Int32
End Structure

Private Structure DEV_BROADCAST_V OLUME
Public dbcv_size As Int32
Public dbcv_devicetype As Int32
Public dbcv_reserved As Int32
Public dbcv_unitmask As Int32
Public dbcv_flags As Int32
End Structure

#End Region

#Region "WndProc (SUB)"

Protected Overrides Sub WndProc(ByRef m As Message)
' Windows MESSAGE
If m.Msg = WM_DEVICECHANGE Then
' Check to see if device was plugged in
If m.WParam.ToInt3 2() = DBT_DEVICEARRIV AL Then
Dim hdr As DEV_BROADCAST_H DR =
CType(Marshal.P trToStructure(m .LParam, GetType(DEV_BRO ADCAST_HDR)),
DEV_BROADCAST_H DR)
' See if the device is a USB Flash Drive etc.
If hdr.dbch_device type = DBT_DEVTYP_VOLU ME Then
' Declare volume as structure
Dim vol As DEV_BROADCAST_V OLUME =
CType(Marshal.P trToStructure(m .LParam, GetType(DEV_BRO ADCAST_VOLUME)) ,
DEV_BROADCAST_V OLUME)
' Get volume number
Dim mask As Int32 = vol.dbcv_unitma sk
' Get the drive letter
Dim chDriveLetter As Char() = GetDriveLetter( mask)
' Display the drive letter
lblDrive.Text = String.Format(" USB Flash Drive Plugged
Into {0}:", chDriveLetter(0 ).ToString().To Upper())
End If
ElseIf m.WParam.ToInt3 2() = DBT_DEVICEREMOV ECOMPLETE Then
' Flash Drive Unplugged
lblDrive.Text = "USB Flash Drive Removed"
End If
End If
MyBase.WndProc( m)
End Sub

#End Region

#Region "Get Drive Letter (FUNCTION)"

Private Function GetDriveLetter( ByVal iChar As Int32) As Char()
Dim cnt As Integer = 0
Dim intTemp As Int32 = iChar
Dim i As Integer

For i = 0 To 31
If (intTemp And 1) = 1 Then
cnt += 1
End If
intTemp = (intTemp >1)
If intTemp = 0 Then
Exit For
End If
Next

Dim chResult(0) As Char
Dim ii As Integer

For ii = 0 To 31
If (iChar And 1) = 1 Then
chResult(0) = Chr(97 + ii)
End If
iChar = (iChar >1)
If iChar = 0 Then
Exit For
End If
Next
Return chResult
End Function

#End Region
Feb 9 '07 #2
"JK" <JK@discussions .microsoft.coms chrieb:
>I need to load the contents of a file from a USB flash drive when it
inserted. Can someone please provide a sample of how this can be achieved?
<URL:http://www.vb-tips.com/dbpages.aspx?IA =NG>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Feb 9 '07 #3
Thank you both!

"JK" wrote:
I need to load the contents of a file from a USB flash drive when it
inserted. Can someone please provide a sample of how this can be achieved?
Feb 12 '07 #4

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

Similar topics

1
6610
by: Matteo | last post by:
Hello everyone, I'm trying to write a little app that wait for the WM_DEVICECHANGE message from Windows that tht OS raises when a device (such as CD, DVD,...) is inserted or ejected (in BC++ 6) I've tried whith the Application->OnMessage() but it never receives the message, so I've tried with the esample in the MSDN, write below, but I dont know how to register the message handler in the application. I know that it could works because...
2
21469
by: Ed Sutton | last post by:
How can a WndProc be created inside a component? Currently I have a WndProc in my frmMain. It looks for WM_DEVICECHANGE messages for connection and removal events for a USB/Serial device. My frmMain uses a component to communicate with a USB/Serial device. I would prefer that this component handle the WM_DEVICECHANGE messages internally and only fires events to my frmMain when my USB device connects or disconnects. In case anyone...
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
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...
9
37210
by: Paul Steele | last post by:
I am writing a C# app that needs to periodically poll for cdroms and usb storage device insertions. I've looked at the WMI functions but haven't found anything all that useful. The closest is Win32_DiskDrive, but it doesn't seem to return any information on cdrom devices. I suspect there might be a Win32 API call, but I haven't found any info yet using Google. Is there a way to achieve this in C#?
0
2243
by: Ryan | last post by:
I'm writing a utility that needs to be aware of media (CD, DVD, Zip disk, usb stick, etc) insertions and removals. Which seems to be more of a problem than I original imagined. I can't use any auto-run features because this is actually running on Windows XP Embedded... so that's the first thing to be aware of. Using WM_DEVICECHANGE works about half the time...that is, it works when a DVD or CD is inserted, but it does not work for Zip...
0
1803
by: Erwin Hill | last post by:
Hi guys, I have a weird problem. I wrote an application that has 2 UI threads both windows are top-level. When I insert my USB disk on key into computer I receive several WM_DEVICECHANGE which are sent trough PostMessage and WM_DEVICECHANGE with wParam set to DBT_DEVICEARRIVAL and this one is sent to me through SendMessage(by windows) I checked this through Spy++(I don't know why last one is sent through SendMessage). The
4
6476
by: SharpCoderMP | last post by:
hi, what can i do to have my app be notified when there is a new drive in the system? I need to perform some actions in my application when new drive (like SD card or some usb storage device) shows up in the system.
5
13334
by: bfoo75 | last post by:
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 RegisterDeviceNotification 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...
0
9647
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
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10161
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
10098
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
9958
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
8986
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...
1
7506
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
6743
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.