在命令行上将 HRESULT 作为字符串传递

发布于 2024-09-29 19:41:49 字数 599 浏览 4 评论 0原文

我需要将 HRESULT 值作为命令行参数传递给程序。我本来打算通过传递十六进制值来实现这一点,例如:

>receiver.exe 0x80048836

我正在尝试使用 wcstol 将此字符串表示形式转换回 HRESULT,例如:

HRESULT hr = wcstol(argv[2], NULL, 16);

但是,原始 HRESULT 的值通常更大大于 LONG_MAX,因此在上面的行中,hr 最终为 0x7fffffff

那么,有两个问题:

  1. 我认为 HRESULTS 只是 32 位整数?所以我不确定如何返回大于 LONG_MAX 的 HRESULT。不过,它似乎在原始程序中工作正常(即 HRESULT 不会溢出)。

  2. 有没有办法绕过 wcstolLONG_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:

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

  2. Is there a way to get around the LONG_MAX restriction of wcstol? Maybe another version of the function that matches up with whatever size integer the HRESULT actually is?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情栀口红 2024-10-06 19:41:49

查看wcstoulhttp://msdn.microsoft.com/en-我们/library/5k9xb7x1(v=VS.80).aspx
HRESULT 确实适合 32 位,但在您给出的示例中,它使用最高有效位,该位被认为充当有符号整数的符号位。使用 wcstoul 会将其放入无符号长整型中。

LONG_MAX0x7FFFFFFF,是 31 个最低有效位中可以容纳的最高值,最高位被清零,但 ULONG_MAX 上升到 0xFFFFFFFF 因为它是无符号的。

Check out wcstoul. http://msdn.microsoft.com/en-us/library/5k9xb7x1(v=VS.80).aspx
The 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 is 0x7FFFFFFF, the highest that can fit in the 31 least significant bits, leaving the top bit cleared, but ULONG_MAX goes up to 0xFFFFFFFF because it is unsigned.

追风人 2024-10-06 19:41:49

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文