Jump to content
Sign in to follow this  
Akarillon

RegWrite function wont write even if I am admin[solved]

Recommended Posts

Akarillon

Hello again :mellow:

I an trying to make a script that changes the screensaver settings through registry on a domain useraccount.

This setting is located behind a string with the SID of that user.

My problem is when I try to write this information into the string it wont apply the changes.

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_x64=C:\Installationwizard\Remove_Screensaver.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Process.au3>
#Region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("Form1_1", 221, 174, 281, 190)
$TimeoutInput = GUICtrlCreateInput("TimeoutInput", 72, 40, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
$UsernameInput = GUICtrlCreateInput("Username", 72, 80, 121, 21)
$Label = GUICtrlCreateLabel("How many second timeout?", 72, 16, 135, 17)
$ON = GUICtrlCreateRadio("ON", 16, 16, 41, 17)
$OFF = GUICtrlCreateRadio("OFF", 16, 40, 41, 17)
$Start_button = GUICtrlCreateButton("Start", 24, 120, 75, 25)
$Exit_button = GUICtrlCreateButton("Exit", 112, 120, 75, 25)
$Label2 = GUICtrlCreateLabel("Username:", 16, 80, 55, 17)
GUISetState(@SW_SHOW)
$Form1 = GUICreate("Form1", 600, 175, 526, 192)
$Label1 = GUICtrlCreateLabel("Label1", 24, 8, 180, 17)
$Label2 = GUICtrlCreateLabel("Label2", 24, 40, 180, 17)
$Label3 = GUICtrlCreateLabel("Label3", 24, 72, 180, 17)
$Label4 = GUICtrlCreateLabel("Label4", 24, 104, 180, 17)
$Label5 = GUICtrlCreateLabel("Label5", 24, 136, 400, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $var[5]
$var[0] = 0 ;OFF
$var[1] = 0 ;ON
$var[2] = 0 ;Timeout
$var[3] = 0 ;Username
$var[4] = 0 ;SID
While 1
GUICtrlSetData( $Label1, $var[0])
GUICtrlSetData( $Label2, $var[1])
GUICtrlSetData( $Label3, $var[2])
GUICtrlSetData( $Label4, $var[3])
GUICtrlSetData( $Label5, $var[4])
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $OFF
   $var[0] = 1
   $var[1] = 0
  Case $ON
   $var[1] = 1
   $var[0] = 0
  Case $Start_button
   $var[2] = GUICtrlRead($TimeoutInput)
   $var[3] = GUICtrlRead($UsernameInput)
    _RunDOS('if not exist C:\temp\ mkdir C:\temp\ && wmic path win32_useraccount where name="' & $var[3] & '" get sid > C:\temp\sid.txt')
    FileOpen("C:\temp\sid.txt", 0)
    Sleep(200)
    $var[4] = FileReadLine("C:\temp\sid.txt", 2)
    $var[4] = StringStripWS($var[4], 8)
    Sleep(500)
    If $var[0] = 1 Then
     RegWrite("HKEY_USERS\" & $var[4] & "\Software\Policies\Microsoft\Windows\Control Panel\Desktop", "ScreenSaveActive", "REG_SZ", "0")
    EndIf
    If $var[1] = 1 Then
     RegWrite("HKEY_USERS\" & $var[4] & "\Software\Policies\Microsoft\Windows\Control Panel\Desktop", "ScreenSaveActive", "REG_SZ", "1")
     RegWrite('HKEY_USERS\"' & $var[4] & '"\Software\Policies\Microsoft\Windows\Control Panel\Desktop', "ScreenSaveTimeOut", "REG_SZ", $var[2])
     if @error Then msgbox(0, "Error", @error)
     ;msgbox(0, "Done", "Settings are changed")
    EndIf
  Case $Exit_button
   Exit
 
EndSwitch
WEnd

I tried to debug the script by putting in another GUI that shows all variables, but the variables are correct, it just wont apply.

And 1 more question: is the @error working so that it gives the error message of the last RegWrite?

PS! The RegWrite function works if I just put this simple script:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_x64=C:\installationwizard\Regtest.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
global $var[2]
$var[0] = 60
$var[1] = "S-1-5-21-1927809936-1189766144-1318725885-362183"
RegWrite("HKEY_USERS\" & $var[1] & "\Software\Policies\Microsoft\Windows\Control Panel\Desktop", "ScreenSaveTimeOut", "REG_SZ", $var[0])

Kind Regards,

Akarillon

Edited by Akarillon

Challenge accepted!

Share this post


Link to post
Share on other sites
water

I would suggest to do more error checking in your script. When RegWrite fails @error can have different values (according to the help file).

You check for "if @error then .." which is resolved to "if @error = True then ..." and because @error is numeric means "if @error = 1 then ...". So you only get the msgBox if @error = 1. But @error can be in the range from -2 to 3.

To put it short: Change "if @error then .." to "if @error <> 0 then .."


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
Akarillon

Tried both, and I only get error message 1, which in RegWrite indicates as "1 if unable to open requested key"

This narrows it down to 1 error, and that must be an error somewhere that prevents RegWrite from finding the correct path.

Even tried to remove 2 spaces that appears behind the SID that I get from the notepad.


Challenge accepted!

Share this post


Link to post
Share on other sites
Jos

this line:

RegWrite('HKEY_USERS\"' & $var[4] & '"\Software\Policies\Microsoft\Windows\Control Panel\Desktop', "ScreenSaveTimeOut", "REG_SZ", $var[2])

Should be:

RegWrite('HKEY_USERS\' & $var[4] & '\Software\Policies\Microsoft\Windows\Control Panel\Desktop', "ScreenSaveTimeOut", "REG_SZ", $var[2])

What is the exact content of $var[2] and $var[4]?

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
water

Do you run on a 64-bit Windows?

According to the RegWrite help file: "When running on 64-bit Windows if you want to write a key or value specific to the 64-bit environment you have to suffix the HK... with 64 i.e. HKLM64."


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Akarillon

String is successfully written :)

Think it was both Jos' and water's suggestions cominated that fixed it :mellow:

Now just to make it smaller and more robust ^^


Challenge accepted!

Share this post


Link to post
Share on other sites
rcmaehl

Here's an improved version of your script that SHOULD work:

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_x64=C:\Installationwizard\Remove_Screensaver.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Process.au3>
#Include <Security.au3>
#Region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("Form1_1", 221, 174, 281, 190)
$TimeoutInput = GUICtrlCreateInput("TimeoutInput", 72, 40, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
$UsernameInput = GUICtrlCreateInput("Username", 72, 80, 121, 21)
$Label = GUICtrlCreateLabel("How many second timeout?", 72, 16, 135, 17)
$ON = GUICtrlCreateRadio("ON", 16, 16, 41, 17)
$OFF = GUICtrlCreateRadio("OFF", 16, 40, 41, 17)
$Start_button = GUICtrlCreateButton("Start", 24, 120, 75, 25)
$Exit_button = GUICtrlCreateButton("Exit", 112, 120, 75, 25)
$Label2 = GUICtrlCreateLabel("Username:", 16, 80, 55, 17)
GUISetState(@SW_SHOW)
$Form1 = GUICreate("Form1", 600, 175, 526, 192)
$Label1 = GUICtrlCreateLabel("Label1", 24, 8, 180, 17)
$Label2 = GUICtrlCreateLabel("Label2", 24, 40, 180, 17)
$Label3 = GUICtrlCreateLabel("Label3", 24, 72, 180, 17)
$Label4 = GUICtrlCreateLabel("Label4", 24, 104, 180, 17)
$Label5 = GUICtrlCreateLabel("Label5", 24, 136, 400, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $var[5]
$var[0] = 0 ;OFF
$var[1] = 0 ;ON
$var[2] = 0 ;Timeout
$var[3] = 0 ;Username
$var[4] = 0 ;SID
While 1
GUICtrlSetData( $Label1, $var[0])
GUICtrlSetData( $Label2, $var[1])
GUICtrlSetData( $Label3, $var[2])
GUICtrlSetData( $Label4, $var[3])
GUICtrlSetData( $Label5, $var[4])
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $OFF
   $var[0] = 1
   $var[1] = 0
  Case $ON
   $var[1] = 1
   $var[0] = 0
  Case $Start_button
   $var[2] = GUICtrlRead($TimeoutInput)
   $var[3] = GUICtrlRead($UsernameInput)
   $sid =_Security__GetAccountSid($var[3])
   $var[4] = _Security__SidToStringSid(DllStructGetPtr($sid,1))
    Sleep(500)
    If $var[0] = 1 Then
     RegWrite("HKEY_USERS\" & $var[4] & "\Software\Policies\Microsoft\Windows\Control Panel\Desktop", "ScreenSaveActive", "REG_SZ", "0")
    EndIf
    If $var[1] = 1 Then
     RegWrite("HKEY_USERS\" & $var[4] & "\Software\Policies\Microsoft\Windows\Control Panel\Desktop", "ScreenSaveActive", "REG_SZ", "1")
     RegWrite('HKEY_USERS\' & $var[4] & '\Software\Policies\Microsoft\Windows\Control Panel\Desktop', "ScreenSaveTimeOut", "REG_SZ", $var[2])
     If @error <> 0 Then MsgBox(0, "Error", @error)
     ;MsgBox(0, "Done", "Settings are changed")
    EndIf
  Case $Exit_button
   Exit
EndSwitch
WEnd

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
BrewManNH

I would suggest to do more error checking in your script. When RegWrite fails @error can have different values (according to the help file).

You check for "if @error then .." which is resolved to "if @error = True then ..." and because @error is numeric means "if @error = 1 then ...". So you only get the msgBox if @error = 1. But @error can be in the range from -2 to 3.

To put it short: Change "if @error then .." to "if @error <> 0 then .."

Actually isn't it read as TRUE for any value other than zero?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
water

Actually isn't it read as TRUE for any value other than zero?

You are right. I've learned something new today :mellow:

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
GEOSoft

@Water

Add this one to the list.

The suffix isn't required. If you want an explaination you can PM me.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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!
       
       
       
       
       
    • antonioj84
      By antonioj84
      Hi all to the forum guru and expert I am trying to  automate this. in the registry  I have the network profile name network 2 and network   I want to  change their  CATEGORY  to Private .  Can someone lead me in the right direction.
      Private is 1 and Public is 0
      #RequireAdmin Global $sHKLMRoot = @OSArch = "x64" ? "HKLM64" : "HKLM" RegWrite($sHKLMRoot &"\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\NewNetworks" ,'/v NetworkList /t REG_MULTI_SZ /d 00000000 /f') see  attached picture below
      Much appreaciate
       

    • cahsobo
      By cahsobo
      Hello,

      I need help inserting these keys and values to registry
       
      [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DirectDraw\Compatibility\WK.1.exe]
      "Flags"=hex:00,08,00,00
      "ID"=hex:ec,33,74,3b
       
      Still can't figure out how...
    • nitron
      By nitron
      Hy to all, 
      I am really Sorry to come up with this question but i can't seem to solve the Problem.
      Its quite easy, I have been using RegNumKey for Years, but i seemed to lose track of something.
      For $ZaehlerLocal = 1 to 1200
            $RegKey = RegEnumKey("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall", $ZaehlerLocal)
            If @error <> 0 then ExitLoop
            $RegKey2=RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\"&$RegKey,"DisplayName")
            $RegKey3=RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\"&$RegKey,"UninstallString")
            $RegKey4=RegRead("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\"&$RegKey,"QuietUninstallString")

            if StringInStr($RegKey,"_Office15")==0 and StringInStr($RegKey2,"(German) 2013")==0 and StringInStr($RegKey,".KB")==0 and StringInStr($RegKey2,"Security update")==0 and StringInStr($RegKey2,"Framework")==0 Then
              FileWrite($FileHandleLocal,$RegKey&";")
              FileWrite($FileHandleLocal,$RegKey2&";")
              FileWrite($FileHandleLocal,$RegKey3&";")
              FileWriteline($FileHandleLocal,$RegKey4&";")
           EndIf
         Next
       
      Ive been using this to get all uninstall Strings from the Registry but for some reason, this doesn't work anymore. 
      I get some keys but not all, nore does it start with the first registry.
      As you can see in the picture, the Registry starts with {13DA9C7C-EBFB-40D0-94A1-55B42883DF21}
      but RegNumKey starts with Adressbook.
      Any Ideas what I am doing wrong? I tried HKLM64 instead as well, but with same result.
      Again sorry to bother, but i can't Find the mistake.
       

    • Simpel
      By Simpel
      Hi,
      I wondered why negative integers I wrote into registry (e.g. negative x-coordinates of a gui if using two monitors and the right one is the main one) wouldn't return right when reading. Now I know: it is saved as an unsigned integer (without algebraic sign). So here is a snippet that is changing unsigned to signed integer:
      Global Const $g_sRegKey = "HKEY_CURRENT_USER\Software\" & @ScriptName ; path to registry RegWrite($g_sRegKey, "Value", "REG_DWORD", -2147483647) ; write some negative integer into registry; -2147483647 highest possible negative integer , 2147483648 highest possible positive integer if talking of 32bit Local $sValue = RegRead($g_sRegKey, "Value") ; read out registry ConsoleWrite("Value: " & $sValue & @CRLF) ; show real value in console Local $sResult = _SignedInteger($sValue) ; change to signed value ConsoleWrite("Result: " & $sResult & @CRLF) ; and show it in console Func _SignedInteger($iUnsignedInteger) Local $iSignedInteger If $iUnsignedInteger > (2^31) Then ; then it means a negative integer $iSignedInteger = $iUnsignedInteger - (2^32) Else $iSignedInteger = $iUnsignedInteger EndIf Return $iSignedInteger EndFunc It took me some time to find out the problem and so I hope I can help somebody with this.
      Regards, Conrad
×