How can I programmitcally Disable and Enable a USB device

Posted on 2006-04-06
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 6

    Expert Comment

    LVL 28

    Accepted Solution

    Maybe you can call some command line utilities from vb. Maybe devcon or deveject.;EN-US;Q311272

    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

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

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    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…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now