无法从uint32_t*转换为LPDWORD

Rob*_*ner 9 c++ winapi casting

我正在尝试调用Windows API函数GetExitCodeProcess,该函数将LPDWORD第二个参数作为其第二个参数.

根据MSDN,LPDWORD是一个指向无符号32位值的指针.所以我试图传递一个uint32_t*,但编译器(MSVC 11.0)对它不满意:

错误C2664:'GetExitCodeProcess':无法将参数2从'uint32_t*'转换为'LPDWORD'

也是一个static_cast没有帮助.这是为什么?reinterpret_cast在这种情况下使用a是否安全?

Dav*_*nan 6

文档:

DWORD

一个32位无符号整数.范围是0到4294967295十进制.此类型在IntSafe.h中声明如下:

typedef unsigned long DWORD;
Run Code Online (Sandbox Code Playgroud)

所以,LPDWORD是的unsigned long int*.但是你想要通过unsigned int*.我知道类型指向大小相同的变量,但指针类型不兼容.

解决方案是声明一个类型的变量DWORD,并传递该变量的地址.像这样的东西:

DWORD dwExitCode;
if (!GetExitCodeProcess(hProcess, &dwExitCode))
{
    // deal with error
}
uint32_t ExitCode = dwExitCode;
Run Code Online (Sandbox Code Playgroud)