Warning when casting int to HWND

Viewed 3702

I have a program I'm writing for Win64 in C++ that is executed from a parent program, and needs to set its parent window to the parent program's window. The parent program passes in it's HWND in as a command line argument, and I'm parsing the argument as an int (using stoi()) before it is cast to an HWND. A simplified version of my code is shown below:

int parentHwnd = stoi(args[HWND_INDEX]);
SetParent(childHwnd, (HWND) parentHwnd);

However, I'm getting the following error when compiling:

warning C4312: 'type cast': conversion from 'int' to 'HWND' of greater size

Is there safe way to cast an int to a HWNDand eliminate this error? Or should I be parsing the given command line arg to something other than an int that will safely be able to be cast to a HWND?

1 Answers
  1. When passing pointers/handles you should use std::stoull;
  2. And use explicit cast HWND parentHwnd = (HWND)std::stoull(args[HWND_INDEX]);
Related