从HANDLE到ULONG C ++进行类型转换截断 [英] Type Cast Truncation from HANDLE to ULONG C++

查看:496
本文介绍了从HANDLE到ULONG C ++进行类型转换截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting the warning (treated as error):

从HANDLE到ULONG的类型转换指针截断

Type Cast Pointer Truncation from HANDLE to ULONG

当我尝试编译时,我知道该类型在编译ARM64而不是ARM时具有不同的长度,因此我需要更改类型或static_cast,但是会收到诸如"expected( ",将行更改为类似这样的内容:

When I try to compile, I understand that the type has a different length as I am compiling ARM64 rather than ARM, therefore I need to change the type or static_cast it, however I receive errors such as "expected (" when changing the line to something like this:

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

我添加了方括号,但是始终存在问题,而且似乎永远都无法解决,总是"Expected(":

I add brackets, however there is always a problem and it never seems to work, always "Expected (":

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

推荐答案

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

The HANDLE type is used to point to an opaque structure.

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

It usually stores an index value, but in the winnt.h header file, it is defined as a pointer-length type.

typedef void *HANDLE;

所以正确的方法是也将进程id视为指针长度类型.

So the correct approach is to treat the process id as a pointer-length type as well.

我知道您不喜欢HANDLE,因此您可以使用ULONG_PTR,它的长度与指针类型相同.

I know you don't like HANDLE, so you can use ULONG_PTR, it has the same length as the pointer type.

这是固定代码:

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的解决方案.

If you have to use ULONG for other reasons, you can refer to @SoronelHaetir's solution.

评论示例:

之前:

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 ==========

这篇关于从HANDLE到ULONG C ++进行类型转换截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆