|
-
Jan 11th, 2009, 09:40 AM
#41
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Oh ok, so you've got it to output to a textbox instead of a messagebox now then?
-
Jan 11th, 2009, 10:02 AM
#42
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
-
Jan 18th, 2009, 11:24 AM
#43
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
I just found out why Biggy-D's way didn't work, have a look at this post:
http://www.vbforums.com/showpost.php...94&postcount=9
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Apr 15th, 2009, 09:41 AM
#44
Hyperactive Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
I this thread is kind of dead but I found an easier way to detect the insertion of any hardware.
Code:
Private Const WM_DEVICECHANGE As Integer = &H219
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case m.Msg
Case WM_DEVICECHANGE
'Device has been inserted.
Case Else
MyBase.WndProc(m)
End Select
End Sub
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Apr 15th, 2009, 02:06 PM
#45
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
That detects hardware but it doesn't provide drive info, drive letter or any other information...
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Apr 16th, 2009, 10:19 AM
#46
Hyperactive Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Correct, its more of a hardware change like you said.
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Apr 16th, 2009, 10:40 AM
#47
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
So here's a possible solution: Grab a list of the current drives then use that code to trigger an event in which when the event fires, you check the current drives against your list, if there's a change then you'll know it and you'll be able to get the letter of the added drive (or know to remove the letter of a drive from that list)
It's a manual thing, but you could put in into a class that'll raise an event passing the new drive letter or something.
-
Apr 17th, 2009, 02:40 AM
#48
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Yeah, it would work that way as well, but I prefer the "If it works, don't fix it" method
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Feb 11th, 2010, 06:13 PM
#49
Addicted Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
I know this is way late in the game to add, but i tried to build this in a class afte reading and trying and having the textbox issue. I've done everything een try to trick it through adding events, and indeed, as soon as it goes to write to any other medium other than a msgbox or string, it appears as if the code just stops, until the usb device is inserted or removed(whichever state it was in to start with, oppisite that) again.
I was wanting to make this into a class with on connection and on disconnection and on error events, but indeed, it's usless past turning the drive letter into a string or msgbox, and i still see no answer? - just saw this second page -edited- lol
Should be noted, once i got it into a string, it stops there until plugged/unplugged
the string is usless till it starts going again and then it skips any event code that told that string to write to a textbox or anything else
I guess i can try outputting the events back on form called string, but to do it from class is over my head in dotnet right now
Last edited by spyk3; Feb 11th, 2010 at 06:21 PM.
-
Feb 12th, 2010, 02:57 PM
#50
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Have you tried using the Windows API way of doing it rather than WMI or whatever it was that was suggested in this thread? Example code: http://cjwdev.wordpress.com/2009/11/...-drive-letter/
-
Feb 12th, 2010, 03:02 PM
#51
Addicted Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
That's really not that good, it is extremely lacking in finding all disconnects and onConnection Error logging ability. that and that was designed to help that one guy grab a specific file and transfer it, which is similar to what i'm doing but i already know and have classed my file grabber, and tranfer codes as well as a great way to grab all drives, and with a timer could prolly use my drive grabber to grab usb connects and disconnects, but i'd rather do it with out timer since there are system events already in place to catch that inda stuff, might as well use them. :-P
-
Feb 12th, 2010, 03:35 PM
#52
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
but i'd rather do it with out timer since there are system events already in place to catch that inda stuff, might as well use them. :-P
You class a WMI event as more of a 'system event' than a Windows API?
it is extremely lacking in finding all disconnects and onConnection Error logging ability
Every time you connect a drive it will run whatever code you put in the windows message processing part of the example code I wrote, what more do you want from a piece of code that is supposed to run each time a drive is connected? :S
It works 100% of the time that I plug a USB drive into my computer so I'm sure it would work for you if you actually gave it a go. Also just so you know, this is the same method that Windows Explorer uses to detect when a drive has been added/removed - that is the primary purpose of this windows message that is sent to all windows, to notify Explorer (and any other programs that might want to know) that a drive has been connected or removed so that it can take appropriate action. I dont know about you but I have never had a case where Explorer doesnt detect a connect/disconnect of a drive so I would say that shows that it is not "extremely lacking".
Also you do realise that code is an example ... you can make it do whatever you want, it doesnt have to just look for one file - you can put your 'file grabber and transfer codes' in there just as easily. You can also make it detect disconnects as well but because its just an example I only made it detect connections because there are not many cases where you would care if a drive has been disconnected.
Last edited by chris128; Feb 12th, 2010 at 03:44 PM.
-
Feb 12th, 2010, 04:17 PM
#53
Addicted Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
lol, I suppose, more or less, I'm just more associated with the other and it seems easier to deal with, the only problem I'm having now is passing a variable from one thread to another, perhaps by id? Plus I know enough about the previous code to have control with out having to read more at the msdn, (laziness i know, but i'm looking at 400 thousand different lines of code and machinery and screws and customers and everything else everyday) , so forgive me if i get a little comfortable in stuff i can deal with and rewrite without reading, though i'm home and reading over it now, so i may give your code a try in another class idea i'm writing now, afterall, all of this at this point is both about finding a solution and experimintation to find the eventualy best way to to give me more control.
anyway, thanx for the explination and like i said i'm looking into it too, tho i wanna keep both options open, so still looking for help on crossing variables among threads
-
Feb 12th, 2010, 04:47 PM
#54
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Yeah I understand you wanting to stick to something you can understand so fair enough, at least you know an alternative option now if the WMI way doesnt work out for you.
As for accessing variables across different threads, you can pass an object in to the method that a thread starts on or you can declare your variable at class level and then any method in that class (no matter what thread it is on) can access it - be careful doing it this way though as you can end up with two threads trying to use the same object at once and that can cause problems (you can use SyncLock blocks to avoid this though)
-
Aug 17th, 2011, 09:35 PM
#55
New Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
hello guys ! i'm now here and moderately new to it. still very new to quite a chunk of codes. and i wanted to say thanks for the code here . however i was not able to obtain a pop up/message box when i connected a mouse, camera or sd card. maybe i did not do something? i just literally took out the whole chunk that obi1 and .netninja to try in my form.
i also noticed obi1 had used a form3 and form1. which i am quite confused. :/
-
Aug 18th, 2011, 02:37 AM
#56
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
 Originally Posted by Raskyl
hello guys ! i'm now here and moderately new to it. still very new to quite a chunk of codes. and i wanted to say thanks for the code here . however i was not able to obtain a pop up/message box when i connected a mouse, camera or sd card. maybe i did not do something? i just literally took out the whole chunk that obi1 and .netninja to try in my form.
i also noticed obi1 had used a form3 and form1. which i am quite confused. :/
Can you please post all of your code?
Also, I couldn't find the post where I used both Form1 and Form3, could you provide a link to it?
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Aug 18th, 2011, 03:03 AM
#57
New Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
 Originally Posted by obi1kenobi
Ok after adding references to System.Management and System.Management.Instrumentation, I made some changes to the code, in order to fix several errors (concatenation operator cannot be used on Object etc.)
Code:
Imports System.Management
Public Class Form3
Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher
Public USBDriveName As String
Public USBDriveLetter As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
StartDetection()
End Sub
Private Sub Form3_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
m_MediaConnectWatcher.Stop()
m_MediaConnectWatcher.Dispose()
End Sub
Public Sub StartDetection()
Dim query2 As New WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " & "WHERE TargetInstance ISA 'Win32_DiskDrive'")
m_MediaConnectWatcher = New ManagementEventWatcher
m_MediaConnectWatcher.Query = query2
m_MediaConnectWatcher.Start()
End Sub
Private Sub Arrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles m_MediaConnectWatcher.EventArrived
Dim mbo As ManagementBaseObject
Dim obj As ManagementBaseObject
mbo = CType(e.NewEvent, ManagementBaseObject)
obj = CType(mbo("TargetInstance"), ManagementBaseObject)
Select Case mbo.ClassPath.ClassName
Case "__InstanceCreationEvent"
If obj.Item("InterfaceType").ToString = "USB" Then
USBDriveName = obj.Item("Caption").ToString
USBDriveLetter = GetDriveLetterFromDisk(obj.Item("Name").ToString)
MessageBox.Show(USBDriveName & " (Drive letter " & USBDriveLetter & ") has been plugged in")
End If
Case "__InstanceDeletionEvent"
If obj.Item("InterfaceType").ToString = "USB" Then
MessageBox.Show(USBDriveName & " was disconnected. " & USBDriveLetter & " is now inaccessible.") 'GetDriveLetterFromDisk(obj.Item("Name").ToString))
If obj.Item("Caption").ToString = USBDriveName Then
USBDriveLetter = ""
USBDriveName = ""
End If
End If
Case Else
MessageBox.Show("nope: " & obj.Item("Caption").ToString)
End Select
End Sub
Private Function GetDriveLetterFromDisk(ByVal Name As String) As String
Dim oq_part, oq_disk As ObjectQuery
Dim mos_part, mos_disk As ManagementObjectSearcher
Dim obj_part, obj_disk As ManagementObject
Dim ans As String = ""
Name = Replace(Name, "\", "\\")
oq_part = New ObjectQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & Name & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
mos_part = New ManagementObjectSearcher(oq_part)
For Each obj_part In mos_part.Get()
oq_disk = New ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & obj_part.Item("DeviceID").ToString & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
mos_disk = New ManagementObjectSearcher(oq_disk)
For Each obj_disk In mos_disk.Get()
ans &= obj_disk.Item("Name").ToString & ","
Next
Next
Return ans.Trim(","c)
End Function
Please note that the ManagementEventWatcher must be disposed of when the form is closed, otherwise an exception is thrown.
its this post. i was wondering why there are 2 forms.
-
Aug 18th, 2011, 08:25 PM
#58
New Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
i'm sorry for double posting but i forgot to say that it ran without errors and i had a blank form because i didnt add any other features. maybe i missed something out. also, is it able to detect any usb devices? examples like mouse, camera, storage devices(sd card / hard drives) and literally anything that has a usb ?
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
|