Sign in to follow this  
Followers 0
ViciousXUSMC

Test If Registry Key Exsists

14 posts in this topic

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

Share this post


Link to post
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


C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

 

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

Share this post


Link to post
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.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

Ah OK. They just want the key. Sorry.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

So this works too

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

I tried both > < and <> just did not think to look for @Error meaning yes its working :) 

Share this post


Link to post
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
Sign in to follow this  
Followers 0