从 HANDLE 到 ULONG C++ 的类型转换截断

Type Cast Truncation from HANDLE to ULONG C++

我收到警告(视为错误):

Type Cast Pointer Truncation from HANDLE to ULONG

当我尝试编译时,我了解到该类型的长度不同,因为我正在编译 ARM64 而不是 ARM,因此我需要更改类型或 static_cast 它,但是我收到错误,例如“预期(”将行更改为如下内容时:

return static_cast<ULONG>PsGetProcessId(current_process); //this gives me invalid conversion type as 
                                                          //there are no brackets around the static cast 
                                                          //because I am returning its value

我加了括号,但是总是有问题,而且似乎从来没有用过,总是“预期(”:

return (static_cast<ULONG>)PsGetProcessId(current_process); //this bracket error 

下面是原始代码

ULONG memory::get_process_id_by_name(PEPROCESS start_process, const char* process_name)
{
    PLIST_ENTRY active_process_links;
    PEPROCESS current_process = start_process;

    do
    {
        PKPROCESS kproc = (PKPROCESS)current_process;
        PDISPATCHER_HEADER header = (PDISPATCHER_HEADER)kproc;
        LPSTR current_process_name = (LPSTR)((PUCHAR)current_process + IMAGE_FILE_NAME);

        if (header->SignalState == 0 && strcmp(current_process_name, process_name) == 0)
        {   
            return (ULONG)PsGetProcessId(current_process); //warning occurs here
        }

        active_process_links = (PLIST_ENTRY)((PUCHAR)current_process + ACTIVE_PROCESS_LINKS_FLINK);
        current_process = (PEPROCESS)(active_process_links->Flink);
        current_process = (PEPROCESS)((PUCHAR)current_process - ACTIVE_PROCESS_LINKS_FLINK);

    } while (start_process != current_process);

    return 0;
}

使用 ULONG_PTR 而不是 ULONG:

static_cast<ULONG_PTR>(hHandle)

如果句柄持有的值实际上是用作 ULONG,则将 ULONG_PTR 转换为 ULONG:

static_cast<ULONG>(static_cast<ULONG_PTR>(hHandle))

HANDLE类型用于指向不透明结构。

它通常存储一个索引值,但在winnt.h头文件中,它被定义为指针长度类型。

typedef void *HANDLE;

所以正确的做法是把进程id也当作一个指针长度类型。

我知道你不喜欢HANDLE,所以你可以用ULONG_PTR,它和指针类型一样长

固定代码如下:

ULONG_PTR memory::get_process_id_by_name(PEPROCESS start_process, const char* process_name)
{
    PLIST_ENTRY active_process_links;
    PEPROCESS current_process = start_process;

    do
    {
        PKPROCESS kproc = (PKPROCESS)current_process;
        PDISPATCHER_HEADER header = (PDISPATCHER_HEADER)kproc;
        LPSTR current_process_name = (LPSTR)((PUCHAR)current_process + IMAGE_FILE_NAME);

        if (header->SignalState == 0 && strcmp(current_process_name, process_name) == 0)
        {
            return (ULONG_PTR)PsGetProcessId(current_process);
        }

        active_process_links = (PLIST_ENTRY)((PUCHAR)current_process + ACTIVE_PROCESS_LINKS_FLINK);
        current_process = (PEPROCESS)(active_process_links->Flink);
        current_process = (PEPROCESS)((PUCHAR)current_process - ACTIVE_PROCESS_LINKS_FLINK);

    } while (start_process != current_process);

    return 0;
}

如果因为其他原因不得不使用ULONG,可以参考@SoronelHaetir的解决方法。


评论示例:

之前:

void test()
{
    ULONG value = (ULONG_PTR)0xFFFFFFFFFFFF;
    UNREFERENCED_PARAMETER(value);
}

EXTERN_C NTSTATUS DriverEntry(DRIVER_OBJECT *pDriverObject, UNICODE_STRING *pRegistryPath)
{
    UNREFERENCED_PARAMETER(pDriverObject);
    UNREFERENCED_PARAMETER(pRegistryPath);
    test();
    return STATUS_UNSUCCESSFUL;
}

警告:

error C2220: the following warning is treated as an error
warning C4305: 'initializing': truncation from 'ULONG_PTR' to 'ULONG'
warning C4309: 'initializing': truncation of constant value
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

之后:

#pragma warning(push)
#pragma warning(disable: 4305)
#pragma warning(disable: 4309)
void test()
{
    ULONG value = (ULONG_PTR)0xFFFFFFFFFFFF;
    UNREFERENCED_PARAMETER(value);
}
#pragma warning(pop)

EXTERN_C NTSTATUS DriverEntry(DRIVER_OBJECT *pDriverObject, UNICODE_STRING *pRegistryPath)
{
    UNREFERENCED_PARAMETER(pDriverObject);
    UNREFERENCED_PARAMETER(pRegistryPath);
    test();
    return STATUS_UNSUCCESSFUL;
}

没有警告:

========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========