Sponsored links

Go Back   MP3Car.com > Mp3Car Technical > Software & Software Development > Coders Corner


Reply
 
Share Thread Tools Display Modes
Old 09-21-2006, 05:49 PM   #1
Newbie
 
Join Date: Jul 2006
Location: Central Illinois
Posts: 5
ProjektMayhem is an unknown quantity at this point
vb.net :: detecting a usb or cd media

the end result of what i'm trying to develop is that when a thumb drive or cd-rom is inserted into the system, it fires an event that will allow me to search through the contents of this new media and check for particular filetypes...

anyone have any thoughts? i've been searching for a few days and haven't really found anything pertaining to this particular line of thought...

thanks in advance.
ProjektMayhem is offline   Reply With Quote
Advertisement
 
Advertisement
Sponsored links

Old 10-29-2006, 05:42 PM   #2
Newbie
 
mrspangle's Avatar
 
Join Date: Sep 2003
Posts: 40
mrspangle is on a distinguished road
You want to listen on the window for the HWND value. Look up these HWND values on msdn, i've managed to listen out for new media instered value.

Can't remember the code sorry!
mrspangle is offline   Reply With Quote
Old 10-29-2006, 07:19 PM   #3
Maximum Bitrate
 
Jarrod's Avatar
 
Join Date: Jan 2002
Location: Melb, Australia.
Posts: 475
Jarrod
Code:
' USB DEVICE DETECTION BELOW: '======================================================================================================================== Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher Public Sub StartDetection() ' __InstanceOperationEvent will trap both Creation and Deletion of class instances Dim query2 As String = "SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE TargetInstance ISA ""Win32_DiskDrive""" m_MediaConnectWatcher = New ManagementEventWatcher(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, obj As ManagementBaseObject ' the first thing we have to do is figure out if this is a creation or deletion event mbo = CType(e.NewEvent, ManagementBaseObject) ' next we need a copy of the instance that was either created or deleted obj = CType(mbo("TargetInstance"), ManagementBaseObject) Select Case mbo.ClassPath.ClassName Case "__InstanceCreationEvent" If obj("InterfaceType") = "USB" Then MsgBox(obj("Caption") & " (Drive letter " & GetDriveLetterFromDisk(obj("Name")) & ") has been plugged in") Else MsgBox(obj("InterfaceType")) End If Case "__InstanceDeletionEvent" If obj("InterfaceType") = "USB" Then MsgBox(obj("Caption") & " has been unplugged") If obj("Caption") = USBDriveName Then USBDriveLetter = "" USBDriveName = "" End If Else MsgBox(obj("InterfaceType")) End If Case Else MsgBox("nope: " & obj("Caption")) 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 ' WMI queries use the "\" as an escape charcter Name = Replace(Name, "\", "\\") ' First we map the Win32_DiskDrive instance with the association called ' Win32_DiskDriveToDiskPartition. Then we map the Win23_DiskPartion ' instance with the assocation called Win32_LogicalDiskToPartition 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("DeviceID") & """} WHERE AssocClass = Win32_LogicalDiskToPartition") mos_disk = New ManagementObjectSearcher(oq_disk) For Each obj_disk In mos_disk.Get() ans &= obj_disk("Name") & "," Next Next Return ans.Trim(","c) End Function

That should get you started for detecting USB devices
Not sure exactly what import statements you will need, but probably 'Imports System.Management' and maybe others.

As for CDROM:
Code:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg = WM_DEVICECHANGE Then ' Detect CD If (m.WParam.ToInt32() = DBT_DEVICEREMOVECOMPLETE) Then MsgBox("CD Removal Complete!") ElseIf (m.WParam.ToInt32() = DBT_DEVICEARRIVAL) Then MsgBox("CD Now Avaiable!") End If Else MyBase.WndProc(m) End If End Sub

__________________
Jarrod - Holden VX S!

Last edited by Jarrod; 10-29-2006 at 07:22 PM.
Jarrod is offline   Reply With Quote
Old 11-03-2006, 10:57 AM   #4
Newbie
 
Join Date: Jul 2006
Location: Central Illinois
Posts: 5
ProjektMayhem is an unknown quantity at this point
jarrod, fantastic! thanks so much. i had actually figured out the usb one, but the cd-rom snippet is the perfect size to fit snugly where i need it to.
ProjektMayhem is offline   Reply With Quote
Old 11-04-2006, 11:51 PM   #5
Maximum Bitrate
 
Jarrod's Avatar
 
Join Date: Jan 2002
Location: Melb, Australia.
Posts: 475
Jarrod
No problems, what features will this software have btw?
__________________
Jarrod - Holden VX S!
Jarrod is offline   Reply With Quote
Old 03-16-2007, 05:52 PM   #6
Newbie
 
Join Date: Jul 2006
Location: Central Illinois
Posts: 5
ProjektMayhem is an unknown quantity at this point
sorry for the SUPER late response (thought mine was the last one in here)... at the moment, i'm writing a new frontend (haha, yeah, i know... ANOTHER frontend). i'm not sure whether or not i'll ever release it, or whether it'll be for vehicles other than bmw's or not. at the moment, i'm just playing around with things, seeing what i can do. just an experiment at the moment.
ProjektMayhem is offline   Reply With Quote
Old 03-16-2007, 10:24 PM   #7
Maximum Bitrate
 
Jarrod's Avatar
 
Join Date: Jan 2002
Location: Melb, Australia.
Posts: 475
Jarrod
haha, suuuuper late If a frontend thats available doesnt do the job for ya, code your own
__________________
Jarrod - Holden VX S!
Jarrod is offline   Reply With Quote
Old 06-13-2008, 03:34 AM   #8
Newbie
 
Join Date: Jun 2008
Posts: 1
salman_25 is an unknown quantity at this point
hello

i use ur code but my asp.net 2.0 can not recognize the ManagementEventWatcher

can tell me this is dll or ur genrated class

if it is ur genrated class or dll then please send me that class or dll.

if not then please tell me how to use this................

salman_interlogics@yahoo.com
salman_25 is offline   Reply With Quote
Advertisement
 
Advertisement
Sponsored links

Old 10-16-2009, 10:54 PM   #9
Newbie
 
Join Date: Oct 2009
Posts: 1
AdamBray is an unknown quantity at this point
USB Mouse Connection/Disconnection

Jarrod,

Thanks for posting this code - it works great for detecting USB keys. One thing I'm trying to do is figure out how to detect when a USB mouse is plugged in. I tried modifying the code to query Win32_PointingDevices and Win32_USBControllerDevice but doesn't seem to work. Any suggestions on how I can listen for a USB Mouse connect/disconnect?

Thanks again!

Adam
AdamBray is offline   Reply With Quote
Advertisement
 
Advertisement
Sponsored links

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
USB Roundup JPuma General Hardware Discussion 9 09-17-2006 05:47 PM
USB DVD/RW issues kamikaze2112 General Hardware Discussion 1 08-04-2006 11:43 AM
" HI-SPEED USB device is plugged into a non-HI-SPEED USB hub" Error - HELP!!! hd54321 General Hardware Discussion 15 05-04-2006 05:53 PM
My USB/Wifi troubles. JPuma General Hardware Discussion 0 09-23-2005 12:44 PM
Help! WRX2001 PhoneControl 3 01-25-2005 04:50 PM



All times are GMT -5. The time now is 03:34 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.2
Copyright © 1999 - 2008 Mp3Car.com Inc.Ad Management by RedTyger
Message Board Statistics