Jump to content

Open Regedit at a particular key


GEOSoft
 Share

Recommended Posts

You can use either the abbreviated or full key names (HKLM64 or HKEY_LOCAL_MACHINE64)

Example Usage

;
$sKey = "HKLM\SYSTEM\CurrentControlSet\Control"
_OpenRegEdit($sKey)
;

Functions

;
Func _OpenRegEdit($sKey)
   Local $sUser, $sReg, $sProc = "regedit.exe"
   $sKey = StringReplace($sKey,"HKU", "HKey_USERS")
   $sKey = StringReplace($sKey, "HKLM", "HKEY_LOCAL_MACHINE")
   $sKey = StringReplace($sKey, "HKCU", "HKEY_CURRENT_USER")
   $sKey = StringReplace($sKey, "HKCR", "HKEY_CLASSES_ROOT")
   $sKey = StringReplace($sKey, "HKCC", "HKEY_CURRENT_CONFIG")
   RegRead($sKey, "")
   If NOT StringRegExp(@Error, "-1|0") Then Return SetError(@Error)
   $sKey = "My Computer\" & $sKey
   $sUser = _UserID()
   If $sUser Then
      $sReg = "HKU\" & $sUser & "\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit"
      If RegWrite($sReg, "LastKey", "Reg_SZ", $sKey) Then
         If ProcessExists($sProc) Then ProcessClose($sProc)
         ShellExecute($sProc)
         Return True
      EndIf
   EndIf
   Return False
EndFunc  ;<==> _OpenRegEdit()

Func _UserID()
   Local $sKey = "HKCU\Software\Microsoft\Protected Storage System Provider\"
   Local $sEntry, $iReg = 1, $sRegEx = "(?i)(s-1-5-21-[\d|-]*)"
   While 1
      $sEntry = RegEnumKey($sKey, $iReg)
      If @Error Then ExitLoop
      $iReg += 1
      If NOT StringRegExp($sEntry, $sRegEx) Then ContinueLoop
      Local $aRegExp = StringRegExp($sEntry, $sRegEx, 1)
      Return $aRegExp[0]
   Wend
   Return False
EndFunc  ;<==> _UserID()
;
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Ermm ....

Func _OpenRegEdit($sKey)
    Local $sProc = "regedit.exe"
    $sKey = StringReplace($sKey,"HKU", "HKEY_USERS")
    $sKey = StringReplace($sKey, "HKLM", "HKEY_LOCAL_MACHINE")
    $sKey = StringReplace($sKey, "HKCU", "HKEY_CURRENT_USER")
    $sKey = StringReplace($sKey, "HKCR", "HKEY_CLASSES_ROOT")
    $sKey = StringReplace($sKey, "HKCC", "HKEY_CURRENT_CONFIG")
    RegRead($sKey, "")
    If @Error < -1 OR @Error > 0 Then Return SetError(@Error)
    $sKey = "My Computer\" & $sKey
    If RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", "Reg_SZ", $sKey) Then
        If ProcessExists($sProc) Then ProcessClose($sProc)
        ShellExecute($sProc)
        Return True
    EndIf
EndFunc  ;<==> _OpenRegEdit()

Surely drilling down in to HKEY_USERS with the right UserID is the same as going to HKEY_CURRENT_USER?

WBD

Link to comment
Share on other sites

Another thought ... perhaps it's avoidable to close the process ...

Func _OpenRegEdit($sKey)
    Local $sProc = "regedit.exe"
    $sKey = StringReplace($sKey,"HKU", "HKEY_USERS")
    $sKey = StringReplace($sKey, "HKLM", "HKEY_LOCAL_MACHINE")
    $sKey = StringReplace($sKey, "HKCU", "HKEY_CURRENT_USER")
    $sKey = StringReplace($sKey, "HKCR", "HKEY_CLASSES_ROOT")
    $sKey = StringReplace($sKey, "HKCC", "HKEY_CURRENT_CONFIG")
    RegRead($sKey, "")
    If @Error < -1 OR @Error > 0 Then Return SetError(@Error)
    If ProcessExists($sProc) Then
        _NavigateToKey($sKey)
        Return True
    Else
        $sKey = "My Computer\" & $sKey
        If RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", "LastKey", "Reg_SZ", $sKey) Then
            ShellExecute($sProc)
            Return True
        EndIf
    EndIf
EndFunc  ;<==> _OpenRegEdit()

Func _NavigateToKey($sKey)
    Local $i
    WinActivate("Registry Editor")
    ControlFocus("Registry Editor", "", "[CLASS:SysTreeView32; INSTANCE:1]")
    For $i = 1 To 30
        Send("{LEFT}")
    Next
    Send("{RIGHT}")
    For $i = 1 To StringLen($sKey)
        If StringMid($sKey, $i, 1) = "\" Then
            Send("{RIGHT}")
        Else
            Send(StringMid($sKey, $i, 1))
        EndIf
        Sleep(10)
    Next
EndFunc

WBD

Link to comment
Share on other sites

For your first reply. We need to write to the key in HKU which we could go directly to if there was only one user. If there are more than one user then we have to get the ID of the current user and go to that hive in HKU.

For your second reply. You could do that but I found it was fast enough to close and reopen. The odds are that you won't have regedit already open at that point anyway and close/reopen makes sure that everything is refreshed.

Thanks

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

[1] Perhaps it's a Vista thing (I'm still on XP). I always thought HKEY_CURRENT_USER points to exactly the same hive as HKEY_USERS\{SID of current user} but I'm happy to be wrong (it's not the first time).

[2] Absolutely. I was just posting an "example script" for another method and cheekily tagging it on to your post :D

I bow to the MVP ...

WBD

Edited by WideBoyDixon
Link to comment
Share on other sites

[1] Perhaps it's a Vista thing (I'm still on XP). I always thought HKEY_CURRENT_USER points to exactly the same hive as HKEY_USERS\{SID of current user} but I'm happy to be wrong (it's not the first time).

WBD

I don't use Vista, XP and Win 7. What you say is correct for most of the keys inside those 2 hives however the key that I need to write to is not one of them. It has to be done in HKU.

No Bowing allowed. I wouldn't mind but we don't have video on the forums so I wouldn't get to enjoy it.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Maybe...

If @Error <= -1 OR @Error > 0 Then Return SetError(@Error)oÝ÷ ØêÚºÚ"µÍÌÍÜÔYÈHYÔXY
    ÌÍÜÒÙ^K    ][ÝÉ][ÝÊBY  ÌÍÜÔYÈH    ][ÝÉ][ÝÈ[]Ù]ÜÜ

8)

Damn AutoIt Tags.

Can't do that because if you read the default and there is no value set it will return -1 as the error even though the key is indeed valid and does exist. That's the same reason you can't simply use If @Error Then ...... I had to recently fix the same thing in someone elses version of a KeyExists() function. I could ignore the -2 code as I recall but for the extra time it takes to type the line it might as well stay there.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I don't use Vista, XP and Win 7. What you say is correct for most of the keys inside those 2 hives however the key that I need to write to is not one of them. It has to be done in HKU.

No Bowing allowed. I wouldn't mind but we don't have video on the forums so I wouldn't get to enjoy it.

Just curious GEOSoft, what OS do you use?

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

I worded that wrong. I use XP SP3 and Win 7

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Maybe...

If @Error <= -1 OR @Error > 0 Then Return SetError(@Error)

8)

You gave me an idea for that line so I've changed it.

If NOT StringRegExp(@Error, "-1|0") Then Return SetError(@Error)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Just for fun ...

If Abs(@Error + 0.5) <> 0.5 Then Return SetError(@Error)

:D

WBD

Edited by WideBoyDixon
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...