Multi User access to VB and VBA Program Settings Entries

Page 2 of 3 FirstFirst 123 LastLast

  1. Posts : 16,712
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #11

    My equivalent Registry Key
    HKEY_USERS\S-1-5-21-...-1001\SOFTWARE
    does not have permissions for general users to read it so it seems to be a non-starter after all.
    - Right-click on the Key, Permissions, Advanced

    Denis

    I will check anything else you want but I'll be going offline soon to do a quick 5 minute job which might take hours.
      My Computer


  2. Posts : 1,217
    W10-Pro 22H2
       #12

    I have just tried, and none of my attempts to read HKLM with VBA in Excel succeeded - but HKCU worked fine. I tried messing with the HKLM key permissions, but nothing made a difference. I found many references to others having the same difficulty with HKLM, but no solutions that both applied and made sense to me. I haven't given up...
      My Computer


  3. Posts : 16,712
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #13

    Martin,

    What Key did you try to read?
    Did the Reg Query alternative read it alright?
    What was your code?
    In addition to the HKLM Key you wanted to read, did you also try using my Reg Query & VBA code [reading some boring WD Key]?

    Denis

    I'm only halfway through the 5 minute job I started 5 hours ago so I might be slow to respond.
      My Computer


  4. Posts : 1,217
    W10-Pro 22H2
       #14

    I started by trying to read a key I created: HKLM\software\mng\test (a simple string). That always failed in HKLM, but worked fine when created in HKCU. My code is hardly worth looking at:
    Code:
    Sub test_ws()
        Set wsh = CreateObject("Wscript.Shell")
        cu = wsh.regread("HKCU\Software\mng\test")
        lm = wsh.regread("HKLM\Software\mng\test")
        'lm = wsh.regread("HKLM\Software\pdfforge\pdfcreator\program\applicationversion")
        'lm = wsh.regread("HKLM\Software\7-zip\Path")
     End Sub
    I added the extra tests on other keys just now to no avail (I don't have the key you used). Before that I had spent some time trying to do it using the Win-32 API calls, confident that that would work. Its a bit more complicated:
    Code:
    ' By MNG, but with thanks to the API-Guide guys who were at Allapi.net, now apparently no more'
    Const HKCU = &H80000001
    Const HKLM = &H80000002
    Const KEY_READ = &H20019
    Const KEY_ALL_ACCESS = &HF003F
    Const REG_SZ = 1 ' Unicode nul terminated string'
    Const REG_BINARY = 3 ' Free form binary'
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal samDesired As Long, phkResult As Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    
    Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
        Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
        
        lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize) 'get key info'
        If lResult = 0 Then
            If lValueType = REG_SZ Then
                strBuf = String(lDataBufSize, Chr$(0))  'Create a buffer'
                lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)  'retrieve value'
                If lResult = 0 Then RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)  'Remove nulls'
            ElseIf lValueType = REG_BINARY Then
                Dim strData As Integer
                lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize) 'retrieve value'
                If lResult = 0 Then RegQueryStringValue = strData
            End If
        End If
    End Function
    Sub test_api()
        RegOpenKeyEx HKLM, "software\\mng", 0, KEY_READ, Key
        Value = RegQueryStringValue(Key, "test")
        MsgBox Value, , "HK??"
    End Sub
    NB Too much time spent pairing the quote marks to get comments & strings in green!

    I just hand edit it to either read HKLM or HKCU - the former failed as with the scripting object, HKCU always worked.
    I did try starting Excel as an adminstrator (no change), I checked the permissions of both my 'mng' key, and the ones up the hierarchy - I apparently have full read (and sometimes write) access. Changing the key access to KEY_ALL_ACCESS didn't work - I didn''t expect it to, as that is setting what one wants to do, not giving oneself permission. So I am stumped!

    I can see many others in the past have had an issue reading HKLM from VBA, but have seen no solutions. I ahve seen references to a 32-64 bit split in Registry info, but nothing on how to get at the other half, if that is what is going on.
      My Computer


  5. Posts : 1,217
    W10-Pro 22H2
       #15

    I have an answer of sorts: there is a WOW6432 key under HKLM\software, and that seems to behave! So the OP could use that. The following works:
    Code:
    Sub test_ws()
        Set wsh = CreateObject("Wscript.Shell")
        lm = wsh.regread("HKLM\software\WOW6432Node\mng64\test")
        MsgBox lm
    End Sub
    The OP could try that? Martin the enthused!
      My Computer


  6. Posts : 16,712
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #16

    I have checked another computer and the code works there. On that computer [which is my TV recorder], I installed Office 2007 then lost interest in it without having done any setting up at all [I have not even got around to copying my own Office templates across].

    My original test Key-Value did not exist so I used this instead and it worked.
    Code:
    Sub RegReadTest()
    Dim WshShell As Object
    Set WshShell = CreateObject("WScript.Shell")
    ' The returned value is 5
    MsgBox WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\TPM\OSManagedAuthLevel")
    End Sub

    Since this method is merely running VBS within VBA, I wonder if yours would work in pure VBS. The code is almost the same. These examples work on mine.
    Example 1 - check that the Key exists beforehand.
    Code:
    Dim WshShell 
    Set WshShell = CreateObject("WScript.Shell")
    ' The returned value is 0 because I have not enabled that facility
    MsgBox WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender\PUAProtection")

    Example 2 - check that the Key exists beforehand.
    Code:
    Dim WshShell 
    Set WshShell = CreateObject("WScript.Shell")
    ' The returned value is 2 but I haven't a clue what that means.  I was merely looking for any Key in HKLM that might also exist on other computers.
    MsgBox WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\TPM\OSManagedAuthLevel")
    Do either of these Keys exist on yours and do the examples work?

    Denis
      My Computer


  7. Posts : 16,712
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #17

    It does look like there are special cases and that my initial tests merely happened to chance upon two of them.

    If I try another HKLM Key, this time one used by WinZip, VBA fails [but VBS passes].
    Multi User access to VB and VBA Program Settings Entries-vba-fails-vbs-works.png

    I can change the failure msg to that first seen by the OP ["Invalid root …"] by declaring the object in the VBA code. See the second of the following examples.
    Multi User access to VB and VBA Program Settings Entries-failure-msg.png

    Denis
    Last edited by Try3; 19 Jun 2021 at 10:18.
      My Computer


  8. Posts : 1,217
    W10-Pro 22H2
       #18

    Well, I have found some more interesting (?) stuff - all relating to HKLM keys & values:

    First off, Try3 (Denis), I can get the MS-TPS key SOFTWARE\Policies\Microsoft\TPM\OSManagedAuthLevel to read OK, and anything under Software\WOW6432Node works fine (as noted before).

    But then it seemed that after I had changed a permission on my own Software\mng key (that failed yesterday), it suddenly worked. I thought I had allowed full control & read for Creator owner - but when i went looking, the permissions had reverted - in fact, they don't 'stick'. So why did the key now work? I created several more values under the same key, and some, but not all, failed. But then I noticed that the ones that worked were not returning the values set in the key, but 'parallel' ones I had set in Software\WOW6432Node\mng - and where such a parallel value did not exist, then the read failed.

    If I create a value in Software\WOW6432Node\mng with no corresponding Software\mng value, then read the latter non-existent value, sure enough the 6432 value is read.

    I would call that behaviour bizarre! But as creating duplicate values is time-consuming and apparently pointless, my original suggestion that Software\WOW6432Node\ be used still stands.

    This has been interesting!

    PS I did confirm that all keys, whichever brach they are in, work fine when run direct from a VBS script - so its a VBA permissions issue.
    Last edited by mngerhold; 19 Jun 2021 at 10:57.
      My Computer


  9. Posts : 22
    Windows 10
    Thread Starter
       #19

    Multi User Access to VB and VBA Program Setting Entries


    Well I've given up and will explore a solution through a text file - in effect, a .ini file.

    Thanks to Denis for your assistance.
      My Computer


  10. Posts : 1,217
    W10-Pro 22H2
       #20

    Catfelix said:
    Well I've given up and will explore a solution through a text file - in effect, a .ini file. Thanks to Denis for your assistance.
    Well, to say I am disappointed would be an understatement! Is what I found out no use to you? I obviously don't know what controls the specific REG keys that InnoSetup creates - so downloaded it and had a look. I can't see your program setup (.iss file), but the 3rd example supplied appears to show that you have full control - some lines from it:
    Code:
    ; roaming profiles so settings like paths should be written to HKLM, which
    ; is only possible in administrative install mode.
    Root: HKLM; Subkey: "Software\My Company"; Flags: uninsdeletekeyifempty; Check: IsAdminInstallMode
    Root: HKLM; Subkey: "Software\My Company\My Program"; Flags: uninsdeletekey; Check: IsAdminInstallMode
    Root: HKLM; Subkey: "Software\My Company\My Program\Settings"; ValueType: string; ValueName: "InstallPath"; ValueData: "{app}"; Check: IsAdminInstallMode
    So why can't you use the WOW6432Node key as a starting point? Have you tried? Have a go! Martin
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 03:22.
Find Us




Windows 10 Forums