views:

401

answers:

5

What is the proper way to cast from an 'OLE_HANDLE' to an 'HICON' for an x64 target build?

In particular with a normal C-Style cast, I get this warning when compiling with an x64 config:

warning C4312: 'type cast' : conversion from 'OLE_HANDLE' to 'HICON' of greater size

Here is the offending code:

imgList.Add((HICON)ohIcon);

The above code works fine for me, but I want to get rid of the warning when building for x64.

A: 
HICON hSomeIcon = (HICON) hSomeOLEHandle;

i.e. they're interchangeable.

chaos
see my new comments why this is not what I'm looking for
Brian R. Bondy
+4  A: 

The H gives it away, in this case the library code has created a distinct type to give you a little more type safety (in the days of old C APIs).

They are actually both HANDLEs, which is a kernel object that doesn't really care what the resource is, just that you have a 'handle' to it. Remember the API is a C one, so use C style casts, and when you come to delete it, use DeleteObject().

edit: 64 bits eh... the problem's because MS updated Handles to be 64 bits, but left the OLE stuff alone. Fortunately, all they did was pad the extra bits with zeros.

Try using the LongToHandle conversion routines and see the MIDL porting guide - scroll about halfway down to the "USER and GDI handles are sign extended 32b values" section.

gbjbaanb
Good find - I couldn't locate information that explicitly stated how to get from one to the other.
Harper Shelby
+1  A: 

I did a little digging - OLE_HANDLE appears to be a unsigned long, and HICON is a void* . In 32 bit Windows, these are the same size, but in Windows x64, the void* is 64 bits. There isn't a safe way to make this cast - the extra 32 bits are undefined. Unfortunately, the only advice I was able to dig up involving ULONGs (OLE_HANDLEs are even rarer beasts) said simply "don't cast this to a pointer".

Harper Shelby
+3  A: 

Assuming you're using Microsoft Visual Studio based on the question...

If you are developing only for 32-bit targets, you may choose to disable this (and some other similar warnings) by turning off the project option "Detect 64-bit Portability Issues" (C++ compiler option /Wp64).

If you are developing for 64-bit targets as well, then @Harper is probably right and you'll need to do some more digging on the right way to handle this. You might want to read this white paper as a starting point; go to the section about USER and GDI handles.

Patrick Johnmeyer
thanks for the white paper link
Brian R. Bondy
+1  A: 

I suspect that the "correct" answer for the "proper" way to cast between them is "don't do that". Admittedly that's not very helpful ... where is the OLE_HANDLE coming from in the first place? It sounds like you're going to have to rewrite the code using OLE_HANDLE to use HICON everywhere.

DavidK