MSDN中关于SetWindowLongPtr和GetWindowLongPtr函数的描述错误

编程知识 更新时间:2023-05-01 12:37:35

1 这两个函数的历史

SetWindowLongPtr()和GetWindowLongPtr()这一对API是用来存取窗口对象中的特定数据块的,这也是用来取代SetWindowLong()和GetWindowLong()这对过时API的。不带Ptr结尾API过时的原因在于无法适应64位平台,而本文要指出的MSDN中存在的问题也就在于没有及时更新来适应64平台的情况。

2 错误所在

下面摘自MSDN官方网站(2015年1月13日)。

LONG_PTR WINAPI SetWindowLongPtr(
  _In_  HWND hWnd,
  _In_  int nIndex,
  _In_  LONG_PTR dwNewLong
);
nIndex  [in]

Type: int

The zero-based offset to the value to be set. Valid values are in the range zero through the number of bytes of extra window memory, minus the size of an integer. To set any other value, specify one of the following values.


这两个函数要想存取窗口对象额外数据,就必须在RegisterClass()时,通过WNDCLASS.cbWndExtra来申请附加字节。参数nIndex在函数用来读写窗口对象附加数据时,指的是从附加数据块的第nIndex个字节开始读写sizeof(LONG_PTR)个字节。所以其是否合法取决于从nIndex开始的sizeof(LONG_PTR)个字节是否全部位于窗体对象的附加数据块内。也就是0到(extra window memory)-sizeof(LONG_PTR)。而MSDN却简单的把它写成了0到(extra window memory)-sizeof(int)。

对于32位程序来说,sizeof(LONG_PTR) == sizeof(int),MSDN的描述是没有问题的。然而,对于64位程序,sizeof(LONG_PTR)=8,而sizeof(int)=4,两者不再相等。

也许MSDN此处的单词 integer 指的是依赖平台的整数,但是大多数程序员会把此处的integer和C/C++语言里的int对应起来。这样以来,还是会造成不小的困扰。



更多推荐

MSDN中关于SetWindowLongPtr和GetWindowLongPtr函数的描述错误

本文发布于:2023-04-23 15:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/29522fbda7aaa1617c10e012696312a2.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   错误   MSDN   SetWindowLongPtr   GetWindowLongPtr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 98676文章数
  • 25538阅读数
  • 0评论数