No KiXtart.org Moderators or Administrators aside from myself are allowed to post in this thread.
(This is for those with less experience to learn, not for bored, experienced users to bump their post count)
(If you're able to easily do this task then please don't participate with any solutions)
(If the task is too easy then I'll increase the difficulty level next time)
No using IM,PM or Google to locate/find an answer (you're on the honor system here).
For this lesson please don't use the UDF forum. The only help you're allowed is the KiXtart User Manual
All submitted code must be as though it were to be used in production. ie.. code should set options and have error checks as well as documentation of what the code is doing. Error checking does not need to be extensive but should have some basic check.
Code may be submitted as soon as you're ready.
Objective: Modify the 3rd binary value of the supplied regsitry modification to "bc"
Please download this Registry export which will create a new key and value in the Registry located here: HKEY_CURRENT_USER\Software\KiXtart-Learning Round02
; For use in creating an array to allow for changes to said array $Element = 0
; Read in the Value we want to work with $ValRead = ReadValue("HKCU\Software\KiXtart-Learning","Round02")
; Build an array with each element = to a binary group For $Count = 1 to Len($ValRead) step 2 redim preserve $Valarray[$Element] $ValArray[$Element] = SubStr($ValRead,$Count,2) $Element = $Element + 1 Next
;Now we can change the element we want $Valarray[2] = "bc"
;And now to put it all back together so that we can write it For $Count = 0 to Ubound($Valarray) $ValWrite = $Valwrite + $ValArray[$Count] Next
;Finally we write it back to the registry $SO = Writevalue("HKCU\Software\KiXtart-Learning","Round02",$ValWrite,"Reg_Binary")
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
And as a function with error checking... Code:
;************************************************************************* ; Script Name: Learning Round 2 ; Author: Gargoyle ; Date: 9/25/2006 ; Description: Change a binary string in the registry ;*************************************************************************
;Error Codes ; 0 = Change has been made ; 3 = Change String to long ; 4 = Change String not in range (0 - f) ; 5 = Unable to read Value ; 6 = Unable to write value ; 7 = Invalid Position
; Required values ; $Key = The Registry key to read ; $Entry = The Registry entry to read ; $Position = Which Binary position do you want to change ; $Changeto = The value to change to
;Check to make sure the ChangeTo is acceptable If Len($ChangeTo) > 2 Exit 3 EndIf
For $ = 1 to 2 If Ascan($ValCheck,SubStr($Changeto,$,1)) = -1 Exit 4 EndIf Next
; For use in creating an array to allow for changes to said array $Element = 0
; Read in the Value we want to work with $ValRead = ReadValue($Key, $Entry3D"#000000" size=2>)
If @Error = 0
; Build an array with each element = to a binary group For $Count = 1 to Len($ValRead) step 2 redim preserve $Valarray[$Element] $ValArray[$Element] = SubStr($ValRead,$Count,2) $Element = $Element + 1 Next
;Now we can change the element we want If $Position > Ubound($Valarray) Exit 7 Else $Valarray[($Position - 1)] = $ChangeTo EndIf
;And now to put it all back together so that we can write it For $Count = 0 to Ubound($Valarray) $ValWrite = $Valwrite + $ValArray[$Count] Next
;Finally we write it back to the registry If Writevalue($Key,$Entry,$ValWrite,"Reg_Binary") = 0
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Once I had the first one, the second was easy. Had a heck of time figuring out how to break up the ReadValue string though.
I have watched you guys enough to start picking this stuff up, but since 90% of the scripting I do is Admin based, this is stuff I hardly ever get into (that and I am a networking guy and hardly ever touch the servers)
Nothing wrong overall with using a single $ for a var, but again if this was in use in a 1K - 2K lines of script using a single $ could be a nightmare for someone to manage that was not the orignal author of the code.
Again (imho) one should use a unique var for such a case or a meaningful name.
For use though in small scripts it is fine with me too.
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
The lone $ was supposed to be changed to $Count before posting but I forgot to. As for the complexity, this one made me think a bit. So if you do decide to rachet it up another notch, just don't make it to big.
Yes, and I agreed that for use here it was fine, but in a larger script it could easily lead to problems such as scope issues and or difficulty in others being able to manage the code.
The point being that one has to be aware and not just blindly use it because they saw other scripters use it.
Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
I'm trying not to! I'm currently stuck at the same point I always get stuck. will try to post something working in a while. (don't get your hopes up tho ) grrr. how can this be so tricky? wait... I am not to add the same value again... grrr, back to the drawingboard - need to learn how to read.. Now, just dug my own grave. I'll post in a week or so when I'll start to think somewhat normal again...
;call registry binary data change function ;AlterBinRegVal(< key >,< value >,< byte to be altered >,< new byte data >) $Return = AlterBinRegVal('HKCU\Software\KiXtart-Learning','Round02',$Byte_To_Be_Altered,$NewByteData) If Not($Return=0) ;do somthing with error code EndIf
Function AlterBinRegVal($Key,$Value,$Byte,$Data)
;region Setup Variables Dim $so,$Result,$AlteredResult,$Return ;endregion
;read registry value $Result = ReadValue($Key,$Value) ;check for reading error, if so exit function If @ERROR Exit @ERROR EndIf
;create new binary value by adding: ;'bytes Left of byte to be altered' + 'new data byte' + 'bytes Right of byte to be altered' $AlteredResult = Left($Result,$Byte*2-2) + $Data + Right($Result,Len($Result)-$Byte*2)
;write altered value back to registry $Return = WriteValue($Key,$Value,$AlteredResult,'Reg_Binary') ;check for writing error, if so exit function If @ERROR Exit @ERROR EndIf