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.
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.