Jump to content

Recommended Posts

Posted (edited)

This function will return a boolean value on whether a registry string is correctly formatted. Any suggestions for improvements then please post below. Thanks.

Function:

; #FUNCTION# ====================================================================================================================
; Name ..........: _IsRegKey
; Description ...: Check if a string is a valid registry string.
; Syntax ........: _IsRegKey($sRegistryKey)
; Parameters ....: $sRegistryKey        - A registry string.
;                  $fFullPath           - [optional] A fullpath is required. Default is False.
; Return values .: Success - True
;                  Failure - False
; Author ........: guinness
; Modified ......: Robjong - Improved syntax as well as adding additional parameter to check against a fullpath.
; Example .......: Yes
; ===============================================================================================================================
Func _IsRegKey($sRegistryKey, $fFullPath = Default)
    Local $sPathSRE = $fFullPath ? '+' : '*' ; Requires fullpath
    Return StringRegExp($sRegistryKey, '(?i)\A\h*HK(CC|CR|CU|LM|U|EY_CLASSES_ROOT|EY_LOCAL_MACHINE|EY_USERS|EY_CURRENT_(USER|CONFIG))(64)?(?:\\[^\\]*)' & $sPathSRE & '\z') = 1
EndFunc   ;==>_IsRegKey
Example use of Function:

Example()

Func Example()
    _Output('HKLM', True)
    _Output('HKLM\', True)
    _Output('HKLM\Example\Test', True)
    _Output('HKU\Example\Test', True)
    _Output('HKCU\Example\Test', True)
    _Output('HKCR\Example\Test', True)
    _Output('HKCC\Example\Test', True)
    _Output('HKEY\Example\Test', False) ; This will return False.

    _Output('HKEY_LOCAL_MACHINE64\Example\Test', True)
    _Output('HKEY_USERS\Example\Test', True)
    _Output('HKEY_CURRENT_USER\Example\Test', True)
    _Output('HKEY_CLASSES_ROOT\Example\Test', True)
    _Output('HKEY_CLASSES_ROOT\Example\Test', True)
    _Output('HKEY_CURRENT_CONFIG\Example\Test', True)

    _Output('HKCR_CLASSES_ROOT\Example\Test', False) ; This will return False.

    _Output('HKEY_CURRENT_EXAMPLE\Example\Test', False) ; This will return False.
EndFunc   ;==>Example

Func _Output($sString, $fExpect)
    Local $fResult = _IsRegKey($sString)
    Local $sSciTEOutput = ($fResult == $fExpect)? '+' : '!'
    ConsoleWrite($sSciTEOutput & ' [' & $fResult & '] ' & $sString & @CRLF)
EndFunc   ;==>_Output
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

A different approach without using a regular expression.

 

Example()

Func Example()
    _Output('HKLM', True)
    _Output('HKLM\', True)
    _Output('HKLM\Example\Test', True)
    _Output('HKU\Example\Test', True)
    _Output('HKCU\Example\Test', True)
    _Output('HKCR\Example\Test', True)
    _Output('HKCC\Example\Test', True)
    _Output('HKEY\Example\Test', False) ; This will return False.

    _Output('HKEY_LOCAL_MACHINE64\Example\Test', True)
    _Output('HKEY_USERS\Example\Test', True)
    _Output('HKEY_CURRENT_USER\Example\Test', True)
    _Output('HKEY_CLASSES_ROOT\Example\Test', True)
    _Output('HKEY_CLASSES_ROOT\Example\Test', True)
    _Output('HKEY_CURRENT_CONFIG\Example\Test', True)

    _Output('HKCR_CLASSES_ROOT\Example\Test', False) ; This will return False.

    _Output('HKEY_CURRENT_EXAMPLE\Example\Test', False) ; This will return False.
EndFunc   ;==>Example

Func _Output($sString, $fExpect)
    Local $fResult = _IsRegKey($sString)
    Local $sSciTEOutput = ($fResult == $fExpect)? '+' : '!'
    ConsoleWrite($sSciTEOutput & ' [' & $fResult & '] ' & $sString & @CRLF)
EndFunc   ;==>_Output

; #FUNCTION# ====================================================================================================================
; Name ..........: _IsRegKey
; Description ...: Check if a string is a valid registry string.
; Syntax ........: _IsRegKey($sRegistryKey)
; Parameters ....: $sRegistryKey        - A registry string.
; Return values .: Success - True
;                  Failure - False
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _IsRegKey($sRegistryKey)
    Local Const $sRegistryKeys = ';HKCC;HKCR;HKCU;HKEY_CLASSES_ROOT;HKEY_CURRENT_CONFIG;HKEY_CURRENT_USER;HKEY_LOCAL_MACHINE;HKEY_USERS;HKLM;HKU;'
    If StringRight($sRegistryKey, 1) <> '\' Then $sRegistryKey &= '\'
    Return StringInStr($sRegistryKeys, ';' & StringReplace(StringLeft($sRegistryKey, StringInStr($sRegistryKey, '\', Default, 1) - 1), '64', '') & ';') > 0
EndFunc   ;==>_IsRegKey
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

A big thanks to Robjong who greatly improved the syntax as well as the example. I have updated the function with their invaluable input. Thank you.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 3 months later...
Posted

Fixed the Forum messing up the example and function about. The backslashes have now been added again. Thanks.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 4 months later...
Posted (edited)

You mean can I explain it further? Sure. If you set the parameter to True then if a key is just HKLM or HKEY_CLASSES_ROOT it will fail, it must contain at least one backslash in the path e.g. HKEY_CLASSES_ROOT to return True.

Subsequently if the parameter is set to False/Default then HKEY_CLASSES_ROOT will return True, as it's not as strict with checking.

Let me know if you need further explanation?

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

No problem.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 10 months later...
Posted

Updated both examples and the functions as the Forum had removed the backslashes.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
  • Recently Browsing   0 members

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