NTDOCAdministrator
(KiX Master)
2006-03-13 09:43 AM
Help converting VBS code to KiXtat

Seem to keep having trouble converting this to working KiXtart code.

Can someone convert it to working KiXtart code please.

I keep getting an error passing the flag in the last array when I set it in parameter.
 
Code:
Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
strComputer = "atl-win2k-01"
Set objUser = GetObject("WinNT:// " & strComputer & "/kenmyer ")
objUserFlags = objUser.Get("UserFlags")
objPasswordExpirationFlag = objUserFlags OR ADS_UF_DONT_EXPIRE_PASSWD
objUser.Put "userFlags", objPasswordExpirationFlag
objUser.SetInfo


 


LonkeroAdministrator
(KiX Master Guru)
2006-03-13 09:57 AM
Re: Help converting VBS code to KiXtat

Code:

$ADS_UF_DONT_EXPIRE_PASSWD = &10000
$strComputer = "atl-win2k-01"
$objUser = GetObject("WinNT:// " + $strComputer + "/kenmyer")
$objUserFlags = $objUser.Get("UserFlags")
$objPasswordExpirationFlag = $objUserFlags | $ADS_UF_DONT_EXPIRE_PASSWD
$objUser.Put("userFlags", $objPasswordExpirationFlag)
$objUser.SetInfo



I think...


NTDOCAdministrator
(KiX Master)
2006-03-13 10:10 AM
Re: Help converting VBS code to KiXtat

Nope, thanks for trying but same error I get.

ERROR : expected ')'!
Script: C:\A\AD\test.kix
Line : 7


This is line 7
$objUser.SetInfo

But I think Line 6 is the one really causing the error.


NTDOCAdministrator
(KiX Master)
2006-03-13 10:13 AM
Re: Help converting VBS code to KiXtat

Well late here and I have work in the morning. I'll check back in the morning.

Thanks again.


LonkeroAdministrator
(KiX Master Guru)
2006-03-13 10:16 AM
Re: Help converting VBS code to KiXtat

check out the reference for the put() function.
I doubt it supports what we are throwing at it.


Benny69
(MM club member)
2006-03-13 01:59 PM
Re: Help converting VBS code to KiXtat

doc give this a try:
Code:

$objPasswordExpirationFlag = $objUserFlags + $ADS_UF_DONT_EXPIRE_PASSWD



Richard H.Administrator
(KiX Supporter)
2006-03-13 02:36 PM
Re: Help converting VBS code to KiXtat

Quote:

doc give this a try:
Code:

$objPasswordExpirationFlag = $objUserFlags + $ADS_UF_DONT_EXPIRE_PASSWD






No Doc, don't try that. Stick with the bitwise "|", it's fine.


ShawnAdministrator
(KiX Supporter)
2006-03-13 02:46 PM
Re: Help converting VBS code to KiXtat

Doc, you got an extra space in your WinNT string and its screwing up the WinNT path:

$objUser = GetObject("WinNT:// " + $strComputer + "/kenmyer")


NTDOCAdministrator
(KiX Master)
2006-03-13 06:41 PM
Re: Help converting VBS code to KiXtat

Thanks Jooel for the bitwise code and thanks Shawn for the space in the name, that was the final key.

Code now works as suggested by Jooel and removing the space as suggested by Shawn.

Thanks again guys.


NTDOCAdministrator
(KiX Master)
2006-03-13 07:20 PM
Re: Help converting VBS code to KiXtat

Quote:

Quote:

doc give this a try:
Code:

$objPasswordExpirationFlag = $objUserFlags + $ADS_UF_DONT_EXPIRE_PASSWD






No Doc, don't try that. Stick with the bitwise "|", it's fine.




Richard, what's up with that? Benny's code worked fine too.


Howard Bullock
(KiX Supporter)
2006-03-13 07:25 PM
Re: Help converting VBS code to KiXtat

If the bit was already set then adding would change other bits as the two values would be summed. You only want to adjust the single bit.

NTDOCAdministrator
(KiX Master)
2006-03-13 07:30 PM
Re: Help converting VBS code to KiXtat

Thanks for the reply Howard. I understand what you mean.

Wasn't sure why exactly as I didn't spend the time to work it out that far, but I was thinking concatenation and not bitwise as to why it was or would be wrong.


yeah it toggles it when you use the + and either enables or leaves it alone with the |

Thanks Howard.


Les
(KiX Master)
2006-03-13 09:15 PM
Re: Help converting VBS code to KiXtat

It may "toggle" it, but the carry bit would also affect the adjacent bit. I thought you were 01 of the 10 types of people that understood binary?

NTDOCAdministrator
(KiX Master)
2006-03-13 09:27 PM
Re: Help converting VBS code to KiXtat

LOL, well you see I removed that signature already

Les
(KiX Master)
2006-03-13 11:42 PM
Re: Help converting VBS code to KiXtat

No, I don't see. I don't show sigs and avatars. My pages load quicker and there is less scrolling to do. Not to mention how annoying some of them are.

Richard H.Administrator
(KiX Supporter)
2006-03-14 10:07 AM
Re: Help converting VBS code to KiXtat

Benny asked the same question, so a quick explanation for anyone else who comes across this thread and who is not binary-flag-aware.

It is common practice to store similar flags as bits (on or off) in a single datum.

Using an eight bit field as an example, you have eight possible flags. Their positions and values are:
Code:
76543210
--------
00000001 - 1
00000010 - 2
00000100 - 4
00001000 - 8
00010000 - 16
00100000 - 32
01000000 - 64
10000000 - 128



You can explicitly set the flags in the field just by adding the values together, so to set the flags at positions 1 and 6 you would use 2+64 = 66. However this also sets all the other flags to zero which may not be what you want.

If you use the bitwise OR which is "|" in KiXtart, then only the flags that you specify will be set - the other flags will be left unchanged.

You cannot simply add the flag values to the existing value. If you do this then (as Les says) the flag will "carry" to the next higher position. This is easy to see with an example.

Say you try to set flag 1 four times using addition. This is what would happen:
Code:

76543210
--------
00000000 - Start, add 2 (the value of flag 1)
00000010 - SUCCESS! flag #1 is set. Now add it again.
00000100 - Oh-oh! The flag is unset, and worse flag #2 is set.
00000110 - Success! We've set flag #1 again
00001000 - Oh-oh! Flag#1 is unset and so is flag #2, but now flag #3 is set!



As you can see, it's all turned into a horrible mess.

Even when you are setting bit-fields explicitly it is good programming practice to only use bitwise operators to manipulate them. Not only does this serve as a reminder of what you are changing, but it helps to avoid costly hard to find errors if the field use should later change.

To complete the story, you can safely use simple addition and subtraction to change bit settings, but only if you check the setting first:
Code:

; To set the flag:
If Not ($objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD)
$objUserFlags=$objUserFlags + $ADS_UF_DONT_EXPIRE_PASSWD
EndIf

; To clear a flag
If $objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD
$objUserFlags=$objUserFlags - $ADS_UF_DONT_EXPIRE_PASSWD
EndIf



NTDOCAdministrator
(KiX Master)
2006-03-14 10:14 AM
Re: Help converting VBS code to KiXtat

Thanks a lot for the explanation there Richard.

LonkeroAdministrator
(KiX Master Guru)
2006-03-14 12:32 PM
Re: Help converting VBS code to KiXtat

I liked the explanation so much that I had to cast my rating in http://www.kixtart.org/ubbthreads/showprofile.php?Cat=0&User=362

Howard Bullock
(KiX Supporter)
2006-03-14 01:14 PM
Re: Help converting VBS code to KiXtat

Richard, I think it would be better to be consistent and use the bit-wise NOT (~) operator when turning off a flag instead of subtracting.
Code:
; To clear a flag:
If $objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD
$objUserFlags=$objUserFlags & ~$ADS_UF_DONT_EXPIRE_PASSWD
EndIf



Code:
; To set the flag:
If Not ($objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD)
$objUserFlags=$objUserFlags | $ADS_UF_DONT_EXPIRE_PASSWD
EndIf



Richard H.Administrator
(KiX Supporter)
2006-03-14 01:31 PM
Re: Help converting VBS code to KiXtat

Quote:

Richard, I think it would be better to be consistent and use the bit-wise NOT (~) operator when turning off a flag instead of subtracting.




Yes, the last examples were demonstrations of how to manipulate the bits using simple math instead of bitwise operations, they are not how I would recommend that the operations are performed.

Using the bitwise operations means that you do not need the conditionals at all.

I hope that clarifies things.


Howard Bullock
(KiX Supporter)
2006-03-14 02:07 PM
Re: Help converting VBS code to KiXtat

Oh, how does one adequately check to see if a bit is toggled on in oredr to use simple math to turn a flag on or off? You use bitwise oparations. So why use bitwise operators just to then use addition and subtraction?

So to simplify:

Code:
; To check a flag a flag:
If $objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD
"Flag is turned ON" ?
Else
"Flag is turned OFF" ?
EndIf



When using bitwise operations it really is necessary to check if a bit is toggled or not. You can just set it or turn it off without regard for its previous state unless your program flow is dependent on the bit value.


Code:
;To clear a flag
$objUserFlags=$objUserFlags & ~$ADS_UF_DONT_EXPIRE_PASSWD



Code:
;to set a flag
$objUserFlags=$objUserFlags | $ADS_UF_DONT_EXPIRE_PASSWD



Richard H.Administrator
(KiX Supporter)
2006-03-14 05:10 PM
Re: Help converting VBS code to KiXtat

Quote:

Oh, how does one adequately check to see if a bit is toggled on in oredr to use simple math to turn a flag on or off? You use bitwise oparations. So why use bitwise operators just to then use addition and subtraction?




Errr yes If you read back you'll see that I don't at any point advocate using simple maths instead of bitwise operations, in fact I stress that it's a bad idea.

The simple maths examples are there to:
  1. To complete the story for those people who may not get complex bitwise operations but can readily understand bitwise AND and ORs as conditional tests.
  2. To give an alternative for anyone who is revision locked - bitwise NOT is only available with recent versions of KiXtart
  3. To demonstrate a KiXtart version of the technique of (re)setting bits which for some reason appears to be commonly used in VB scripts.


Les
(KiX Master)
2006-03-14 05:27 PM
Re: Help converting VBS code to KiXtat

d. To demonstrate how NOT to do it.

Howard Bullock
(KiX Supporter)
2006-03-14 05:43 PM
Re: Help converting VBS code to KiXtat

This was not a dig. It was only to make a point for the novice readers.

NTDOCAdministrator
(KiX Master)
2006-03-14 06:56 PM
Re: Help converting VBS code to KiXtat

Well though not new, I have not studied or used that type of math in decades on my own, so I appreciate the input and feedback from both Howard and Richard.

Thanks for the lesson and information guys.


Richard H.Administrator
(KiX Supporter)
2006-03-15 10:24 AM
Re: Help converting VBS code to KiXtat

I had a bit of a eureka moment about 03:00 this morning, which leads me to be less adamant about using only bitwise operators to reset bits.

The problem is that scripting languages like KiXtart are "loosely typed". In particular, KiXtart uses sub-types of variant for all it's basic data types.

This is especially an issue when using constructs like the bitwise NOT "~". A bitwise NOT reverses the truth of each bit in the parameter. However, the size of the result is determined by the parameter.

The results of ~0 could be 255, 65535, 4294967295 or 18446744073709551615 depending on whether the "0" is interpreted as (using Windows terminology) an 8 bit byte, a 16 bit word a 32 bit dword or a 64 bit qword.

In KiXtart it is currently simple and everything will be treated as a dword. If you need a byte or word result you have to explicitly cap it, for example "&FF & ~0" for a byte and "&FFFF & ~0" for a word.

In other loosely typed languages small values might be interpreted as a byte or a word which could lead to truncation.

For example, if you wanted to reset flag #5 (value 32) you might do:
Code:
$iFlags=3112
$iFlags & ~32 ?



In KiXtart this will work, and $iFlags will be set to 3080.

But what if you are using a loosely typed language which interprets the 32 as being a byte? You can simulate this in KiXtart:
Code:
$iFlags=3112
$iFlags & ( ~32 & &FF) ?



The result is 8, which is clearly wrong.

The same problem will occur when small numbers are interpreted as being 16 bit integers.

If you know the size of the data that you are attempting to reset the bit in you can explicitly convert the value to the correct type using casts or functions, for example "$iFlags & ~CDbl(32)"

Using a conditional test and subtraction (or XOR) to reset the bit is data size independant and might be preferable where the size of the data cannot be pre-determined or guaranteed.


Howard Bullock
(KiX Supporter)
2006-03-15 04:20 PM
Re: Help converting VBS code to KiXtat

Very good post Richard. This is something I had not considered to date.

JochenAdministrator
(KiX Supporter)
2006-03-15 04:36 PM
Re: Help converting VBS code to KiXtat

just for the records...

http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=157126&an=0&page=0#157126

Same question asked, solved, but not with that complete explanation


Les
(KiX Master)
2006-03-15 04:39 PM
Re: Help converting VBS code to KiXtat

Richard,
Please consider reassembling your words of wisdom into a FAQ and post it in the FAQ forum so more can benefit.


JochenAdministrator
(KiX Supporter)
2006-03-15 04:43 PM
Re: Help converting VBS code to KiXtat

I'd like to second that... Anyone to third it ?

LonkeroAdministrator
(KiX Master Guru)
2006-03-15 06:29 PM
Re: Help converting VBS code to KiXtat

I can third it.
although, it's not must as I already learned it.

just that it would be beneficial for others too.


Les
(KiX Master)
2006-03-15 06:31 PM
Re: Help converting VBS code to KiXtat

Motion carried

LonkeroAdministrator
(KiX Master Guru)
2006-03-15 06:50 PM
Re: Help converting VBS code to KiXtat

it will face the US senate and will be turned down as it introduces threats to national security

NTDOCAdministrator
(KiX Master)
2006-03-15 07:10 PM
Re: Help converting VBS code to KiXtat

ROFL - You have too much insight to the US Law making strategy there Jooel.

LonkeroAdministrator
(KiX Master Guru)
2006-03-15 07:13 PM
Re: Help converting VBS code to KiXtat

well, I watched the reality show called "banning the selling of or harbours to terrorists" and I liked it.
like les' sig says, stupidity is human right of every soul...