-
Aug 7th, 2008, 06:48 PM
#1
[RESOLVED] Detecting Drive Letter of USB Drive
OK so this is kind of following on from someone else's thread that I am trying to help out with (http://www.vbforums.com/showthread.php?t=534932).
If you cant be bothered to read the other thread, here's a brief summary of what I (and the OP in that thread) would like to achieve:
When a USB drive is plugged in, the application automatically searches this drive for a specific file.
So to cut a long story short, this is what I have so far:
I am using VB.NET by the way, not VB6
vb.net Code:
Public Class Form1
Private Const WM_DEVICECHANGE As Integer = &H219
Private Const DBT_DEVICEARRIVAL As Integer = 32768
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
If m.WParam = DBT_DEVICEARRIVAL Then
MessageBox.Show("Removable Drive Detected")
End If
End If
MyBase.WndProc(m)
End Sub
End Class
This works in that when a USB drive is plugged in, it pops up with the messagebox. It took me a lot of trial and error and a lot of reading on the MSDN site to get even this much working (this is only the 3rd or 4th API I have ever tried to use) as there didnt seem to be much useful stuff on google... but anyway yeah, the problem is that its all well and good detecting that a drive was connected but now we need to be able to scan it. So we need to get the drive letter that was assigned to the newly connected drive - According to the MSDN site, the following is passed in through the lparam property of the DEVICECHANGE message:
A pointer to a structure identifying the device inserted. The structure consists of an event-independent header, followed by event-dependent members that describe the device. To use this structure, treat the structure as a DEV_BROADCAST_HDR structure, then check its dbch_devicetype member to determine the device type.
Looking at the DEV_BROADCAST_HDR structure and its dbch_devicetype member in particular I can see that I can check to see if the device is a Volume. Then if it is a volume I can check the DEV_BROADCAST_VOLUME structure member named dbcv_unitmask and this relates to the drive letter that is assigned. Hopefully I didnt explain that too badly... The problem is, like I said, I'm fairly new to APIs and so I havent got a clue how to do what I just explained!
I'm guessing I need to just declare each of these structures and then it will be fairly easy to use them in my managed code but none of the mentioned structures appear in my API Viewer program so I have no idea what type of object each member needs to be declared as etc
Anyone help me out?
Thanks
Chris
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|