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")
;************************************************************************* ; 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
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.
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.
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
Agree about the delay - ain't got any time to do this stuff, too much going on at work and private right now.. I've got something working. will post at lunchtime (1,5h away)
ok, lunchtime and 2:nd post since my previous post seems to have failed Notice : I'm not really happy with this - but cannot give it more time nor fix it better... added: just noticed that my comments wasn't in this .. you'll have to live with it a few hours (removed those tags after my first post...) ADDED AGAIN ... Well, after peeking at the other codes, I finally got to the point of REALLY knowing what was to be done... buggr. well, I'll be happy if I recive a 'f' for this
Code:
;enjoy my totally wrong code: If NOT @LOGONMODE Break On EndIf
Dim $SO
$SO = SetOption("Explicit","On") $SO = SetOption("NoVarsInStrings","On") $SO = SetOption("NoMacrosInStrings","On") ; dim $counter,$key,$val dim $wr,$add,$wri dim $split[],$c,$x dim $sp[],$spl,$ ;define values $counter=0 $key='HKEY_CURRENT_USER\Software\Golfing' $val='Round02' $add='28,00,00,00,ff,ff,ff,ff,02,00,00,00,03,00,00,00,3c,00,00,00,50,\ 00,00,00,fe,ff,ff,ff,b2,03,00,00,02,05,00,00,02,04,00,00' $wri='28000000ffffffff02000000030000003c00000050000000feffffffb20300000205000002040000'
$wr=readvalue($key,$val) $split=split($add,',')
for each $c in $split select case $counter = 2 $c = 'bc' Redim preserve $sp[$counter] $sp[$counter]=$c case 1 if instr($c,'\') $x=substr($c, len($c) - 1, 3) $=trim($x) Redim preserve $sp[$counter] $sp[$counter]=$x else Redim preserve $sp[$counter] $sp[$counter]=$c endif endselect $counter=$counter+1 next
ReDim $counter $counter=0
for $counter = 0 to ubound($sp) $spl = $spl + $sp[$counter] next
/* $wr ' Read value from reg' ? $wri ' should be equal to line above' ? $spl ' this is what we wanna write' ? */ if $wr = $spl 'entry already altered' ? exit @error else $wr=writevalue($key,$val,$spl,'reg_binary') if @error 'error: ' + @ERROR @SERROR + ' ' ? else 'altered key' ? endif endif
If you haven't noticed yet - this doesn't do what it was meant to do ... and since this is the second time I cannot use my webbrowser and screwed up a third post / alterning of this post... gah, this is what I'll post and deliver, since this post I have peeked at others codes, and therefore I disqalify both my spelling and paricipation in this 'round'... well, rewrote it quickly - or, did my 'own' version of the 'real' thing... this is more or less fresh from the drawingboard - not something I would put in production... Code:
If NOT @LOGONMODE Break On EndIf
Dim $SO Dim $wro[],$apa,$count,$counter,$wr,$wri,$key,$val,$to,$o,$ $SO = SetOption("Explicit","On") $SO = SetOption("NoVarsInStrings","On") $SO = SetOption("NoMacrosInStrings","On") $key='HKEY_CURRENT_USER\Software\Golfing' $val='Round02' $count=0 $counter=0
$wri=readvalue($key,$val)
for $count=0 to 80 step 2 $o=right(ltrim(substr($wri, 1, $count)),2) if $count=6 $o='bc' endif Redim preserve $wro[$counter] $wro[$counter]=$o $counter=$counter+1 next Redim $counter $counter=0
for $counter = 0 to ubound($wro) $to= $to + $wro[$counter] next
This is what I finally came up with - no real errorchecking tho =/ And man, I've used things I've never touched before.. and the redim preserve, that took like ages to acctually figure out how to do it.
Code:
If NOT @LOGONMODE
Break On
EndIf
Dim $SO
$SO = SetOption("Explicit","On")
$SO = SetOption("NoVarsInStrings","On")
$SO = SetOption("NoMacrosInStrings","On")
;
dim $counter,$key,$val
dim $wr,$add,$wri
dim $split[],$c,$x
dim $sp[],$spl,$
;define values
$counter=0
$key='HKEY_CURRENT_USER\Software\Golfing'
$val='Round02'
$add='28,00,00,00,ff,ff,ff,ff,02,00,00,00,03,00,00,00,3c,00,00,00,50,\'+ '00,00,00,fe,ff,ff,ff,b2,03,00,00,02,05,00,00,02,04,00,00'
$wri='28000000ffffffff02000000030000003c00000050000000feffffffb203'+
'00000205000002040000'
$wr=readvalue($key,$val)
$split=split($add,',')
for each $c in $split
select
case $counter = 2 $c = 'bc' Redim preserve $sp[$counter] $sp[$counter]=$c
case 1 if instr($c,'\') $x=substr($c, len($c) - 1, 3)
$=trim($x) Redim preserve $sp[$counter]
$sp[$counter]=$x else Redim preserve $sp[$counter] $sp[$counter]=$c endif
endselect
$counter=$counter+1
next
ReDim $counter
$counter=0
for $counter = 0 to ubound($sp)
$spl = $spl + $sp[$counter]
next
/*
$wr ' Read value from reg' ?
$wri ' should be equal to line above' ?
$spl ' this is what we wanna write' ?
*/
if $wr = $spl 'entry already altered' ? exit @error
else
$wr=writevalue($key,$val,$spl,'reg_binary')
if @error 'error: ' + @ERROR @SERROR + ' ' ?
else
'altered key' ? endif endif
I just noticed that my fifth edit of this post has gone a-wall, and my final part that acctually did what it more or less was supposed to is gone.. Provided below:
Code:
If NOT @LOGONMODE
Break On
EndIf
Dim $SO
Dim $wro[],$apa,$count,$counter,$wr,$wri,$key,$val,$to,$o,$
$SO = SetOption("Explicit","On")
$SO = SetOption("NoVarsInStrings","On")
$SO = SetOption("NoMacrosInStrings","On")
$key='HKEY_CURRENT_USER\Software\Golfing'
$val='Round02'
$count=0
$counter=0
$wri=readvalue($key,$val)
for $count=0 to 80 step 2
$o=right(ltrim(substr($wri, 1, $count)),2)
if $count=6 $o='bc' endif
ReDim preserve $wro[$counter] $wro[$counter]=$o
$counter=$counter+1
next
ReDim $counter
$counter=0
for $counter = 0 to ubound($wro)
$to= $to + $wro[$counter]
next
;$to ?
;$wri ?
if $to = $wri 'entry already altered' ? exit @error
else
$wr=writevalue($key,$val,$to,'reg_binary')
if @error 'error: ' + @ERROR @SERROR + ' ' ?
else
'altered key' ? endif endif
This still lacks errorchecking - and uses the wrong key-structure.., but should be the code-snippet that you could comment. Interesting that you didn't notice that it didn't do the job correctly
Thanks for the input Doc, sorry for the long lines, but my postings were screwed up all the time, and all my alterings is totally gone.. just edited it again and if you just changed so I can alter I hope you can verify my second snippet.
I do have a question, but I will rewrite my second submission / perhaps my first as well edited more in the lines of the rules and the way it should be written. Note, this is only for my own, I need to get a grip on handeling this more correct as you might have noticed. Will also try to participate in lesson #3 and hopefully submit something this weekend.