DWORD_PTR,INT_PTR,LONG_PTR,UINT_PTR,ULONG_PTR何时,如何以及为什么? [英] DWORD_PTR, INT_PTR, LONG_PTR, UINT_PTR, ULONG_PTR When, How and Why?

查看:227
本文介绍了DWORD_PTR,INT_PTR,LONG_PTR,UINT_PTR,ULONG_PTR何时,如何以及为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I found that Windows has some new Windows Data Types

DWORD_PTR, INT_PTR, LONG_PTR, UINT_PTR, ULONG_PTR

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

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

推荐答案

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

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

由于32位API通常使用诸如DWORD之类的数据类型传递指针,因此有必要创建新的类型以实现64位兼容性,以在32位应用程序中替代DWORD,但在64位应用程序中使用时会扩展到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 DWORD in 32bit applications, but were extended to 64bits when used in a 64bit applications.

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

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位版本中,SetWindowLongPtr只是一个解析为SetWindowLong的宏,而LONG_PTR同样是一个解析为LONG的宏. 另一方面,在64位版本中,SetWindowLongPtr是一个接受64位作为其第3个参数的API,而ULONG_PTRunsigned __int64的typedef.

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

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

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

在执行指针算术时,还应该在需要与64位兼容的32位代码中使用这些类型.

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

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

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

  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.

这篇关于DWORD_PTR,INT_PTR,LONG_PTR,UINT_PTR,ULONG_PTR何时,如何以及为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆