#158120 - 2006-03-06 10:09 PM
Re: ChangeVLKey() - Test Function
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11622
Loc: CA
|
Quote:
However, when the function is not expected to return data then it makes sense use the error status as the return value.
Well then I would assume in most cases that it would be 0
If that's the case then why do we really need to set it to anything? If it's not expected to return data then as long as one checks for @ERROR and does not find an error then it should have worked, otherwise regardless of what caused the error it would be captured in the EXIT @ERROR code.
Also in your example by using the NOT operator forces one to use KiXtart 4.50 or newer (which is okay )
But as I view it if no data is expected why would you check for data? That's like expecting to really see the Tooth Fairy when you know it's not there.
Code:
Function ChangeVLKey($VOL_PROD_KEY) Dim $obj, $objects, $result ;Key is without hyphens (ABCDEFGHIJKLMNOPQRSTUVWXY) $objects = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("win32_WindowsProductActivation") If Not @ERROR For Each $obj in $objects $result = $obj.SetProductKey($VOL_PROD_KEY) Next EndIf Exit Val('&' + Right(DecToHex(@ERROR), 4)) EndFunction
Here we simply exit with the ERROR if anything caused one during that processing which we didn't expect data.
I assume from your point of view that well we would see the error and know that something went wrong, but I'm thinking the opposite in that for a function that does not return data, I won't go looking, I'll only look for an ERROR
Regardless of outcome here I think it has been a good discussion on the subject.
|
Top
|
|
|
|
#158121 - 2006-03-06 10:29 PM
Re: ChangeVLKey() - Test Function
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
I think the point is that returning the errorlevel is most "kix-like". For example - WriteValue from the manual is:
Code:
$RC=WriteValue("EZReg\Test", "A MultiString variable", "Line 1|Line 2|Line 3 with a || in it|" "REG_MULTI_SZ") If @ERROR = 0 ? "Value written to the registry" Endif
WriteValue returns the errorlevel and sets the errorlevel. This keeps two types of coders happy - those that like to check @ERROR (you i think) and those that like to check the return value (me for example).
The only pain-in-the-ass thing is when you forget to capture the return value - but we all deal with that day-in day-out. In fact, when I get rc's dumped to the console its a good reminder (to me anyways) about something that should be checked and handled if issues.
|
Top
|
|
|
|
#158123 - 2006-03-07 09:06 AM
Re: ChangeVLKey() - Test Function
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1892
Loc: Hilversum, The Netherlands
|
I think this should cover it all I removed the Error parsing NTDOC suggested earlier because I want to keep the function as simple as can be. It returns an error code, ppl who use this function should be able to recover the error themselves.
Code:
Function ChangeVLKey($VOL_PROD_KEY) Dim $obj, $objects, $x ;Key is without hyphens (ABCDEFGHIJKLMNOPQRSTUVWXY) $objects = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("win32_WindowsProductActivation") If @Error <> 0 Exit @Error EndIf For Each $obj in $objects $x = $obj.SetProductKey($VOL_PROD_KEY) $ChangeVLKey = @error Next EndFunction
|
Top
|
|
|
|
#158125 - 2006-03-07 09:57 AM
Re: ChangeVLKey() - Test Function
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
If Exist()
That just doesn't work with 0 for success so he used positive logic for testing.
Returns: 0 File not found 1 File found
That is a very bad example to choose 
You've assumed that because the function is returning a 1 or a 0 that this is the same as returning an error state.
But is not is it? The 1 or 0 returned by Exist() is data and has nothing to do with the error condition.
This still fits into the simple rule set:
- If a function is designed to return data it should return FALSE, 0 or null on an error where that will not prejudice the purpose of the function.
- If a function has no explicit return value then it should return an error status where "0" is success.
Now the example you have chosen (Exist) is designed to explicitly return a value, so it matches rule 1. If an error occurs then 0 will be returned, which fits it's purpose and the way it is used in scripts.
AScan is a good example of a function which fits rule 1 but cannot return 0 on an error which is a bit of a design error IMO.
|
Top
|
|
|
|
#158127 - 2006-03-07 10:35 AM
Re: ChangeVLKey() - Test Function
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
I think I understand what you are saying. If I have it right then:
- If a function is designed to return data it should return FALSE, 0 or null on an error where that will not prejudice the purpose of the function.
- If a function has no explicit return value then it should also return 0 on error and non-zero on success so that it is consistent with rule 1.
I see the argument but it would be pretty counter-intuitive as it is the reverse of every other scripting or programming language I've ever used. I guess that's why it took me a while to see what you were getting at.
|
Top
|
|
|
|
#158128 - 2006-03-07 12:16 PM
Re: ChangeVLKey() - Test Function
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1892
Loc: Hilversum, The Netherlands
|
Quote:
That's okay, but you're missing an EXIT code when it's finished. All UDF code should have an Exit code and not just Exit either. This has been discussed in the past and shown that on some occassions it can lead to other problems if an Exit with value is not supplied.
I don't think that is the case in this scenario, it returns an error code at the end of the function. Thus the function is ended. I do not believe the function can "hang" in any case scenario however I'd like to hear it if you believe otherwise.
|
Top
|
|
|
|
#158131 - 2006-03-08 08:06 PM
Re: ChangeVLKey() - Test Function
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11622
Loc: CA
|
Code:
For Each $obj in $objects $x = $obj.SetProductKey($VOL_PROD_KEY) $ChangeVLKey = @error Next Exit 0
As I said though I'm sure the script as you have would probably run good on 99.9% of every run.
Personal opinion is that having the Exit 0 or @ERROR is just a bit better is all.
|
Top
|
|
|
|
#158132 - 2006-03-09 08:44 AM
Re: ChangeVLKey() - Test Function
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1892
Loc: Hilversum, The Netherlands
|
I agree with that, I've changed the first post and the UDF post to reflect your suggestion. Thanks
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 306 anonymous users online.
|
|
|