Jump to content

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


 Share

Recommended Posts

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!

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

WhyNotWin11
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Link to comment
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!

Link to comment
Share on other sites

  • Developers

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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

WhyNotWin11
Cisco FinesseGithubIRC UDFWindowEx UDF

 

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

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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!"

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

×
×
  • Create New...