How to convert an Integer to LARGE_INTEGER

How do i convert an integer to LARGE_INTEGER?

For example, when I want to trigger a timer immediately:

LARGE_INTEGER zero; zero.QuadPart = 0; KeSetTimer(pTimer, zero, pDpc); 

Is there any way to convert 0 to LARGE_INTEGER? So I could do this instead:

KeSetTimer(pTimer, (SomeType)0, pDpc); 

I have tried:

KeSetTimer(pTimer, (LARGE_INTEGER )0, pDpc); 

But it doesn't work. I have Googled, but couldn't find any help.

1

3 Answers

LARGE_INTEGER is a struct. It is not possible to cast a value to a struct type.

You need to create an instance of the struct and set its fields as needed.

For example:

LARGE_INTEGER intToLargeInt(int i) { LARGE_INTEGER li; li.QuadPart = i; return li; } 

You can then use it like this:

KeSetTimer(pTimer, intToLargeInt(0), pDpc); 

To expand upon and enhance previous answers to this question for greater portability, the LARGE_INTEGER is actually a union of two structures intended to represent a 64-bit signed integer value while also accounting for cases where a compiler may not have built-in support for the 64-bit integral data type.

As the documentation hosted at: states:

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.

In the latter case, one would have to compose a signed long long integer, guaranteed by the C99 standard to have a width of at least 64 bits (as specified in ) from both the HighPart and LowPart of a given LARGE_INTEGER object, each with a width of 32 bits.

This can be done by shifting all of the bits of the HighPart left by 32 and bitwise OR-ing the result with the LowPart member so as to set its lower 32-bits.

Therefore, the reverse operation is to mask out the 32-bit High and Low parts of a given long long integer and correspondingly assign them to a LARGE_INTEGER instance's members.

Below are code samples for both operations, ensuring the case where 64-bit integers are not natively supported by the compiler are also accounted for:

// LARGE_INTEGER to 64-bit integral type: static long long toInteger(LARGE_INTEGER const & integer) { #ifdef INT64_MAX // Does the compiler natively support 64-bit integers? return integer.QuadPart; #else return (static_cast<long long>(integer.HighPart) << 32) | integer.LowPart; #endif } // signed long long to LARGE_INTEGER object: static LARGE_INTEGER toLargeInteger(long long value) { LARGE_INTEGER result; #ifdef INT64_MAX // Does the compiler natively support 64-bit integers? result.QuadPart = value; #else result.high_part = value & 0xFFFFFFFF00000000; result.low_part = value & 0xFFFFFFFF; #endif return result; } 

Yes, but if you are using C#, LARGE_INTEGER is a 64-signed integer, so why not just use System.Int64, or if you prefer, "using LARGE_INTEGER = System.Int64" // just as System.Int64 without calling the whole library it is also "good practice" prevents function poisoning

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like