Results 1 to 8 of 8

Thread: [RESOLVED] USB ports enable/disable by code

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] USB ports enable/disable by code

    In this registry key

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR

    I want to modify by code the value of Start to 3 or 4 to enable/disable the USB ports.

    However, my googling for a solution has left me somewhat confused. What's the most straightforward solution?
    Last edited by krtxmrtz; Jun 5th, 2015 at 04:44 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: USB ports enable/disable by code

    A member here took the time to write something up on that:

    http://www.vbforums.com/showthread.p...plete-Tutorial

    Where I work that is secured by group policy and cannot be updated without some type of hack. Management doesn't want anything going out the door via that method.
    Please remember next time...elections matter!

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: USB ports enable/disable by code

    Quote Originally Posted by TysonLPrice View Post
    A member here took the time to write something up on that:

    http://www.vbforums.com/showthread.p...plete-Tutorial

    Where I work that is secured by group policy and cannot be updated without some type of hack. Management doesn't want anything going out the door via that method.
    All right, I assume I must use:

    SaveSetting appname, section, key, setting

    but I'm not sure I can identify the parameters.

    Probably key is "start" and value is 3 but how about appname and section? Which part of

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR

    is appname and which is section, where do I have to split it?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: USB ports enable/disable by code

    SaveSetting won't work... it writes to a specific section of the registry... if you want to get to other hives or other sections, you need to use the API calls... in the linked thread, it starts with post #8.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: USB ports enable/disable by code

    Quote Originally Posted by techgnome View Post
    SaveSetting won't work... it writes to a specific section of the registry... if you want to get to other hives or other sections, you need to use the API calls... in the linked thread, it starts with post #8.

    -tg
    Thank you.

    I've finally successfully used the code from post #8 in

    http://www.vbforums.com/showthread.p...g-Registry-Key
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: USB ports enable/disable by code

    I've just realized the value must be REG_DWORD rather than REG_SZ and for this reason the USB port wasn't actually enabled when the value was set to 3.
    How do I build the strKey value in this case?

    This is what my code looks like:
    VB Code:
    1. 'The call to enable USB ports:
    2.  
    3.     regUpdateValue HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\services\USBSTOR", "start", "3"
    4.  
    5. Public Sub regUpdateValue(ByVal lngRootKey As Long, ByVal strRegKeyPath As String, ByVal strRegSubKey As String, varRegData As Variant)
    6.     Dim lngKeyHandle As Long, lngDataType As Long, lngKeyValue As Long, strKeyValue As String
    7.  
    8.     lngDataType = REG_DWORD
    9.    
    10.     '~~> Querying the key path
    11.     lngRetVal = RegCreateKey(lngRootKey, strRegKeyPath, lngKeyHandle)
    12.    
    13.     '~~> Making it Null terminated
    14.     strKeyValue = Trim(varRegData) & Chr(0)
    15.    
    16.     '~~> Setting the value
    17.     lngRetVal = RegSetValueEx(lngKeyHandle, strRegSubKey, 0&, lngDataType, ByVal strKeyValue, Len(strKeyValue))
    18.              
    19.     '~~> Closing the key
    20.     lngRetVal = RegCloseKey(lngKeyHandle)
    21.    
    22. End Sub

    I've modified it as follows:

    VB Code:
    1. 'Enable USB ports
    2.     regUpdateValue HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\services\USBSTOR", "start", 3
    3.    
    4. Public Sub regUpdateValue(ByVal lngRootKey As Long, ByVal strRegKeyPath As String, ByVal strRegSubKey As String, varRegData As Variant)
    5.     Dim lngKeyHandle As Long, lngDataType As Long, lngKeyValue As Long
    6.  
    7.     lngDataType = REG_DWORD
    8.    
    9.     '~~> Querying the key path
    10.     lngRetVal = RegCreateKey(lngRootKey, strRegKeyPath, lngKeyHandle)
    11.    
    12.     '~~> Setting the value
    13.     lngRetVal = RegSetValueEx(lngKeyHandle, strRegSubKey, 0&, lngDataType, varRegData, Len(varRegData))
    14.              
    15.     '~~> Closing the key
    16.     lngRetVal = RegCloseKey(lngKeyHandle)
    17.    
    18. End Sub

    But then I started regedit and instead of the value there was the string "32 bit DWORD value not valid"
    Last edited by krtxmrtz; Jun 5th, 2015 at 02:31 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [ALMOST resolved] USB ports enable/disable by code

    Quote Originally Posted by krtxmrtz View Post
    What's the most straightforward solution?
    Probably the RegWrite method of the WshShell object:

    Code:
    CreateObject("WScript.Shell").RegWrite "HKLM\SYSTEM\CurrentControlSet\services\USBSTOR\Start", 3&, "REG_DWORD"
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [ALMOST resolved] USB ports enable/disable by code

    Quote Originally Posted by Bonnie West View Post
    Probably the RegWrite method of the WshShell object:

    Code:
    CreateObject("WScript.Shell").RegWrite "HKLM\SYSTEM\CurrentControlSet\services\USBSTOR\Start", 3&, "REG_DWORD"
    You're right! Thanks.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width