![]() |
KnowBrainer Speech Recognition | ![]() |
Topic Title: Advanced Scripting - Accessing the registry? Topic Summary: Is there a simple way to create and read registry entries? Created On: 05/09/2022 06:46 PM Status: Post and Reply |
|
![]() |
![]() |
- noblemd | - 05/09/2022 06:46 PM |
![]() |
![]() |
- Alan Cantor | - 05/09/2022 07:57 PM |
![]() |
![]() |
- speechpro | - 05/10/2022 08:48 AM |
![]() |
![]() |
- noblemd | - 05/10/2022 07:18 PM |
![]() |
![]() |
- noblemd | - 05/10/2022 10:09 PM |
![]() |
|
Trying to create a registry entry the read it back when needed. ------------------------- Michael Noble DP 16 - Windows 11 Pro for Workstations Latest Update / MS Office 2021 LTSC
|
|
|
|
![]() |
|
I don't know whether it's possible to read and write registry values via Advanced Scripting.
If it's not possible, there is an indirect way. Use a third-party scripting tool like Macro Express or AutoHotkey to interact directly with the registry. Then trigger the script via Advanced Scripting. |
|
|
|
![]() |
|
Searching for "VBA" + (...) generally turns up things that can be implemented in Advanced Scripting. Also, [insert standard caveats on making registry changes]... https://www.google.com/search?q=vba+%2Bread%2Fwrite+registry |
|
|
|
![]() |
|
My solution gleaned from: Registry Functions (cpearson.com)
'#Language "WWB-COM" Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' API Constants '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Const HKEY_CURRENT_USER As Long = &H80000001 Public Const HKCU = HKEY_CURRENT_USER Private Const KEY_ALL_ACCESS = &H3F Private Const REG_CREATED_NEW_KEY = &H1 Private Const REG_OPENED_EXISTING_KEY = &H2 Private Const REG_OPTION_NON_VOLATILE = 0& Private Const REG_OPTION_VOLATILE = &H1 Private Const ERROR_SUCCESS = 0& Private Const MAX_DATA_BUFFER_SIZE = 1024 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' API Types '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Boolean End Type
Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type
Public Enum REG_DATA_TYPE REG_INVALID = -1 ' Invalid REG_SZ = 1 ' String REG_DWORD = 4 ' Long End Enum
Private Type ACL AclRevision As Byte Sbz1 As Byte AclSize As Integer AceCount As Integer Sbz2 As Integer End Type
Private Type SECURITY_DESCRIPTOR Revision As Byte Sbz1 As Byte Control As Long Owner As Long Group As Long Sacl As ACL Dacl As ACL End Type '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' API Declares ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Declare Function RegCloseKey Lib "advapi32.dll" ( _ ByVal HKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" ( _ ByVal HKey As Long, _ ByVal lpSubKey As String, _ ByVal Reserved As Long, _ ByVal lpClass As String, _ ByVal dwOptions As Long, _ ByVal samDesired As Long, _ lpSecurityAttributes As SECURITY_ATTRIBUTES, _ phkResult As Long, _ lpdwDisposition As Long) As Long
Private Declare Function RegSetValueExStr Lib "advapi32" Alias "RegSetValueExA" ( _ ByVal HKey As Long, _ ByVal lpValueName As String, _ ByVal Reserved As Long, _ ByVal dwType As Long, _ ByVal szData As String, _ ByVal cbData As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" ( _ ByVal HKey As Long, _ ByVal lpSubKey As String, _ ByVal ulOptions As Long, _ ByVal samDesired As Long, _ phkResult As Long) As Long
Private Declare Function RegQueryValueExStr Lib "advapi32" Alias "RegQueryValueExA" ( _ ByVal HKey As Long, _ ByVal lpValueName As String, _ ByVal lpReserved As Long, _ ByRef lpType As Long, _ ByVal szData As String, _ ByRef lpcbData As Long) As Long '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Application Constants '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Type RegValue ValueName As String ValueValue As Variant End Type '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Public Functions '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function RegistryGetValue(BaseKey As Long, KeyName As String, _ ValueName As String) As Variant
Dim HKey As Long Dim Res As Long Dim RegDataType As REG_DATA_TYPE Dim LenData As Long Dim LongData As Long Dim StringData As String Dim IntArr(0 To 1024) As Integer Dim LenStringData As Long
HKey = OpenRegistryKey(BaseKey:=BaseKey, KeyName:=KeyName) StringData = String$(MAX_DATA_BUFFER_SIZE, vbNullChar) LenStringData = Len(StringData) Res = RegQueryValueExStr(HKey:=HKey, lpValueName:=ValueName, lpReserved:=0&, _ lpType:=RegDataType, szData:=StringData, lpcbData:=LenStringData) If Res <> ERROR_SUCCESS Then RegCloseKey HKey RegistryGetValue = Null Exit Function End If ' StringData = TrimToNull(StringData) RegistryGetValue = StringData End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function RegistryCreateValue(BaseKey As Long, KeyName As String, _ ValueName As String, ValueValue As Variant, _ Optional CreateKeyIfNotExists As Boolean = False) As Boolean
Dim HKey As Long Dim Res As Long Dim DataType As REG_DATA_TYPE Dim StringValue As String Dim LongValue As Long
StringValue = CStr(ValueValue) HKey = OpenRegistryKey(BaseKey:=BaseKey, KeyName:=KeyName) If HKey = 0 Then RegCloseKey HKey RegistryCreateValue = False Exit Function End If Res = RegSetValueExStr(HKey:=HKey, lpValueName:=ValueName, Reserved:=0&, _ dwType:=REG_SZ, szData:=StringValue, cbData:=Len(StringValue)) If Res <> ERROR_SUCCESS Then RegistryCreateValue = False RegCloseKey HKey Exit Function End If RegCloseKey HKey RegistryCreateValue = True End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Function OpenRegistryKey(BaseKey As Long, KeyName As String) As Long Dim Res As Long Dim HKey As Long Res = RegOpenKeyEx(HKey:=BaseKey, lpSubKey:=KeyName, ulOptions:=0&, samDesired:=KEY_ALL_ACCESS, phkResult:=HKey) If Res <> ERROR_SUCCESS Then Exit Function OpenRegistryKey = HKey End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Function RegistryCreateKey(BaseKey As Long, KeyName As String) As Boolean
Dim Res As Long Dim HKey As Long Dim DataType As REG_DATA_TYPE Dim SecAttrib As SECURITY_ATTRIBUTES Dim Disposition As Long
Res = RegCreateKeyEx(HKey:=BaseKey, lpSubKey:=KeyName, Reserved:=0&, lpClass:="", _ dwOptions:=REG_OPTION_NON_VOLATILE, samDesired:=KEY_ALL_ACCESS, _ lpSecurityAttributes:=SecAttrib, phkResult:=HKey, lpdwDisposition:=Disposition) If Res <> ERROR_SUCCESS Then RegistryCreateKey = False Exit Function End If
RegistryCreateKey = True
End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim EKey As String Sub Main 'Creates -then reads key in HKEY_CURRENT_USER\MY_Key\MY123\ and displays the value in a message box RegistryCreateKey(HKCU, "MY_Key") RegistryCreateValue(HKCU, "MY_Key", "MY123","ThIs_iS_tHe_VaLuE") EKey = RegistryGetValue(HKCU, "MY_Key","MY123") MsgBox EKey End Sub
------------------------- Michael Noble DP 16 - Windows 11 Pro for Workstations Latest Update / MS Office 2021 LTSC
|
|
|
|
![]() |
|
After all the work and searching, I found an easier solution. ------------------------- Michael Noble DP 16 - Windows 11 Pro for Workstations Latest Update / MS Office 2021 LTSC
|
|
|
FuseTalk Standard Edition v4.0 - © 1999-2023 FuseTalk™ Inc. All rights reserved.