Weird behaviour with QueryPerformanceCounter

  • Thread starter Thread starter Bob Nicholls
  • Start date Start date
B

Bob Nicholls

I am noticing something weird with the value returned from
QueryPerformanceCounter. I have declared the function as:

<DllImport("coredll.dll", EntryPoint:="QueryPerformanceCounter")> _

Public Shared Function QueryPerformanceCounter(ByRef perfCounter As Int64)
As Integer

End Function


and am calling it thus:
Dim lNow As Int64

If QueryPerformanceCounter(lNow) = 0 Then

Throw New ApplicationException

End If


The strange thing is that lNow seems to wrap at 2^32 even though the
parameter is declared as Int64. Is this to be expected if so I am not sure
how it should be used.

Thanks for any help.

Bob
 
The argument passed should be an Int32, not an Int64 which explains the
unusual behaviour with this declaration.

Peter
 
Thanks Peter. So it is not possible to use this function for timing anything
longer than a few seconds?

Bob
 
The argument passed should be an Int32, not an Int64 which explains the
unusual behaviour with this declaration.

Could you please explain why one should use an Int32 instead of Int64 ? The
MSDN clearly states that both functions QueryPerformanceCounter() and
QueryPerformanceFrequency() take a pointer to an LARGE_INTEGER as argument.
LARGE_INTEGER is defined as LONGLONG. So it should work fine with an Int64,
shouldn't it ?

Btw, both functions are working without problems when using an "ref Int64"
in the declaration ... at least on the devices we're using (Windows CE 4.1,
..NET CF SP2).

Greetings, Christian
 
Actually sorry that's my mistake, I was looking at an existing piece of code
for this function I had seen which it turns out is wrong, the parameter is
indeed an Int64.

The actual implementation of this counter is dependant on the hardware
device it is called on. So some hardware may behave differently.

Peter
 
Scrub that last post - it should indeed accept an Int64.

What device are you seeing this behaviour on?

Peter
 
Back
Top