WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Brute Force Way to Convert an int64 that WinBatch Can Handle

 Keywords: 64 64-bit bit int integer QueryUnbiasedInterruptTime bitwise floating point number

Question:

I found an interesting API that is valid on Windows 7 or newer called: QueryUnbiasedInterruptTime. So I thought I would attempt to DllCall this function. However it seems to return a 64-bit integer. Not quite sure how to handle this value since WinBatch does currently support 64 bit integers.

Here is what I came up with:

 
;BOOL QueryUnbiasedInterruptTime(
;  _Out_  PULONGLONG UnbiasedTime
;);
 
If WinVersion( 5 ) < '2-6-1'
Pause('Notice','Not Supported on this Platform')
Exit
EndIf
 
hDll = DllLoad(DirWindows(1):"kernel32.dll")  
lpUnbiasedTime = BinaryAlloc(8)
 
rslt = DllCall( hDll, long:'QueryUnbiasedInterruptTime',lpBinary:lpUnbiasedTime )
time = BinaryPeekFlt( lpUnbiasedTime, 0 ) ; Peeks a 64 bit floating point number.
 
Pause('Unbiased interrupt-time count in system time units of 100 nanoseconds.',time)
 
BinaryFree( lpUnbiasedTime )
Exit

Answer:

Integers and floating point numbers have completely different binary representation. You cannot just pull a the binary representation of an integer out of a buffer and expect it to be a floating point number. You have to do bit wise manipulation (or ugly string conversions) to get the bits in the right order for floating point. so here is a quick-and-dirty, crude, brute force way to convert a an _int64 to something WinBatch can handle. The result are not always correct but it will work most of the time
hUnbiasedTime = BinaryAlloc(8)
 
nResult = DllCall(DirWindows(1):"kernel32.dll", long:'QueryUnbiasedInterruptTime',lpBinary:hUnbiasedTime )
BinaryEODSet(hUnbiasedTime, 8)
nlower = BinaryPeek4(hUnbiasedTime, 0 )  
nupper = BinaryPeek4(hUnbiasedTime, 4 ) 

; Not likely to ever be negative but for completeness's sake.
if nupper < 0 then nupper = (nupper & 2147483647) *  2.0 
nupper = nupper * 4294967296.0 

; This on the other hand can often be negative.
if nlower < 0 then nlower = (nlower & 2147483647) * 2.0 

; Total and convert to seconds
nUptime = nupper + nlower
nUptime = nUptime/10000000.0

Pause('System Up Time',nUpTime:' sec.')
 
BinaryFree( hUnbiasedTime )
Exit

Article ID:   W17907
Filename:   Brute Force Way to Convert an int64 that WinBatch Can Handle.txt
File Created: 2013:07:02:08:06:26
Last Updated: 2013:07:02:08:06:26