Jump to content
Sign in to follow this  
Colyn1337

Help with RegRead Results

Recommended Posts

Colyn1337

This code loads the result of a regread into a variable and then parses the result:

$RegResult = RegRead($HKLM & "\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters", "AutoShareWks")
    Select
        Case $RegResult = ""
            $RegWriteResult = RegWrite($HKLM & "\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters", "AutoShareWks", "REG_DWORD", "0")
            TrayTip("Registry Read", "AutoShareWks Reg key not found and was created. The read result was: " & $RegResult & " " & @error & _
                    ".  The write result was: " & $RegWriteResult & " " & @error, 5)
        Case $RegResult = "0" ; 0 indicates sharing is off
            TrayTip("Registry Read", "Auto Drive Sharing is currently DISABLED.", 5)
        Case $RegResult = "1" ; 1 indicates sharing is on
            TrayTip("Registry Read", "Auto Drive Sharing is currently ENABLED", 5)
    EndSelect

The problem is that the value of AutoShareWks is boolean and either 1 or 0.  The documentation for regread states that if failure of the regread, "" is returned.  However it seems to also interpret 0 as "" and trigger a false failure.  Even @error has the same issue.  If the reading value is 0, @error will trigger.

So, how can I get around this false failure trigger?

EDIT:

I figured out a work around like this:

$RegResult = RegRead($HKLM & "\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters", "AutoShareWks")
    Select
        Case $RegResult = "0" ; 0 indicates sharing is off
            TrayTip("Registry Read", "Auto Drive Sharing is currently DISABLED.", 5)
        Case $RegResult = "1" ; 1 indicates sharing is on
            TrayTip("Registry Read", "Auto Drive Sharing is currently ENABLED.", 5)
        Case Else ; Default Actions
            TrayTip("Registry Read", "AutoShareWks value not present in registry.  Auto Drive Sharing is currently ENABLED.", 5)
    EndSelect

However, still.......  Why is the error process not catching it correctly?

Edited by Colyn1337

Share this post


Link to post
Share on other sites
mikell

This works for me

; this regkey value is 0
$key = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Avidemux 2.5", "MementoSection_SecLangGerman")
If $key = "" Then
   If @error Then 
         msgbox(0,"", "failure")
   Else
         msgbox(0,"", "value = 0")
   EndIf
EndIf

Share this post


Link to post
Share on other sites
sahsanu

This code loads the result of a regread into a variable and then parses the result:

$RegResult = RegRead($HKLM & "\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters", "AutoShareWks")
    Select
        Case $RegResult = ""
            $RegWriteResult = RegWrite($HKLM & "\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters", "AutoShareWks", "REG_DWORD", "0")
            TrayTip("Registry Read", "AutoShareWks Reg key not found and was created. The read result was: " & $RegResult & " " & @error & _
                    ".  The write result was: " & $RegWriteResult & " " & @error, 5)
        Case $RegResult = "0" ; 0 indicates sharing is off
            TrayTip("Registry Read", "Auto Drive Sharing is currently DISABLED.", 5)
        Case $RegResult = "1" ; 1 indicates sharing is on
            TrayTip("Registry Read", "Auto Drive Sharing is currently ENABLED", 5)
    EndSelect

The problem is that the value of AutoShareWks is boolean and either 1 or 0.  The documentation for regread states that if failure of the regread, "" is returned.  However it seems to also interpret 0 as "" and trigger a false failure.  Even @error has the same issue.  If the reading value is 0, @error will trigger.

So, how can I get around this false failure trigger?

 

You are trying to compare a Number and a String in your Case statements so you have two solutions (maybe more ;-)):

Compare numbers (remove ""):

Case $RegResult = 0 ; 0 indicates sharing is off
    TrayTip("Registry Read", "Auto Drive Sharing is currently DISABLED.", 5)
Case $RegResult = 1 ; 1 indicates sharing is on
    TrayTip("Registry Read", "Auto Drive Sharing is currently ENABLED", 5)

Compare strings (use == instead of =):

Case $RegResult == "0" ; 0 indicates sharing is off
        TrayTip("Registry Read", "Auto Drive Sharing is currently DISABLED.", 5)
Case $RegResult == "1" ; 1 indicates sharing is on
        TrayTip("Registry Read", "Auto Drive Sharing is currently ENABLED", 5)

Take a look to Language Reference - Operators

I hope this helps.

Cheers,

sahsanu

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  

  • Similar Content

    • GeorgeB
      By GeorgeB
      I'm writing a little applet that basically tells you when Windows was installed.  There is a REG_DWORD in Windows that gives you this. It's basically a value that is the # of seconds from 1970.
      The location is:  "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\InstallDate"
      So if I run this in AutoIT, I should get the value displayed within the msgbox:
      MsgBox($MB_SYSTEMMODAL, "InstallDate Test", RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallDate"))
      However, what happens is it always returns a value of "0"  I tried this on several machines (Windows 8, Windows 8.1 and Windows 10). 
      Am I missing something?  If I manually view this REG_DWORD with RegEdit, it shows me the HEX value, or I can view the Decimal value. I don't care which value AutoIT reads, as I can always convert back and forth, but I just don't see why it can't read a value from this REG_DWORD.  As a test, I've read other REG_DWORD values, and with most it doesn't return any value, not even a 0.
      Please, even if you guys have some other (perhaps better) way to read the Windows install date, I would still like to find a resolution to this problem, because I want to understand why I am having so much difficulty with reading REG_DWORD values from the Windows Registry with AutoIT.
      Thanks for any help!
       
       
       
       
       
    • timmy2
      By timmy2
      I want to determine if AutoLogon is enabled on a Windows 10 Pro (64-bit) system. It's my understanding that the following registry key will exist and equal 1 if autologon is enabled, or equal 0 if disabled. 
      Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon So l looked up RegRead in AutoIt's help file and tested the example.
      #include <MsgBoxConstants.au3> Local $sVar = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir") MsgBox($MB_SYSTEMMODAL, "Program files are in:", $sVar) The resulting message box says:  C:\Program Files (x86)
      Regedit says the value in ProgramFilesDir is C:\Program Files. "C:\Program Files (x86)" is in a nearby key "ProgramFilesDir(x86)", which makes sense.
      I ignored this anomaly and tried RegRead in my own script:
      #include <MsgBoxConstants.au3> $isEnabled = RegRead("Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", "AutoAdminLogon") If $isEnabled = 1 then MsgBox($MB_SYSTEMMODAL, "", "Autologon enabled.") Else MsgBox($MB_SYSTEMMODAL, "", "Autologon disabled.") EndIf My punishment for ignoring the problem with the Help file example is that regardless of whether the AutoAdminLogon key equals 0 or 1 in reality, my script's $isEnabled variable returns 0.
      Despite the problem with the RegRead example I still figure I'm at fault, but I would appreciate someone pointing out my mistake, please. 
       
       
    • kelso
      By kelso
      Hello Guru's,
       
      I'm trying to write an autoit script to select from the dropdown list as you see in the attached picture. 
      I read the help page for _IEFormElementOptionSelect, but I cannot grasp how to correlate that with the source code that I'm seeing. any suggestions?

    • jdegraff
      By jdegraff
      I created a quick script to set/clear the topmost status of a window. It works like
          top <title> /on | /off
          
      I want the title match to be case insensitive and to work on partial strings. For example, if the window of note is titled "APLX for Windows" I want
          top apl /on
          
      to work. The AutoIt manual says that for WinTitleMatchMode
          Mode -1 to -4
              Force lower case match according to other type of match.
      so I assumed that by
          aut.Opt "WinTitleMatchMode",-1
          
      I would be doing a lower case match on existing windows. As long as I lcase(title) for the comparison it should match on "APLX for Windows" but it does not Is this a bug or am I misreading the docs?    
    • Ambient
      By Ambient
      I am trying to build a list from a query run against a sybase db. This part works fine. My problem is this, I want to display values in a list depending 
      on the value of certain fields  i.e. See below which works for two. Unfortunately, I need it to work for 3 values.  I don't know how I can do this for 3.  I did try doing a select case but I was getting errors.
       
      i.e. if value =0  then "Card Check" if Value = - Then "Card Spend" Else "Money Added"
      The below immediate iif works brilliantly for two but as I say I need to build this with 3 different values. 
       
       
      $idItem = GUICtrlCreateListViewItem( _iif($adoRs.Fields("TransactionAmount").value =0, "Card check","Money Added") 
       
      Any help appreciated.
×