Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert from uint32_t* to LPDWORD

I'm trying to call the Windows API function GetExitCodeProcess which takes a LPDWORD as its second parameter.

According to MSDN, LPDWORD is a pointer to an unsigned 32-bit value. So I tried to pass a uint32_t*, but the compiler (MSVC 11.0) is not happy with it:

error C2664: 'GetExitCodeProcess' : cannot convert parameter 2 from 'uint32_t *' to 'LPDWORD'

Also a static_cast does not help. Why is that? And is it safe to use a reinterpret_cast in this case?

like image 810
Robert Hegner Avatar asked Jan 29 '14 17:01

Robert Hegner


1 Answers

From the documentation:

DWORD

A 32-bit unsigned integer. The range is 0 through 4294967295 decimal. This type is declared in IntSafe.h as follows:

typedef unsigned long DWORD;

So, LPDWORD is unsigned long int*. But you are trying to pass unsigned int*. I know that the types point to variables that are the same size, but the pointer types are not compatible.

The solution is to declare a variable of type DWORD, and pass the address of that variable. Something like this:

DWORD dwExitCode;
if (!GetExitCodeProcess(hProcess, &dwExitCode))
{
    // deal with error
}
uint32_t ExitCode = dwExitCode;
like image 80
David Heffernan Avatar answered Sep 25 '22 10:09

David Heffernan