[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?
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.
Re: USB ports enable/disable by code
Quote:
Originally Posted by
TysonLPrice
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?
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
Re: USB ports enable/disable by code
Quote:
Originally Posted by
techgnome
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
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:
'The call to enable USB ports:
regUpdateValue HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\services\USBSTOR", "start", "3"
Public Sub regUpdateValue(ByVal lngRootKey As Long, ByVal strRegKeyPath As String, ByVal strRegSubKey As String, varRegData As Variant)
Dim lngKeyHandle As Long, lngDataType As Long, lngKeyValue As Long, strKeyValue As String
lngDataType = REG_DWORD
'~~> Querying the key path
lngRetVal = RegCreateKey(lngRootKey, strRegKeyPath, lngKeyHandle)
'~~> Making it Null terminated
strKeyValue = Trim(varRegData) & Chr(0)
'~~> Setting the value
lngRetVal = RegSetValueEx(lngKeyHandle, strRegSubKey, 0&, lngDataType, ByVal strKeyValue, Len(strKeyValue))
'~~> Closing the key
lngRetVal = RegCloseKey(lngKeyHandle)
End Sub
I've modified it as follows:
VB Code:
'Enable USB ports
regUpdateValue HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\services\USBSTOR", "start", 3
Public Sub regUpdateValue(ByVal lngRootKey As Long, ByVal strRegKeyPath As String, ByVal strRegSubKey As String, varRegData As Variant)
Dim lngKeyHandle As Long, lngDataType As Long, lngKeyValue As Long
lngDataType = REG_DWORD
'~~> Querying the key path
lngRetVal = RegCreateKey(lngRootKey, strRegKeyPath, lngKeyHandle)
'~~> Setting the value
lngRetVal = RegSetValueEx(lngKeyHandle, strRegSubKey, 0&, lngDataType, varRegData, Len(varRegData))
'~~> Closing the key
lngRetVal = RegCloseKey(lngKeyHandle)
End Sub
But then I started regedit and instead of the value there was the string "32 bit DWORD value not valid"
Re: [ALMOST resolved] USB ports enable/disable by code
Quote:
Originally Posted by
krtxmrtz
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"
Re: [ALMOST resolved] USB ports enable/disable by code
Quote:
Originally Posted by
Bonnie West
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.