How to convert an Integer to LARGE_INTEGER

走远了吗. 提交于 2020-06-17 07:15:48

问题


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:


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);



回答2:


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: https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-large_integer~r1 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 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) 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;
}


来源:https://stackoverflow.com/questions/40584547/how-to-convert-an-integer-to-large-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!