LONG_PTR

The LONG_PTR type is a signed integer type that is large enough to hold a pointer. It is used for pointer-sized signed integer values.

Type Information

Description

The LONG_PTR type is a platform-dependent signed integer type. Its size is determined by the underlying architecture of the operating system. On a 32-bit Windows system, it is equivalent to a LONG (32 bits). On a 64-bit Windows system, it is equivalent to a LONGLONG (64 bits).

This type is commonly used in Windows API functions and structures when a signed integer value needs to hold a memory address or a pointer-related value. Using LONG_PTR ensures that your code is compatible with both 32-bit and 64-bit versions of Windows without requiring explicit conditional compilation for pointer size.

Usage

You will typically encounter LONG_PTR in the following scenarios:

Example

Consider a hypothetical scenario where you need to store a pointer and its offset. Using LONG_PTR ensures correct sizing across architectures:


#include <windows.h>
#include <stdio.h>

int main() {
    void* pData = (void*)0x12345678; // Example pointer
    LONG_PTR offset = 0x1000;

    // Calculate the address with offset
    LONG_PTR addressWithOffset = (LONG_PTR)pData + offset;

    printf("Original Pointer Address: %p\\n", pData);
    printf("Offset: %lld\\n", (long long)offset); // Use %lld for LONG_PTR on 64-bit
    printf("Address with Offset: %p\\n", (void*)addressWithOffset);

    // Example of a negative value if needed
    LONG_PTR negativeValue = -500;
    printf("Negative LONG_PTR value: %lld\\n", (long long)negativeValue);

    return 0;
}
        

Note: When printing LONG_PTR values, especially in C or C++, you may need to cast them to the appropriate format specifier for your compiler and target architecture (e.g., %lld for 64-bit with `long long`, or use specific macros if available in newer C++ standards).

Related Types