What is the correct way to cast from "OLE_HANDLE" to "HICON"?

What is the correct way to drop from OLE_HANDLE to HICON for x64 assembly target?

Specifically when using regular C-Style, I get this warning when compiling with x64 config:

warning C4312: "cast type": conversion from "OLE_HANDLE" to "HICON" larger

Here is the violation code:

imgList.Add((HICON)ohIcon);

      

The above code works great for me, but I want to get rid of the warning on build for x64.

+1


source to share


5 answers


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

They are actually HANDLES, which are kernel objects that don't care what the resource is, just that you have a "handle". Remember the API is C so use C style and when you delete it use DeleteObject ().



edit: 64 bit eh ... problem because MS updated Handles to 64 bit but left only OLE stuff. Luckily, all they did was extra bits with zeros.

Try using the LongToHandle conversion routines and see the port of the MIDL guide - scroll about halfway down to "USER GUIDE and GDI Descriptors denoted by extended 32-bit values".

+4


source


Assuming you are using Microsoft Visual Studio based on the question ...

If you are developing for 32-bit targets only, you can disable this (and some other similar warnings) by disabling the project's "Detect 64-bit portability issues" option (C ++ / Wp64 compiler option).



If you are also developing for 64-bit purposes, then @Harper is probably right and you will need to do some more digging to get the hang of it. You might want to read this white paper as a starting point; skip to the section on USER and GDI descriptors.

+3


source


I did a bit of work - OLE_HANDLE looks long unsigned and HICON looks empty *. On 32 bit Windows they are the same size, but on Windows x64 void * is 64 bit. There is no safe way to make this cast - an extra 32 bits of undefined. Unfortunately, the only advice I could dig up involving ULONG (OLE_HANDLE are even rarer beasts) simply said, "Don't throw it on the pointer."

+1


source


I suspect the "right" answer to the "right" way to throw in between is "don't do it." Admittedly this is not very useful ... where is OLE_HANDLE located? It looks like you will have to rewrite your code with OLE_HANDLE in order to use HICON all over the place.

+1


source


HICON hSomeIcon = (HICON) hSomeOLEHandle;

      

i.e. they are interchangeable.

0


source







All Articles