Jump to content

Access to the registry


Recommended Posts

I can not get write access to the registry. I'm trying to write here: HKEY_CLASSES_ROOT\CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}\shell
This is the section of the shortcut menu for the Computer icon. The owner is the SYSTEM. I try to do as follows:

Func __RegWrite($akey)
    Local $aAdjust, $aPrivileges[2] = [$SE_BACKUP_NAME, $SE_RESTORE_NAME]
    Local $hToken = _WinAPI_OpenProcessToken(BitOR($TOKEN_ADJUST_PRIVILEGES, $TOKEN_QUERY))
    _WinAPI_AdjustTokenPrivileges($hToken, $aPrivileges, $SE_PRIVILEGE_ENABLED, $aAdjust)
    If @error Or @extended Then
        Return 0
    EndIf
    Local $hRoot = StringLeft($akey, StringInStr($akey, "\") - 1)
    Switch $hRoot
        Case "HKEY_LOCAL_MACHINE", "HKLM", "HKEY_LOCAL_MACHINE32", "HKLM32", "HKEY_LOCAL_MACHINE64", "HKLM64"
            $hRoot = $HKEY_LOCAL_MACHINE
        Case "HKEY_USERS", "HKU", "HKEY_USERS32", "HKU32", "HKEY_USERS64", "HKU64"
            $hRoot = $HKEY_USERS
        Case "HKEY_CURRENT_USER", "HKCU", "HKEY_CURRENT_USER32", "HKCU32", "HKEY_CURRENT_USER64", "HKCU64"
            $hRoot = $HKEY_CURRENT_USER
        Case "HKEY_CLASSES_ROOT", "HKEY_CLASSES_ROOT64", "HKCR"
            $hRoot = $HKEY_CLASSES_ROOT
        Case Else
            Return SetError(1, 0, 0)
    EndSwitch
    Local $Subkey = StringTrimLeft($akey, StringInStr($akey, "\"))
    Local $hKey = _WinAPI_RegOpenKey($hRoot, $Subkey, $KEY_CREATE_SUB_KEY)
    Local $newKey = _WinAPI_RegCreateKey($hKey, $Subkey, $KEY_CREATE_SUB_KEY)
    _WinAPI_RegCloseKey($newKey)
    _WinAPI_RegCloseKey($hKey)
    _WinAPI_AdjustTokenPrivileges($hToken, $aAdjust, 0, $aAdjust)
    _WinAPI_CloseHandle($hToken)
EndFunc

What am I doing wrong?

Edited by musicstashall
Link to comment
Share on other sites

You check @error and @extended after each call to a _WinAPI* function.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Checked:

Local $hKey = _WinAPI_RegOpenKey($hRoot, $Subkey, $KEY_CREATE_SUB_KEY)
ConsoleWrite(@error & ' ' & @extended & ' ' & _WinAPI_GetLastErrorMessage() & @CR)

  — 10 2 Operation completed successfully

$newKey = _WinAPI_RegCreateKey($hKey, $Subkey, $KEY_CREATE_SUB_KEY)
ConsoleWrite(@error & ' ' & @extended & ' ' & _WinAPI_GetLastErrorMessage() & @CR)

  — 10 6 Operation completed successfully

Link to comment
Share on other sites

If this means that @error is returned as value 10 then the first function returning @error <> 0 is the cause of your problem.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Indeed the owner of main key ( HKEY_CLASSES_ROOT\CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}) is  trustedinstaller and the owner of the sub-key (HKEY_CLASSES_ROOT\CLSID\{20D04FE0-3AEA-1069-A2D8-08002B30309D}\shell) is system.

ttRunning autoit as a SYSYEM local service doesn't require changing the ownership of the key but it requires to run the compiled autoit as a local service.   That is more complicated than changing the ownership on the key.

Link to comment
Share on other sites

The task was solved only with the help of the third-party utility SetACL.exe

Func _RegSetAccess($a_key)
    RunWait(@ScriptDir & '\SetACL.exe -on ' & $a_key & ' -ot reg -actn setowner -ownr "n:S-1-5-32-544"', '', @SW_HIDE)
    RunWait(@ScriptDir & '\SetACL.exe -on ' & $a_key & ' -ot reg -actn ace -ace "n:S-1-5-32-544;p:full"', '', @SW_HIDE)
EndFunc

Func _RegGetAccess($a_key)
    Local $a_file = _TempFile()
    RunWait(@ScriptDir & '\SetACL.exe -on ' & $a_key & ' -ot reg -actn list -lst "w:o;s:y" -bckp ' & $a_file, '', @SW_HIDE)
    If StringInStr(FileRead($a_file), 'Owner:S-1-5-32-544') Then Return True
    Return False
EndFunc

 

Edited by musicstashall
Link to comment
Share on other sites

Did you mean this?

Global $_HKEY_LOCAL_MACHINE, $_HKEY_CLASSES_ROOT, $_HKEY_CURRENT_USER

Func DetectInfrastructure()
    If @OSTYPE = "WIN32_WINDOWS" Then
    Else
        If Not @AutoItX64 And @OSArch = "X86" Or (@OSArch = "X64" And @AutoItX64) Then
            $_HKEY_LOCAL_MACHINE = "HKEY_LOCAL_MACHINE"
            $_HKEY_CLASSES_ROOT = 'HKEY_CLASSES_ROOT'
            $_HKEY_CURRENT_USER = 'HKEY_CURRENT_USER'
        Else
            $_HKEY_LOCAL_MACHINE = "HKEY_LOCAL_MACHINE64"
            $_HKEY_CLASSES_ROOT = "HKEY_CLASSES_ROOT64"
            $_HKEY_CURRENT_USER = "HKEY_CURRENT_USER64"
        EndIf
    EndIf
    Return @OSArch
EndFunc   ;==>DetectInfrastructure

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...