manipulating LARGE_INTEGERS
Asked Answered
F

6

14

I am converting some code from C to C++ in MS dev studio under win32. In the old code I was doing some high speed timings using QueryPerformanceCounter() and did a few manipulations on the __int64 values obtained, in particular a minus and a divide. But now under C++ I am forced to use LARGE_INTEGER because that's what QueryPerformanceCounter() returns. But now on the lines where I try and do some simple maths on the values I get an error:

error C2676: binary '-' : 'LARGE_INTEGER' does not define this operator or a conversion to a type acceptable to the predefined operator

I tried to cast the variables to __int64 but then get:

error C2440: 'type cast' : cannot convert from 'LARGE_INTEGER' to '__int64'

How do I resolve this?

Thanks,

Flatworm answered 8/9, 2009 at 17:54 Comment(0)
P
31

LARGE_INTEGER is a union of a 64-bit integer and a pair of 32-bit integers. If you want to perform 64-bit arithmetic on one you need to select the 64-bit int from inside the union.

LARGE_INTEGER a = { 0 };
LARGE_INTEGER b = { 0 };

__int64 c = a.QuadPart - b.QuadPart;
Paternity answered 8/9, 2009 at 18:0 Comment(2)
How can you have 2 LARGE_INTEGER? Usually (link in the question) you have only one as input and want to convert it to long long or anything.Burrus
@Burrus LARGE_INTEGER frequency; frequency.QuadPart = 0; QueryPerformanceFrequency(&frequency); Do_Something_Worth_Timing(); QueryPerformanceCounter(&t2); TRACE("It took %f seconds!\n", (float)(t2.QuadPart - t1.QuadPart) / (float)frequency.QuadPart ); LARGE_INTEGER t1, t2; QueryPerformanceCounter(&t1); // Assuming the (float) is precise enough...Shroff
F
11

LARGE_INTEGER is a union, documented here. You probably want a QuadPart member.

Foundling answered 8/9, 2009 at 17:58 Comment(0)
M
10

Here it is:

LARGE_INTEGER x,y;
///
//Some codes...
///

__int64 diff = x.QuadPart - y.QuadPart

Because QuadPart is defined as a LONGLONG , that same as __int64.

Millisent answered 8/9, 2009 at 18:4 Comment(0)
E
3

LARGE_INTEGER is a union, you can still use .QuadPart if you want to work on the 64-bit value.

Epiblast answered 8/9, 2009 at 17:58 Comment(0)
S
1

As the Documentation says in the Remarks section :

The LARGE_INTEGER structure is actually a union. If your compiler has built-in support for 64-bit integers, use the QuadPart member to store the 64-bit integer. Otherwise, use the LowPart and HighPart members to store the 64-bit integer.

So if your compiler supports 64 bit integers use quadPart like this :

LARGE_INTEGER a, b;
__int64 diff = a.QuadPart - b.QuadPart
Suntan answered 30/8, 2016 at 16:28 Comment(0)
F
0

In addition to the answers, if you are looking to construct a LARGE_INTEGER with a value other than zero, you can assign the low and high parts separately. LowPart is first as defined in the union, and the only highPart is signed.

LARGE_INTEGER li = {0x01234567, -1};
Furan answered 23/2, 2017 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.