|
-
Jun 4th, 2015, 04:00 AM
#1
[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)
-
Jun 4th, 2015, 05:24 AM
#2
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!
-
Jun 4th, 2015, 05:57 AM
#3
Re: USB ports enable/disable by code
 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?
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)
-
Jun 4th, 2015, 07:07 AM
#4
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
-
Jun 4th, 2015, 07:52 AM
#5
Re: USB ports enable/disable by code
 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
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)
-
Jun 5th, 2015, 02:10 AM
#6
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"
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)
-
Jun 5th, 2015, 03:06 AM
#7
Re: [ALMOST resolved] USB ports enable/disable by code
 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"
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)
-
Jun 5th, 2015, 04:44 AM
#8
Re: [ALMOST resolved] USB ports enable/disable by code
 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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|