在命令行上将 HRESULT 作为字符串传递
我需要将 HRESULT 值作为命令行参数传递给程序。我本来打算通过传递十六进制值来实现这一点,例如:
>receiver.exe 0x80048836
我正在尝试使用 wcstol 将此字符串表示形式转换回 HRESULT,例如:
HRESULT hr = wcstol(argv[2], NULL, 16);
但是,原始 HRESULT 的值通常更大大于 LONG_MAX
,因此在上面的行中,hr
最终为 0x7fffffff
。
那么,有两个问题:
我认为 HRESULTS 只是 32 位整数?所以我不确定如何返回大于
LONG_MAX
的 HRESULT。不过,它似乎在原始程序中工作正常(即 HRESULT 不会溢出)。有没有办法绕过
wcstol
的LONG_MAX
限制?也许该函数的另一个版本与 HRESULT 实际大小的整数相匹配?
谢谢!
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_MAX
restriction ofwcstol
? Maybe another version of the function that matches up with whatever size integer the HRESULT actually is?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看
wcstoul
。 http://msdn.microsoft.com/en-我们/library/5k9xb7x1(v=VS.80).aspxHRESULT 确实适合 32 位,但在您给出的示例中,它使用最高有效位,该位被认为充当有符号整数的符号位。使用 wcstoul 会将其放入无符号长整型中。
LONG_MAX
为0x7FFFFFFF
,是 31 个最低有效位中可以容纳的最高值,最高位被清零,但ULONG_MAX
上升到0xFFFFFFFF
因为它是无符号的。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
wcstoul
will fit it into an unsigned long.LONG_MAX
is0x7FFFFFFF
, the highest that can fit in the 31 least significant bits, leaving the top bit cleared, butULONG_MAX
goes up to0xFFFFFFFF
because it is unsigned.0x80048836 大于系统的 LONG_MAX (2147483647L),即 (0x7FFFFFFF)。根据msdn“当表示会导致溢出时,在这种情况下它将返回 LONG_MAX 或 LONG_MIN”,
因此在您的情况下,您会得到 LONG_MAX 作为结果返回。
实际的函数返回类型被声明为 long wcstol(...)。 long 的大小不一定是 32 位,这取决于您的系统。
在本例中,返回类型是有符号的 32 位,因此适合 32 位的最大有符号整数是 7FFFFFFF。
00000000 到 7FFFFFFFF 是从 0 到 LONG_MAX 的正数
FFFFFFFF 到 8000001 从 -1 到 LONG_MIN 是负数
顺便说一句,我相信“HRESULT hr = wcstol...”是不正确的,因为 wcstol 的返回类型是 (signed) long ,但 HRESULT 是 ULONG (unsigned long)。这可能会成为一个问题,具体取决于您如何使用该数据。
0x80048836 is greater than LONG_MAX for your system (2147483647L) which is (0x7FFFFFFF). According to msdn "when the representation would cause an overflow, in which case it returns LONG_MAX or LONG_MIN"
So in your case you get LONG_MAX returned as your result.
the actual function return type is declared as long wcstol(...). long is not necessarily 32 bits in size, that will depend on your system.
In this case the return type is signed and 32 bit so the largest signed integer that will fit in 32 bits is 7FFFFFFF.
00000000 to 7FFFFFFF is positive from 0 to LONG_MAX
FFFFFFFF to 8000001 is negative from -1 to LONG_MIN
Incidentally I believe "HRESULT hr = wcstol..." would be incorrect since the return type of wcstol is (signed) long , but HRESULT is ULONG (unsigned long). This might be a problem depending on how you use that data.