windows DWORD_PTR、INT_PTR、LONG_PTR、UINT_PTR、ULONG_PTR 何时、如何以及为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1271748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:39:05  来源:igfitidea点击:

DWORD_PTR, INT_PTR, LONG_PTR, UINT_PTR, ULONG_PTR When, How and Why?

windowswinapimfc

提问by ju.

I found that Windows has some new Windows Data Types

我发现 Windows 有一些新的Windows 数据类型

DWORD_PTR, INT_PTR, LONG_PTR, UINT_PTR, ULONG_PTR

can you tell me when, how and why to use them?

你能告诉我何时、如何以及为什么使用它们吗?

回答by Chris Becke

The *_PTRtypes were added to the Windows API in order to support Win64's 64bit addressing.

这些*_PTR类型已添加到 Windows API 中以支持 Win64 的 64 位寻址。

Because 32bit APIs typically passed pointers using data types like DWORD, it was necessary to create new types for 64 bit compatibility that could substitute for DWORDin 32bit applications, but were extended to 64bits when used in a 64bit applications.

因为 32 位 API 通常使用诸如 之类的数据类型传递指针DWORD,所以有必要为 64 位兼容性创建新类型,这些类型可以替代DWORD32 位应用程序,但在 64 位应用程序中使用时扩展到 64 位。

So, for example, application developers who want to write code that works as 32bit OR 64bit the windows 32bit API SetWindowLong(HWND,int,LONG)was changed to SetWindowLongPtr(HWND,int,LONG_PTR)

因此,例如,想要编写可作为 32 位或 64 位运行的代码的应用程序开发人员,Windows 32 位 APISetWindowLong(HWND,int,LONG)已更改为SetWindowLongPtr(HWND,int,LONG_PTR)

In a 32bit build, SetWindowLongPtris simply a macro that resolves to SetWindowLong, and LONG_PTRis likewise a macro that resolves to LONG. In a 64bit build on the other hand, SetWindowLongPtris an API that accepts a 64bit long as its 3rd parameter, and ULONG_PTRis typedef for unsigned __int64.

在一个32位的构建,SetWindowLongPtr是一个简单的宏解析为SetWindowLong,并且LONG_PTR同样是宏观解析到LONG。另一方面,在 64 位构建中,SetWindowLongPtrAPI 接受 64 位长作为其第三个参数,并且ULONG_PTRunsigned __int64.

By using these _PTRtypes, one codebase can compile for both Win32 and Win64 targets.

通过使用这些_PTR类型,一个代码库可以同时针对 Win32 和 Win64 目标进行编译。



When performing pointer arithmetic, these types should also be used in 32bit code that needs to be compatible with 64bit.

在进行指针运算时,这些类型也应该用在需要兼容 64bit 的 32bit 代码中。

so, if you need to access an array with more than 4billion elements, you would need to use an INT_PTR rather than an INT

因此,如果您需要访问具有超过 40 亿个元素的数组,则需要使用 INT_PTR 而不是 INT

  CHAR* pHuge = new CHAR[0x200000000]; // allocate 8 billion bytes
  INT idx;
  INT_PTR idx2;
  pHuge[idx]; // can only access the 1st 4 billion elements.
  pHuge[idx2]; // can access all 64bits of potential array space.

回答by Goz

Chris Becke is pretty much correct. Its just worth noting that these _PTR types are just types that are 32-bits wide on a 32-bit app and 64-bits wide on a 64-bit app. Its as simple as that.

克里斯贝克几乎是正确的。值得注意的是,这些 _PTR 类型只是在 32 位应用程序上为 32 位宽而在 64 位应用程序上为 64 位宽的类型。就这么简单。

You could easily use __int3264 instead of INT_PTR for example.

例如,您可以轻松地使用 __int3264 而不是 INT_PTR 。