Sign in to follow this  
Followers 0
Akarillon

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

11 posts in this topic

#1 ·  Posted (edited)

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



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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

Share this post


Link to post
Share on other sites

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

#5 ·  Posted (edited)

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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                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

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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

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


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

Share this post


Link to post
Share on other sites

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

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

@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  
Followers 0

  • Similar Content

    • 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
    • damon
      By damon
      I am having an issue with windows 10 and regwrite.  when i run my script it acts as though it has updated the registry but when i view the registry it has not updated the keys.  I added error checking to the script but it is not showing errors.  now if i remove #requireadmin i will get error 1 - unable to open requested key.
      her is my code.
       
      #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <Process.au3> ; Declared Variables Global $userName = "user" Global $userPassword = "Ktesting" Global $autologin = "1" Global $notification = "0" Global $balloon = "0" If @OSArch = "X64" Then Global $HKLM = "HKLM64" Else Global $HKLM = "HKLM" EndIf ; Looks for switch on program start If $CmdLine[0] > 0 Then ; looks for switch on startup If $CmdLine[1] = "/boot" Then BootMachine() Else MachineInstallation() EndIf ; Setup Kiosk for first time Func MachineInstallation() #RequireAdmin MsgBox (1, "Installation", "Starting Install") ; Add Program to Run registry Key RegWrite ($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "DL Kiosk", "REG_SZ", "c:\temp\DL Testing Kiosk\dl testing kiosk.exe /boot") If @error Then MsgBox (1, "error", @error) EndIf ; Add Automatic Logon Registry Key RegWrite ($HKLM & "\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon", "AutoAdminLogon", "REG_SZ", $autologin) If @error Then MsgBox (1, "error", @error) EndIf RegWrite ($HKLM & "\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon", "DefaultUserName", "REG_SZ", $userName) If @error Then MsgBox (1, "error", @error) EndIf RegWrite ($HKLM & "\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon", "DefaultPassword", "REG_SZ", $userPassword) If @error Then MsgBox (1, "error", @error) EndIf ; Pop Ups and Notification Center Registry RegWrite ("HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\Explorer", "DisableNotificationCenter", "REG_SZ", $notification) If @error Then MsgBox (1, "error", @error) EndIf RegWrite ("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", "EnableBalloonTips", $balloon) If @error Then MsgBox (1, "error", @error) EndIf MsgBox (1, "Installation", "Install completed") EndFunc Exit  
      EDIT:
      i think i figured out my problem.  I was missing the space between Windows and NT.  user error, sorry.  
    • GreenSparks
      By GreenSparks
      Hi!
      I post here because i was wonderring if someone in this forum could help me with my script.
      What i'm trying to do, is to basically install a new keyboard layout in Windows and activate it. I want to acheive by the registry or the command line, but not with the autoit automation tools, i don't find it stable...
      Thanks for your help and have a good day!
    • Leo1906
      By Leo1906
      Hello guys,
      I'm having a problem right here ..
      I was trying to create a few Registry Values using autoit, but it seems to me that Autoit can't write those values to this specific part of the registry ..
      Here is the code I'm trying to run:
      RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\test\command", "", "REG_SZ", "C:\test\convert.exe %1 geojson|convert") I'm definitly sure that the path is correct. Script is runned with Admin Rights. Everything's fine here.
      It's possible to read other values stored by Windows in this directory:
      MsgBox(0, 0, RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Windows.burn", "Description")) But you can't edit those using Autoit. I don't know what this is. In my eyes it's an Autoit Bug or something like that ...
      Possible solution for me is to write a ".reg" file and run it afterwards, but this is an unclean method I don't really want to use ..
      Any suggestions on this?
       
      Edit: the OS is Windows 7 x64 German Language btw ..