Page 1 of 1 1
Topic Options
#214200 - 2023-02-20 10:50 PM Kixtart cannot create a blank/null registry value
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
 Code:
Function CheckAndSetRegValue($RegPath, $RegValue, $RegData, $RegType)
  
	$ReturnValue = ReadValue($RegPath, $RegValue)
  
	If ($ReturnValue = $RegData)
		? "Registry value already applied"
	Else
		WriteValue($RegPath, $RegValue, $RegData, $RegType)
		? "Registry value is being set for: "
	EndIf
	
EndFunction

CheckAndSetRegValue($PCSoftware + "\Policies\Mozilla\Firefox", "OverrideFirstRunPage", "", "REG_SZ")



I want the data field of the Registry Value OverrideFirstRunPage to be empty/blank/null. However, I have to put a space in between the apostrophes, otherwise the registry entry is not added. Is there some way to get Kixtart to create a registry value with no data.

https://admx.help/?Category=Firefox&Policy=Mozilla.Policies.Firefox::OverrideFirstRunPage

Top
#214203 - 2023-02-22 12:36 PM Kixtart can't create empty data in registry
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
 Code:
;============================================================================================================
;   Checks the registry VALUE and if incorrect, it applies correct setting, otherwise says already applied
;============================================================================================================

Function CheckAndSetRegValue($RegPath, $RegValue, $RegData, $RegType)
  
	$ReturnValue = ReadValue($RegPath, $RegValue)
  
	If ($ReturnValue = $RegData)
		? "Registry value already applied"
	Else
		WriteValue($RegPath, $RegValue, $RegData, $RegType)
		? "Registry value is being set for: "
	EndIf
	
EndFunction

CheckAndSetRegValue($PCSoftware + "\Policies\Mozilla\Firefox", "OverrideFirstRunPage", "", "REG_SZ")



The data field for the Registry Value "OverrideFirstRunPage" is null or empty. However, Kixtart will not create the value if the data field is empty. I have to put in at least a space, which is incorrect as a space is something, whereas the data field needs to be null.

I posted this a couple of days ago. But for some reason, my post vanished into thin air. It might be because I logged out immediately after posting. This time I will try waiting until it goes back to the previous page!

Top
#214204 - 2023-02-22 02:02 PM Re: (NA) Kixtart can't create empty data in registry [Re: Robdutoit]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
A) Posts need to be approved - it's possible that none of the mods were available to approve it (until now).

B) - the WriteValue function requires all arguments. If RegType is string, RegData must be "" (empty string), and must be zero for other types. I'm not getting an error unless the data isn't defined (not specified in function).

C) you should be capturing the return code 0 with $Rc = WriteValue(), otherwise you'll litter the screen with result codes.

D)If statements are not functions and do not require parentheses.

E) it's generally considered "bad form" for functions to generate any output. Functions should do a job and return a result. The result can be evaluated and appropriate messages displayed.

F) Declare variables to prevent interference from other functions and main code.
_________________________
Actually I am a Rocket Scientist! \:D

Top
#214205 - 2023-02-22 02:17 PM Re: (NA) Kixtart can't create empty data in registry [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
PS - merged your posts.

I'm not seeing what the actual problem is as the code you posted works. Note that I did change the reg path to a test path in HKCU where I had access rights.

When you call it the first time, if the value isn't defined and you pass the empty string, they match and the first message is displayed. When you define a value, it says it's being set and the item is created or updated. Run again with the same value and it displays the first message again. Change the value, it updates the value and displays the second message.

The second message should probably have the value var, otherwise it just says "Registry value is being set for:"

_________________________
Actually I am a Rocket Scientist! \:D

Top
#214206 - 2023-02-22 04:25 PM Re: (NA) Kixtart can't create empty data in registry [Re: Glenn Barnas]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
The below seems to work fine for me as well. With no space required in the data field to create the value.

 Code:
$nul = writevalue("HKEY_CURRENT_USER\Test", "OverrideFirstRunPage", "", "REG_SZ")

Top
#214207 - 2023-02-22 08:55 PM Re: (NA) Kixtart can't create empty data in registry [Re: ShaneEP]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
Yes, I see what the problem is now. How stupid of me to miss that. Because the data is null/empty/blank, the $ReturnValue = $RegData therefore will always return ? "Registry value already applied"

I need to modify the code for this one to check if $RegValue exists, if not writevalue, then on subsequent logins check what $RegData equals.

A - Noted

B - Noted as above

C - I will add 0 $Rc = WriteValue(). Didn't realise it was required. But I will put that into my UDF file.

D - I never knew when parentheses were required, so just generally added them and Kixtart forgives me! I will put this onto my project list to go through the coding (which is now considerably leaner simpler than it used to be). So shouldn't be a problem to remove parentheses that are not required.

E - Are you referring to ? "Registry value already applied" returns. This is usually remmed out and only made available during debugging when I am having a problem getting code to work as expected? Or is this related to point C - which I will fix.

F - I will look into this as you are correct, I should declare variables for each function.

ShaneP - the problem wasn't as I thought with the writevalue command. I neglected to realise that because the data value is blank, my udf function code always returned the result - ? "Registry value already applied" For this particular registry addition, I need to check if the regvalue "OverrideFirstRunPage" exists as well as checking the return of the regdata.

Top
#214208 - 2023-02-22 10:49 PM Re: (NA) Kixtart can't create empty data in registry [Re: Robdutoit]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
The general rule for Parens is:

STATEMENTS (ie: Kix COMMANDS) do not require parenthesis and generally do not return data but may return status via @ERROR/@SERROR (see MD and RD for exaples).
A Statement may take args in a specific syntax. IF is a statement.

FUNCTIONS always require parenthesis, and can return both Values and Result Codes. A value is returned when you call a function as $V = Function(arg). The result code is returned by @ERROR.

Most functions that I write return a value (use an array if you need to return multiple values values) and the result code. If the function performs an action and doesn't need to return data, I generally return "1" and Exit 0 on success and return "0" and Exit # on failure. This allows me to use
 Code:
If Function(data)
  'SUCCESS!' @CRLF
Else
  'Error in function - ' @SERROR @CRLF
EndIf
As for item "C" - if you don't use $Rc = func(), the regurn codes just fall out all over the screen, leaving a trail of numbers. It isn't required, but is good practice.

As for debugging messages, see my fMsg() function. Declare the following:
 Code:
Global $DEBUG
$DEBUG = 1 ; set to non-zero when debugging
fMsg('this is an always visible message', '', 0, 0)
fMsg('This is a debug message...', '', 0, 4)
The first message is always output, while the second message is displayed only when $DEBUG is non-zero. You can even define various levels of debugging, so if DEBUG is 1, you get some messages, DEBUG=2 you get more, and DEBUG=3 you get a lot.

By using fMsg() you can sprinkle debug messages throughout your application and turn them on/off by simply changing the Global $DEBUG value rather than searching your code and removing or commenting out message lines.
_________________________
Actually I am a Rocket Scientist! \:D

Top
#214209 - 2023-02-23 01:14 PM Re: (NA) Kixtart can't create empty data in registry [Re: Glenn Barnas]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
Do you get your skills at Kixtart from being a Rocket Scientist! You seem to know so much.

It probably is because I don't do coding for a living - my main job - I almost never do coding. I only use it for Kixtart, but I am still amazed how after nearly 20 years of using Kixtart, I am still learning things! But then again, all my "coding skills" has been self taught - never did a formal course.

I will review your points on parenthesis, however I will balance that with making the script as readable as possible. For example it is not strictly necessary to do this:

 Code:
If abc = 5
     Indent this line of code
Else
     Indent this line of code
Endif


but so much easier to read than this when you have lots of code between the IF/Else/Endif statements or when you have multiple if/else statements within other If/Else statements:

 Code:
If abc = 5
No Indent this line of code
Else
No Indent this line of code
Endif


I probably could remove the "registry value added" output as that was put in years ago when I was configuring dozens of registry entries and it helped to ensure that all registry values were actually being set. Didn't think to use it this time as I assumed the problem was Kixtart not being able to declare a null data value! Ironic. I like the idea of your debugging code.

When I have time, I will tweak my coding accordingly.

Top
#214210 - 2023-02-23 02:42 PM Re: (NA) Kixtart can't create empty data in registry [Re: Robdutoit]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
No - the skills come from writing Kix code every day to production level standards that we use for all of our development in Kix, C++, Python, and other languages. About 70% of our production / commercial code is written in Kix because it has no endpoint dependencies, produces tiny code, and is trivial to self-update via the web. Our Kix UDF library now contains over 750 discrete functions. We run Kix-based Windows Services, make API calls to multiple platforms to perform all manner of automated processing and endpoint monitoring.

I have an "Intro to Programming" course I used to teach at the local college adult-ed division. It featured Kix, but concepts apply to most languages. You can download it from https://www.barnas.us/programming where you will also find KGen (script link-editor), the KGen user guide, and my recent Kix UDF library.

Using parenthesis is not related to indenting code blocks, for which we always use 2 spaces per level as full tabs look horrible in complex code. See any of my posted UDFs for examples.

Your original function should use Exit 0 on success and Exit 1 on failure. You can use other codes to exit when appropriate, such as "2" for "not found". I would also use $FnName = 1 on success and $FnName = 0 on failure ($FnName is the actual name of your function). This allows you to easily check the response with
 Code:
If Not MyFunc($Var)
  'Function failed: ' @SERROR @CRLF
  ; possible recovery or second attempt
EndIf
This is where you would put your messages, rather than in the function. The function tells your main code if it was successful, and if not, the Error code tells it why it failed. We often use negative return codees (Exit -1) to report custom errors.
_________________________
Actually I am a Rocket Scientist! \:D

Top
#214211 - 2023-02-24 11:59 AM Re: (NA) Kixtart can't create empty data in registry [Re: Glenn Barnas]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
I must admit using two spaces does look better than using tabs. Somehow looks tidier and more compact. Interesting.

I have put the @error coding onto my list of things to do. I like the idea. Thank you.

Top
#214212 - 2023-02-24 04:06 PM Re: (NA) Kixtart can't create empty data in registry [Re: Robdutoit]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
FYI - I reread your post and then mine and I see some ambiguity to my original statement. That may have caused you to misinterpret the proper action.

In item C) - your code was spilling the return code - 0 in that case - directly onto the screen and I suggested prefixing the command with "$Rc = " to catch the return code. There should not be a zero in front of that. The proper syntax is:
 Code:
$Rc = Function(args)
The $Rc contains the Return Code (if any). You can examine it if you want to, or simply ignore it and possibly check the @ERROR macro for a non-zero value. The benefit of this is that the return codes don't litter the screen with "random" numbers.

$Rc is one of those variables that helps keep output clean, has a short "lifespan", and might be used whether required or not. When you use $Rc to capture the result of a function, it means you either don't care about the value - you just don't want to be a litterbug, or you just want to check the status, such as Pass/Fail. When a function returns DATA that you will use later, then directly define an appropriate variable to use the result.
 Code:
$Value = ReadValue('hklm\path', 'Value')
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 393 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.035 seconds in which 0.012 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org