I have a need to pass in an HRESULT value to a program as a command line argument. I had intended to do so by passing the hex value, e.g.:
>receiver.exe 0x80048836
I’m trying to convert this string representation back into an HRESULT using wcstol, eg:
HRESULT hr = wcstol(argv[2], NULL, 16);
However, the value of the original HRESULT is usually greater than LONG_MAX, so in the line above hr ends up as 0x7fffffff.
So, two questions:
-
I thought HRESULTS were just 32-bit integers? So I’m not sure how I’m getting back an HRESULT greater than
LONG_MAX. It seems to work fine in the originating program, though (i.e. the HRESULT doesn’t overflow). -
Is there a way to get around the
LONG_MAXrestriction ofwcstol? Maybe another version of the function that matches up with whatever size integer the HRESULT actually is?
Thanks!
Check out
wcstoul. http://msdn.microsoft.com/en-us/library/5k9xb7x1(v=VS.80).aspxThe HRESULT does fit in 32 bits, but with the example you gave it uses the most significant bit, which is considered to act like a sign bit for signed integers. Using
wcstoulwill fit it into an unsigned long.LONG_MAXis0x7FFFFFFF, the highest that can fit in the 31 least significant bits, leaving the top bit cleared, butULONG_MAXgoes up to0xFFFFFFFFbecause it is unsigned.