Jump to content

Test If Registry Key Exsists


Go to solution Solved by jguinch,

Recommended Posts

Just curious on this one as I noticed an issue today when setting up some automation for Outlook.

Normally I use this kind of code to see if a registry exists by checking for the default value on a given key (as some programs may not have any other keys and this one is always a given)

RegRead(HKCU\XXX\XXXX", "")
If @Error Then
;Key Not Found or Cant be Read
Else
;Key is found and this registry key is valid
EndIf

I had this working with the last automation I did as it would find the default key at the parent key for a given program.

Today with Outlook however it was giving me "not found" and after some google searching found that its because the default key was "no value" if I changed the value to anything even blank regread would then let me know that the key is valid.  So I could use that as part of my If statement to write some new keys to that location.

So not sure why it worked before and not now, as a temporary work around I am just searching for the Office14 Parent Key and using RegEnumKey so I can verify that Office14 exist but I was trying to be more specific and make sure a specific program of Office14 was installed.

In the future if I am working with a smaller program without  multiple keys in a tree and I only have a single master key to search for with possibly random key values, I would like to know if there is a way to search for the default key even if it has a value set for "No Value".

Link to comment
Share on other sites

 

 

I made my own function for this.

Func _RegExistKey($sKeyname)
    RegEnumVal ($sKeyname, 1)
    Return (@error <= 0)
EndFunc

 

This is basically what I already did as a work around but the problem is that if your key ONLY has the default key with no value then RegEnumVal will fail thus returning results as if the key did not exist.

This can be tested on my system with:

$Test = RegEnumVal("HKCU\Software\Microsoft\Windows Media", "1")
If @Error Then
MsgBox(0, "", "Not Found" & $Test & @Error)
Else
    MsgBox(0, "", "Found" & $Test)
    EndIf
Link to comment
Share on other sites

$isDomain = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\History", "MachineDomain")

if $isDomain = "MyDomain.com" Then
        ; Yay!

    Else
        ; Crap :(
endif

 

This is a standard regread, the post is trying to put emphasis on if a value does not exist other than default.  If I have other values I could search for them instead but at an administrative level I do not want to search for a value unless I know for a fact it will exist on all machines.  Often applications only have a key with values for user name, last server, etc things that are not static for every user thus the only safe value to look for is (Default) because it is created when the program is installed but before any key values are written from users adding there own software settings or configurations.

Link to comment
Share on other sites

  • Solution

my code works. Did you look at it ? It's not the same thing (test of @error)

; yours
$Test = RegEnumVal("HKCU\Software\Microsoft\Windows Media", "1")
If @Error Then
    MsgBox(0, "", "Not Found" & $Test & @Error)
Else
    MsgBox(0, "", "Found" & $Test)
EndIf


; mine
If _RegExistKey("HKCU\Software\Microsoft\Windows Media") Then
    MsgBox(0, "", "Found" & $Test)
Else
    MsgBox(0, "", "Not Found" & $Test & @Error)
EndIf



Func _RegExistKey($sKeyname)
    RegEnumVal ($sKeyname, 1)
    Return (@error <= 0)
EndFunc
Edited by jguinch
Link to comment
Share on other sites

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

 

my code works. Did you look at it ? It's not the same thing (test of @error)

; yours
$Test = RegEnumVal("HKCU\Software\Microsoft\Windows Media", "1")
If @Error Then
    MsgBox(0, "", "Not Found" & $Test & @Error)
Else
    MsgBox(0, "", "Found" & $Test)
EndIf


; mine
If _RegExistKey("HKCU\Software\Microsoft\Windows Media") Then
    MsgBox(0, "", "Found" & $Test)
Else
    MsgBox(0, "", "Not Found" & $Test & @Error)
EndIf



Func _RegExistKey($sKeyname)
    RegEnumVal ($sKeyname, 1)
    Return (@error <= 0)
EndFunc

 

Ok, that does look like it works.

So your just telling RegEnumVal to return a different @Error value?  Just trying to wrap my head around how its working, and how it would give a different result if the parent key was not valid. 

I am assuming that basically the @Error is always -1 if the Main key is valid but it just cant read the value of the sub key.  So if the @Error is 0 or higher set @Error as 0 when you return from the function.

If the main key was not valid @Error would be higher than 0 (probably 2) and the function would not send a modified @Error value and would pass the greater than 0 value.

sound about right?

Edited by ViciousXUSMC
Link to comment
Share on other sites

No, they're checking if the @error return value is zero or less, it works though = 0 would have been suffice. See my function instead for an alternative.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Ah OK. They just want the key. Sorry.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

We can mix the two functions  :):

Func _RegExists($sKeyname, $sValueName = "")
    Local $iRet
    RegRead($sKeyName, $sValueName)
    Return ( $svalueName = "" ? @error <= 0 : @error = 0 )
EndFunc
Edited by jguinch
Link to comment
Share on other sites

  • 6 years later...

Bonjour,  un peu tard soit mais jamais trop tard pour bien faire.

Voici ma solution :

;========================================================
Func REG_ExistKey($Key)

    Local $ii=1, $Existe=False, $Value

    while StringRight($Key,1)=="\"
        $Key=StringLeft($Key,StringLen($Key)-1)
    WEnd

    Local $Prefixe=StringSplit($Key,"\")
    Local $Suffixe=$Prefixe[$Prefixe[0]]
    $Prefixe=StringReplace($Key,"\"&$Suffixe,"")

    While 1
        $Value=RegEnumKey($Prefixe,$ii)
        If @error Then ExitLoop
        If StringCompare($Value,$Suffixe)==0 Then
            $Existe=True
            ExitLoop
        EndIf
        $ii+=1
    WEnd

    Return $Existe

EndFunc    ; --> REG_ExistKey
;========================================================
Func REG_ExistVal($Key,$Val)

    Local $ii=1, $Existe=False, $Value

    while StringRight($Key,1)=="\"
        $Key=StringLeft($Key,StringLen($Key)-1)
    WEnd

    While 1
        $Value=RegEnumVal($Key,$ii)
        If @error Then ExitLoop
        If StringCompare($Value,$Val)==0 Then
            $Existe=True
            ExitLoop
        EndIf
        $ii+=1
    WEnd

    Return $Existe

EndFunc    ; --> REG_ExistVal
;========================================================

Salutations.

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...