Link to home
Start Free TrialLog in
Avatar of Crestline
Crestline

asked on

HANDLE to DWORD

I have a function that needs a DWORD as one of the parameters but I currently have that value in a HANDLE.  When I try to cast a HANDLE as a DWORD it says it's getting truncated and I don't get the desired results.  Both are unsigned 32-int so I thought that would have been possible.

Any ideas?

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

That's very strange, effectively a handler is a 32 bit value (many times a masked pointer), so this wouldn't cause a warning

HANDLE someHandle = ....... etc.
DWORD parameter = (DWORD)someHandle;
Avatar of Crestline
Crestline

ASKER

This is the warning i get...

warning C4311: 'type cast' : pointer truncation from 'HANDLE' to 'DWORD'
This is the chunk of code...  Trying to get the Process Handle (not Process ID) of an application.

DWORD pid;
HWND findWindow = FindWindow("WindowsForms10.Window.8.app4", NULL);
GetWindowThreadProcessId(findWindow, &pid);
HANDLE ph = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
DWORD theDword = (DWORD)ph;
Avatar of Axter
Hi Crestline,
Can you post the code to include variable declarations?
maybe you can make a test.
try to create a MessageBox with sizeof() DWORD and HANDLE
ASKER CERTIFIED SOLUTION
Avatar of guitaristx
guitaristx

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HANDLE is defined as

typedef void *HANDLE;

This would be 64 bits on a 64-bit OS; casting this to a 32 bit DWORD would generate this error.

guitaristx is spot-on.
I turned off 'Detect 64-bit portability issues' and I no longer get the warning.  Thanks quitaristx.

Wayside, thanks for the info.  I learn something new every day!