Checking for TPM readiness with WMI and VBSCRIPT - vbscript

If I am using this WMI method '.IsEnabled' should I be concerned with how I am handling the results in my if statement. If a method returns a bool value can I still use a Not or should I do something like
if myStatus <> 0 OR isTPMEnabled <> True then
Here is my code
function isTPMReadyToBeOwned(myTPMService)
dim myStatus, isTPMEnabled, isTPMActivated, isTPMOwnershipAllowed
myStatus = myTPMService.IsEnabled(isTPMEnabled)
if myStatus <> 0 or not(isTPMEnabled) then
oLogging.CreateEntry "TPM isn't enable and must be enabled and activated manually, errorcode " & Hex(myStatus), LogTypeWarning
isTPMReadyToBeOwned = False
exit Function
end If
myStatus = myTPMService.IsActivated(isTPMActivated)
If myStatus <> 0 or not(isTPMActivated) then
oLogging.CreateEntry "TPM isn't active and must be activated manually, errorcode " & Hex(myStatus), LogTypeWarning
isTPMReadyToBeOwned = False
exit Function
end If
myStatus = myTPMService.isOwnershipAllowed(isTPMOwnershipAllowed)
if myStatus <> 0 or not(isTPMOwnershipAllowed) then
oLogging.CreateEntry "TPM ownership is not allowed, errorcode " & Hex(myStatus), LogTypeWarning
isTPMReadyToBeOwned = False
exit Function
end If
isTPMReadyToBeOwned = True
end Function

Boolean expressions/variables shouldn't be compared to boolean literal, because it adds an extra level of complexity (operator and operand). So use Not isTPMEnabled. As Not is neither a function nor an array, don't use param list/index (); reserve () for cases of precedence override.
Update wrt comment:
() have (too) many functions in VBScript
parameter list () in function calls: x = f(y, z)
index (): a = SomeArray(4711)
precedence override: 2 + 3 * 4 = 14, (2 + 3) * 5 = 25
() in boolean expression should be of type 3 only.

Related

lua debug hook causing race condition?

I wanted to report some debug information for a parser I am writing in lua. I am using the debug hook facility for tracking but it seems like there is some form of race condition happening.
Here is my test code:
enters = 0
enters2 = 0
calls = 0
tailcalls = 0
returns = 0
lines = 0
other = 0
exits = 0
local function analyze(arg)
enters = enters + 1
enters2 = enters2 + 1
if arg == "call" then
calls = calls + 1
elseif arg == "tail call" then
tailcalls = tailcalls + 1
elseif arg == "return" then
returns = returns + 1
elseif arg == "line" then
lines = lines + 1
else
other = other + 1
end
exits = exits + 1
end
debug.sethook(analyze, "crl")
-- main code
print("enters = ", enters)
print("enters2 = ", enters2)
print("calls = ", calls)
print("tailcalls = ", tailcalls)
print("returns = ", returns)
print("lines = ", lines)
print("other = ", other)
print("exits = ", exits)
print("sum = ", calls + tailcalls + returns + lines + other)
and here is the result:
enters = 429988
enters2 = 429991
calls = 97433
tailcalls = 7199
returns = 97436
lines = 227931
other = 0
exits = 430009
sum = 430012
Why does none of this add up? I am running lua 5.4.2 on Ubuntu 20.04, no custom c libraries, no further messing with the debug library.
I found the problem...
The calls to the print function when printing the result also trigger the hook, which only affects the results that have not been printed yet.

How to implement Bit Flags in VBScript

Ok, two disclaimers,
I do not work in Vbscript but need to use it a little for an HMI project
I am looking at doing this because this application limits total "tags" or variables
So basically what I am trying to achieve is the following;
a unit8, foo, has the value 5 which is b00000101 now I would like to view this as 8 binary values a pseudocode way of doing this would be
IF foo AND b00000001 <> 0 THEN 'sudo read
.....
foo = foo OR b00000010 'sudo write 1
foo = foo AND b11111101 'sudo write 0
...ect
I think it will be something like
&foo AND &b00000001 <> 0
is this practical in VBScript again I am well aware this is not standard practice but in between tag limitations and HMI input limitations it might actually make the most sense in this application.
You could give something like this a try which uses Bitwise Operators to read and write the bitwise flags.
Function GetFlag(value, bit_num)
Dim bit_mask
If bit_num < 32 Then bit_mask = 2 ^ (bit_num - 1) Else bit_mask = "&H80000000"
GetFlag = CBool(value AND bit_mask)
End Function
Function SetFlag(value, bit_num, new_value)
Dim bit_mask
If bit_num < 32 Then bit_mask = 2 ^ (bit_num - 1) Else bit_mask = "&H80000000"
If new_value Then
SetFlag = value OR bit_mask
Else
bit_mask = NOT bit_mask
SetFlag = value AND bit_mask
End If
End Function
'Define constants to store the flags (maximum of 32)
Const FLAG_SUDOREAD = 1
Const FLAG_SUDOWRITEONE = 2
Const FLAG_SUDOWRITETWO = 3
'Set the flags
Dim options
options = SetFlag(options, FLAG_SUDOREAD, True)
options = SetFlag(options, FLAG_SUDOWRITEONE, False)
options = SetFlag(options, FLAG_SUDOWRITETWO, True)
'Read the flags
WScript.Echo "FLAG_SUDOREAD = " & GetFlag(options, FLAG_SUDOREAD)
WScript.Echo "FLAG_SUDOWRITEONE = " & GetFlag(options, FLAG_SUDOWRITEONE)
Output:
FLAG_SUDOREAD = True
FLAG_SUDOWRITEONE = False
Useful Links
Using Bitwise Operators In VB (Excellent resource for learning Bitwise operators and how to use them)

VBScript division decimal by integer returns extra remainder

I'm trying to validate my VBScript function that divides an input value by 1440. When I pass 43.56 as input parameter, it returns me the following result:
0.030250000000000002
while the correct answer is
0.03025
What should I do to make it work correctly both with evenly divided integers, like 1440 / 1440 = 1, and when one value is a decimal.
Currently my function looks like so:
Function convertMinutesToDays(value)
If IsNumeric(value) And TypeName(value) <> "Boolean" Then
convertMinutesToDays = value / 1440
Else
convertMinutesToDays = 0
End If
End Function
Actually, if you simply put Response.Write convertMinutesToDays(43.56), it will show 0.03025, but we are using it within an assert javascript method, like so:
Call AssertAreEqual(convertMinutesToDays(43.56), 0.03025, "convertMinutesToDays: pass 43.56 should return 0.03025")
The javascript code:
<script language="JavaScript" runat="server">
function AssertAreEqual(val1, val2, message)
{
var retVal = false;
var divAttributes = "";
var equality = "";
if (val1 === val2)
{
divAttributes = "class=\"unittest assertareequal pass\"";
equality = "=";
retVal = true;
}
else
{
divAttributes = "class=\"unittest assertareequal fail\"";
equality = "!=";
retVal = false;
}
Response.Write("<div " + divAttributes + ">Actual:" + val1 + " " + equality + " " + val2 + ":Expected | " + message + "</div>");
return retVal;
}
</script>
The output:
Actual:0.030250000000000002 != 0.03025:Expected | convertMinutesToDays: pass 43.56 should return 0.03025
Any ideas?
This is due to the way floating point math works in VBScript(well most any language really). For your code, the Round function should suffice because you are always dividing by a single number so rounding errors won't add up on you the way they can in other types of functions. I chose 5 decimal places because it fixes your example. If you find others that still are off then you may need to up that but for representing partial days 5 is probably enough.
Function convertMinutesToDays(value)
If IsNumeric(value) And TypeName(value) <> "Boolean" Then
convertMinutesToDays = Round(value / 1440, 5)
Else
convertMinutesToDays = 0
End If
End Function

Need to pass object and operation in a function that executes it

I need to pass an object and its operation in a function so that each time I can call the function only and save me to write same steps for all the objects like validating the object before performing an operation. Similar way to a Register User Function in QTP/UFT.
However, Testcomplete doesn't have this feature (atleast under my knowledge, would be happy to know if there is)
This is my code that I am trying but unable to:
Call OpenPageorTab("Aliases.Admin.wndMain.toolStrip", ".Visible")
Function OpenPageorTab(obj, method)
'if obj.Exists then
execute a = obj&method
delay(1000)
OpenPageorTab = True
'Else
log.Error "Unable to find the object"
OpenPageorTab = False
'End if
using if condition as i was passing object earlier instead of string
It fails at "execute" statement and gives me VbScript runtime error when executing this statement.
my question is two fold -
How do I pass objects and its operation in a function and execute it
Also is it possible to pass an object it self instead of string for ex:
obtoolbar = "Aliases.Admin.wndMain.toolStrip"
Call OpenPageorTab(obtoolbar, ".Visible")
Appreciate any help or direction on this issue
EDIT 1
I am somewhere close to an answer however not accurately. I am able to pass the object as string - Check the code below
Call OpenPageorTab("Aliases.Admin.wndMain.toolStrip", ".Click")
Function OpenPageorTab(obj, method)
' if obj.Exists then
eobj = "" & obj & method
execute (eobj)
delay(1000)
OpenPageorTab = True
' Else
log.Error "Unable to find the object"
OpenPageorTab = False
' End if
End Function
However I still need to pass the object something like
Set oToolStrip = Aliases.Admin.wndMain.toolStrip
Call OpenPageorTab(oToolStrip, ".Click")
This is something that I'm unable to do.
EDIT 2
I have already got the answer to this problem and have posted the solution. That being said, is there any way that Function can be utilized as a method ?
Here an example of how to reference a function and pass parameteers to it, including objects.
Const forReading = 1, forWriting = 2, forAppending = 8, CreateFile = True
Set my_obj = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\temp\test.txt", forWriting, CreateFile)
Function my_function(my_obj, method, text)
command = "my_obj." & method & " """ & text & """"
ExecuteGlobal command
End Function
'make a reference to our function
Set proc = GetRef("my_function")
'and call it with parameters, the first being the method invoked
Call proc(my_obj, "WriteLine", "testing")
'cleanup'
my_obj.Close
Set my_obj = Nothing
I was able to finally formulate the solution the below function can work as makeshift register function in TestComplete
Sub test
'Set the Object
Set pToolStrip = Aliases.Admin.wndMain.toolStrip.Button("User Admin")
Call GenericOperationFunc(pToolStrip, ".Click", "N")
'if you want to perform an operation which return a value
b = GenericOperationFunc(Aliases.Admin.wndPopup.Child(2), ".Caption", "Y")
End Sub
Public Function GenericOperationFunc(obj, method, Return)
GenericOperationFunc = False
on error resume next
if obj.Exists then
if Ret = "Y" then
eobj = "NewText="&"obj" & method
execute (eobj)
GenericOperationFunc = NewText
Delay(500)
Else
eobj = "obj" & method
execute (eobj)
delay(1000)
GenericOperationFunc = True
End if
Else
log.Error "Unable to find the object"
GenericOperationFunc = False
End if
End Function
'log.error, delay, aliases, ptoolstrip(object) are testcomplete specific

how to return a object in a function that takes arguments visual basic 6

Hi im trying to write a function that returns an object but it gives me an arguments not optional error, this is my code
Public Function searchVehicle(c As String, v As Variant) As Collection
Dim qur As String
qur = "select * from [vehicle] where ( " & c & " like '%" & v & "%')"
Set mobjRst = conn.execQuery(qur)
Dim tmpV As Vehicle
Dim res As Collection
With mobjRst
Do Until .EOF
Set tmpV = New Vehicle
Call tmpV.popVehicle(!ID, !make, !model, !purchaseyear, !totalmilage, !milageafterservice, !servicemilage, !description)
res.Add (tmpV)
.MoveNext
Loop
End With
searchVehicle = res
End Function
My first thought is that since it's an object reference, you need to use Set to set the return value.
Set searchVehicle = res
It may be more helpful to know what line you're seeing the problem on.
As a side note, you may also want to take a look at when you need to use Call and when you don't: https://blogs.msdn.microsoft.com/ericlippert/2003/09/15/what-do-you-mean-cannot-use-parentheses/
Your problem lies in the following call to the function -
searchVehicle = res
You have specified searchVehicle to have a string (c) and a variant (v) combining a collection. This will error as you have set no values to either c or v and then call your function -
searchVehicle = (c, v) collection
give us some more information on how you call this etc in your button click event, what is returned...

Resources