Jump to content
  • Advertisement
Sign in to follow this  

casting from LARGE_INTEGER?

This topic is 4075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using C++ on Windows and I'm trying to write a class that picks the best "tick count" functionality available and returns the current tick. I'm first checking for QueryPerformanceCounter(), then timeGetTime(), and finally GetTickCount(). Basically I want to use the best resolution timer available, but I'm not sure how to create a consistent class interface (or if its possible). For example: QueryPerformanceCounter() returns the tick in a LARGE_INTEGER. timeGetTime() returns a DWORD. GetTickCount() also returns a DWORD. My class will have a function "get_tick()", which uses the version it decided was the best in the class constructor. I'd like the get_tick() function to return a value of the same time no matter what tick function was called. Now I'm pretty sure this can be done by casting, but I'm having trouble figuring out how to cast from the LARGE_INTEGER type. Any advice would be appreciated. Hopefully I'm on the right lines... thanks jimbogd

Share this post

Link to post
Share on other sites
Cast from the parts of the union.

typedef union _LARGE_INTEGER {
struct { DWORD LowPart; LONG HighPart;};
struct { DWORD LowPart; LONG HighPart; } u;
__int64 QuadPart;


DWORD tick;

tick = li.LowPart;

Actually, you don't have to cast.

Share this post

Link to post
Share on other sites
You can't cast as such, or better yet, LARGE_INTEGER is a 64-bit value, while the others are 32-bit.

Given LARGE_INTEGER x, calling x.QuadPart will give you a LONGLONG (64-bit int). You cannot directly cast this one since it contains time in some internal units.

So the proper way would be:

// initialize the counter

double frequency; // global variable
// note that QueryPerformanceFrequency can fail if no high-res counter is available
// and returns 0
frequency = 1000.0 / iFrequency.QuadPart;
// frequency is now conversion between QueryXXX and our units (micro sec, milisec, seconds, up to you)

// to get the current time
return counter.QuadPart * frequency;
// you can of course round the result into an int, rather than use the double returned

But whatever value you receive from QueryPerformanceCounter is not time until you convert it into your units.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!