KnowBrainer Speech Recognition
Decrease font size
Increase font size
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
Linear : Threading : Single : Branch
 Advanced Scripting - Accessing the registry?   - noblemd - 05/09/2022 06:46 PM  
 Advanced Scripting - Accessing the registry?   - Alan Cantor - 05/09/2022 07:57 PM  
 Advanced Scripting - Accessing the registry?   - speechpro - 05/10/2022 08:48 AM  
 Advanced Scripting - Accessing the registry? - My solution   - noblemd - 05/10/2022 07:18 PM  
 Advanced Scripting - Accessing the registry?   - noblemd - 05/10/2022 10:09 PM  
Keyword
 05/09/2022 06:46 PM
User is offline View Users Profile Print this message

Author Icon
noblemd
Power Member

Posts: 48
Joined: 08/09/2019

Trying to create a registry entry the read it back when needed.



-------------------------

Michael Noble
-------------------------
Using $20 USB Mic on adjustable arm ($30 total cost - 99.9% recognition). 


DP 16 - Windows 11 Pro for Workstations Latest Update / MS Office 2021 LTSC


 


 


 


 


 


 


 


 05/09/2022 07:57 PM
User is offline View Users Profile Print this message


Alan Cantor
Top-Tier Member

Posts: 4532
Joined: 12/08/2007

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.
 05/10/2022 08:48 AM
User is offline View Users Profile Print this message


speechpro
Senior Member

Posts: 88
Joined: 11/09/2006

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



 05/10/2022 07:18 PM
User is offline View Users Profile Print this message

Author Icon
noblemd
Power Member

Posts: 48
Joined: 08/09/2019

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
-------------------------
Using $20 USB Mic on adjustable arm ($30 total cost - 99.9% recognition). 


DP 16 - Windows 11 Pro for Workstations Latest Update / MS Office 2021 LTSC


 


 


 


 


 


 


 




 05/10/2022 10:09 PM
User is offline View Users Profile Print this message

Author Icon
noblemd
Power Member

Posts: 48
Joined: 08/09/2019

After all the work and searching, I found an easier solution.

This solution is built into WinWrap Basic. https://www.winwrap.com/web2/basic/#!/ref/WWB-doc_group_overview.htm

It allows creation of and access to registry keys with simple instructions - All keys are put in HKEY_CURRENT_USER\Software\VB and VBA Program Settings

Note: If the first parameter starts with "..\" then "VB and VBA Program Settings\" is omitted and the entry is put under HKEY_CURRENT_USER\Software\.

----------------------------------------

'#Language "WWB-COM"


Sub Main
SaveSetting "..\MY_Key","MYKey","MYValue","This is the value"
Keyvalue = GetSetting("..\MY_Key","MyKey","MyValue")
MsgBox Keyvalue
End Sub

Much simpler programing.



-------------------------

Michael Noble
-------------------------
Using $20 USB Mic on adjustable arm ($30 total cost - 99.9% recognition). 


DP 16 - Windows 11 Pro for Workstations Latest Update / MS Office 2021 LTSC


 


 


 


 


 


 


 




Statistics
32529 users are registered to the KnowBrainer Speech Recognition forum.
There are currently 1 users logged in.
The most users ever online was 12124 on 09/09/2020 at 04:59 AM.
There are currently 404 guests browsing this forum, which makes a total of 405 users using this forum.

FuseTalk Standard Edition v4.0 - © 1999-2023 FuseTalk™ Inc. All rights reserved.