Snippets ( Registry )

From AutoIt Wiki
Jump to navigation Jump to search

Please always credit an author in your script if you use their code. It is only polite.


_IsRegistryExist

Author: guinness








ConsoleWrite(_IsRegistryExist("HKEY_CURRENT_USER\Control Panel\Desktop", "Test") & @LF) ; False = Doesn't Exist
ConsoleWrite(_IsRegistryExist("HKEY_CURRENT_USER\Control Panel\Desktop", "CaretWidth") & @LF) ; True = Exists

Func _IsRegistryExist($sKeyName, $sValueName)
    RegRead($sKeyName, $sValueName)
    Return @error = 0
EndFunc   ;==>_IsRegistryExist

ReturnToContents

_Reg_GetSID

Author: GEOSoft








; Returns the SID for the current user.

$sUser = _Reg_GetSID()
If $sUser Then MsgBox(0, "RESULTS", $sUser)

Func _Reg_GetSID()
   Local $sKey = "HKU\", $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;<==> _Reg_GetSID()

ReturnToContents

_RegReadEX

Author: Realm








; Returns default error message and code if key is missing
ConsoleWrite( 'Program Files Directory= ' & _RegReadEX( 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion', 'ProgramFilesDir', '<Key is not present>') & @LF)
ConsoleWrite( 'Default Return= ' & _RegReadEX( 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion', 'A Missing Key Test', '<Key is not present>') & ' Error= ' & @error & @LF)

Func _RegReadEX($sKeyName, $sValueName, $sDefault)
    Local $var = RegRead($sKeyName, $sValueName)
    If @error Then Return SetError(@error,0,$sDefault)
    Return $var
EndFunc

ReturnToContents