[Webinar] Learn how to a build a cloud-first strategyRegister Now


How can I programmitcally Disable and Enable a USB device

Posted on 2006-04-06
Medium Priority
Last Modified: 2012-05-05
I'm using a USB to Serial Converter (Moxa brand) box.  A few times a week this box locks up.  To reset the box, I can reboot the computer, or unplug the box, or Disable/Enable USB device in device manager.   I'm probably eventually going to replace the converter box with a conventional RS232 Serial board.  In the meantime, I think there must be a way to programmitically Disable and then Enable this USB device.
Question by:dve01
LVL 28

Accepted Solution

vinnyd79 earned 2000 total points
ID: 16397550
Maybe you can call some command line utilities from vb. Maybe devcon or deveject.



I tried using deveject from vb to eject a USB drive so it can be safely removed.
I put deveject in my system32 folder and then used:

Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Const STILL_ACTIVE = &H103

Private Function ShellWait(PathName, Optional WindowStyle As VbAppWinStyle = vbNormalFocus) As Double
Dim hProcess As Long, RetVal As Long
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(PathName, WindowStyle))
        GetExitCodeProcess hProcess, RetVal
        DoEvents: Sleep 100
    Loop While RetVal = STILL_ACTIVE
End Function

Private Sub Command1_Click()
 ShellWait Environ("ComSpec") & " /c deveject -EjectName:" & Chr$(34) & "USB Mass Storage Device" & Chr$(34) & " > C:\Temp.txt", vbHide
Dim ff As Integer
ff = FreeFile

' display results
Open "C:\Temp.txt" For Input As #ff
fData = Input$(LOF(ff), ff)
Close #ff
MsgBox fData
Kill "C:\Temp.txt"
End Sub

Author Comment

ID: 16401591
I tried Devcon and shelled out from project.  Seems to work pretty good

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question