?
Solved

HANDLE to DWORD

Posted on 2005-03-02
8
Medium Priority
?
2,053 Views
Last Modified: 2008-02-01
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?

0
Comment
Question by:Crestline
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13446648
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;
0
 

Author Comment

by:Crestline
ID: 13446704
This is the warning i get...

warning C4311: 'type cast' : pointer truncation from 'HANDLE' to 'DWORD'
0
 

Author Comment

by:Crestline
ID: 13446715
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;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:Axter
ID: 13446720
Hi Crestline,
Can you post the code to include variable declarations?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13450886
maybe you can make a test.
try to create a MessageBox with sizeof() DWORD and HANDLE
0
 
LVL 6

Accepted Solution

by:
guitaristx earned 150 total points
ID: 13451330
I'll bet the error will go away if you disable the compiler's 64-bit portability warnings.
0
 
LVL 14

Expert Comment

by:wayside
ID: 13453320
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.
0
 

Author Comment

by:Crestline
ID: 13454094
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!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question