Page 1 of 2 12>
Topic Options
#168428 - 2006-09-26 12:02 AM KiXtart Learning Series - Lesson 02
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
KiXtart Learning Series - Round 02


    RULES
  • 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

Download round02.reg

Code:
Windows Registry Editor Version 5.00
 
[HKEY_CURRENT_USER\Software\KiXtart-Learning]
"Round02"=hex: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
 




Previous Learning Series
9/21/2006
KiXtart Learning Series - Round 01
http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=168455

 

Top
#168429 - 2006-09-26 02:57 AM Re: KiXtart Learning Series - Round 02
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Code:

;*************************************************************************
; Script Name: Learning Round 2
; Author: Gargoyle
; Date: 9/25/2006
; Description: Change a binary string in the registry
;*************************************************************************

Dim $SO,$ValRead,$Count,$Element,$valarray[],$ValWrite

;Script Options
$SO=SETOPTION("Explicit", "ON")
$SO=SETOPTION("NoMacrosInStrings", "ON")
$SO=SETOPTION("NoVarsInStrings", "ON")
$SO=SETOPTION("WrapAtEOL", "ON")

If NOT @LOGONMODE
    Break On
EndIf

; 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")

?
$valRead
?$ValWrite


Top
#168430 - 2006-09-26 03:58 AM Re: KiXtart Learning Series - Round 02
Gargoyle Offline
MM club member
*****

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

Dim $SO

;Script Options
$SO=SETOPTION("Explicit", "ON")
$SO=SETOPTION("NoMacrosInStrings", "ON")
$SO=SETOPTION("NoVarsInStrings", "ON")
$SO=SETOPTION("WrapAtEOL", "ON")

If NOT @LOGONMODE
    Break On
EndIf

ChangeBinary("HKCU\Software\KiXtart-Learning","Round02",3,"bc")

Function ChangeBinary($Key,$Entry,$Position,$ChangeTo)

Dim $ValRead,$Count,$Element,$valarray[],$ValWrite,$ValCheck[],$

$ValCheck = "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"

;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
   
   
Else
        Exit 6
   
EndIf

Else
    Exit 5
EndIf


EndFunction
? @error


Top
#168431 - 2006-09-26 03:59 AM Re: KiXtart Learning Series - Round 02
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Very nice Gargoyle.

Are you sure you came up with that solution though just on your own and with the user manual?

If so, then I think you're a long way from noobie status on KiXtart.

I would not expect anyone that was new, semi-new to KiXtart to be able to easily have coded that.

Good job, thanks for participating.

Top
#168432 - 2006-09-26 04:05 AM Re: KiXtart Learning Series - Round 02
Gargoyle Offline
MM club member
*****

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)

Top
#168433 - 2006-09-26 04:05 AM Re: KiXtart Learning Series - Round 02
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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.

Top
#168434 - 2006-09-26 04:13 AM Re: KiXtart Learning Series - Round 02
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
So you trying to say that I should ratchet up the complexity of the series now?

Anyone out there lurking that is learning from this or is this just an open forum for me, Garg and Bjorn?

Top
#168435 - 2006-09-26 04:44 AM Re: KiXtart Learning Series - Round 02
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
just a point... (sorry for dropping into this thread I'm not supposed to post in)

using the $ var makes sense in here, when it was used instead of the good old $i.
but then, in the next loop he used $count.

for simplicity's sake, I would use either one, but not both.

Top
#168436 - 2006-09-26 04:58 AM Re: KiXtart Learning Series - Round 02
Gargoyle Offline
MM club member
*****

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.
Top
#168437 - 2006-09-26 06:12 AM Re: KiXtart Learning Series - Round 02
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
I love seeing these things!

By the way?

Code:

(˙˙˙˙&#9787;&#9829;<P_˙˙˙˛&#9829;&#9787;&#9827;&#9787;&#9830;



Did you sneeze on your keyboard?!?!

Top
#168438 - 2006-09-26 06:13 AM Re: KiXtart Learning Series - Round 02
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
hmm the board forums don't like some those ASCII chars it seems :P
Top
#168439 - 2006-09-26 06:57 AM Re: KiXtart Learning Series - Round 02
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
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.

Top
#168440 - 2006-09-26 08:03 AM Re: KiXtart Learning Series - Round 02
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Quote:

I love seeing these things!

By the way?

Code:

(˙˙˙˙☻♥



Did you sneeze on your keyboard?!?!




Must be the HTML tags that I am posting with. I don't see it.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#168441 - 2006-09-26 09:38 AM Re: KiXtart Learning Series - Round 02
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
lol. I've missed that there was a round#02, I'll get right to it (haven't peeked at gargs code yet ) And Doc, I'll give you some noob-code
Top
#168442 - 2006-09-26 10:24 AM Re: KiXtart Learning Series - Round 02
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Okay here is another similar method.

NO LOOKING Bjorn!!


;Declare variable and set options 
If Not @LogonMode
Break On
EndIf
Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')
$SO=SetOption('WrapAtEOL','On')

;Declare our variables
Dim $Key, $Value, $Count, $i, $UpdateValue
;Assign read value from the Registry to our variable
$Key = 'HKCU\Software\KiXtart-Learning'
$Value = ReadValue($Key,'Round02')
;Check if there is an error reading the value, if so quit and set error
If @ERROR Exit @ERROR EndIf
;Declare an array to use
Dim $Binary[(Len($Value)/2)-1]
;Set our counter to 0
$Count = 0
;Step through the read value and create the array
;We take 2 steps so that each element will be part of the 2 character binary value
For $i = 1 To Len($Value) Step 2
;We now assign 2 characters each time through the array
$Binary[$Count] = SubStr($Value,$i,2)
;We reset the counter before going back through the process again
$Count = $Count + 1
Next
;Here we set the specific element to a value we want to change
$Binary[2] = "bc"
;Now we write that value change to the registry using a Join since the $Binary
;var is an array format.
$UpdateValue = WriteValue($Key,'Round02',Join($Binary,""),REG_BINARY)
;Check if there is an error writing the value, if so quit and set error
If @ERROR Exit @ERROR EndIf



Top
#168443 - 2006-09-26 11:02 AM Re: KiXtart Learning Series - Round 02
Björn Offline
Korg Regular
*****

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...


Edited by Björn (2006-09-26 03:18 PM)

Top
#168444 - 2006-09-26 04:39 PM Re: KiXtart Learning Series - Round 02
PaulyT Offline
Fresh Scripter
*****

Registered: 2005-08-24
Posts: 8
Loc: Madtown Wisco
I had ended up with similar code to the example Doc posted with a slight difference in the For - Next.

Code:

For $ = 1 to Len($round02RegValue) step 2
$modifyValueArray[$ / 2] = SubStr($round02RegValue, $, 2)
Next



I peek at the forum every day or two. It would be nice for a day to go by before posting results and making it temping for a look see.

Top
#168445 - 2006-09-26 06:51 PM Re: KiXtart Learning Series - Round 02
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
There is more than one way to skin a cat, here is a method without the use of arrays:
Code:

;region Setup Variables
Dim $so,$Byte_To_Be_Altered,$NewByteData,$Return

$so=SetOption("Explicit", "ON")
$so=SetOption("NoMacrosInStrings", "ON")
$so=SetOption("NoVarsInStrings", "ON")
$so=SetOption("WrapAtEOL", "ON")
;endregion

;set byte to be altered
$Byte_To_Be_Altered = 3

;set new byte data
$NewByteData = 'bc'

;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

EndFunction



Edited by benny69 (2006-09-26 06:53 PM)
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#168446 - 2006-09-26 09:07 PM Re: KiXtart Learning Series - Round 02
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Nice Benny thanks for the input.

I do like this method that Witto has been using for the script header

Code:
If Not @LogonMode
Break On
EndIf


Top
#168447 - 2006-09-26 10:28 PM Re: KiXtart Learning Series - Round 02
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
to be noted. only useful for kix32.exe
Top
Page 1 of 2 12>


Moderator:  NTDOC, ShaneEP, Mart, Radimus, Glenn Barnas, Jochen, Allen 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.075 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

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