用于存储对象指针的 GWL_USERDATA 的替代方法是什么? [英] What's an alternative to GWL_USERDATA for storing an object pointer?

查看:61
本文介绍了用于存储对象指针的 GWL_USERDATA 的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我处理的 Windows 应用程序中,我们有一个直接位于 Win32 之上的自定义框架(不要问).当我们创建一个窗口时,我们通常的做法是通过SetWindowLong(hwnd, GWL_USERDATA, this)this放到窗口的用户数据区中,这样我们就可以有一个MFC- 类似回调或紧密集成的 WndProc,视情况而定.问题是这在 64 位 Windows 上不起作用,因为 LONG 只有 32 位宽.对于这个问题,有什么更好的解决方案既适用于 32 位系统,也适用于 64 位系统?

In the Windows applications I work on, we have a custom framework that sits directly above Win32 (don't ask). When we create a window, our normal practice is to put this in the window's user data area via SetWindowLong(hwnd, GWL_USERDATA, this), which allows us to have an MFC-like callback or a tightly integrated WndProc, depending. The problem is that this will not work on 64-bit Windows, since LONG is only 32-bits wide. What's a better solution to this problem that works on both 32- and 64-bit systems?

推荐答案

SetWindowLongPtr 的创建是为了替换 在这些情况下,SetWindowLong.它的 LONG_PTR 参数允许您为 32 位或 64 位编译存储一个指针.

SetWindowLongPtr was created to replace SetWindowLong in these instances. It's LONG_PTR parameter allows you to store a pointer for 32-bit or 64-bit compilations.

LONG_PTR SetWindowLongPtr(      
    HWND hWnd,
    int nIndex,
    LONG_PTR dwNewLong
);

请记住,常量也发生了变化,所以现在的用法如下:

Remember that the constants have changed too, so usage now looks like:

SetWindowLongPtr(hWnd, GWLP_USERDATA, this);

另外不要忘记现在要检索指针,您必须使用 GetWindowLongPtr:

Also don't forget that now to retrieve the pointer, you must use GetWindowLongPtr:

LONG_PTR GetWindowLongPtr(      
    HWND hWnd,
    int nIndex
);

并且用法看起来像(同样,更改了常量):

And usage would look like (again, with changed constants):

LONG_PTR lpUserData = GetWindowLongPtr(hWnd, GWLP_USERDATA);
MyObject* pMyObject = (MyObject*)lpUserData;

这篇关于用于存储对象指针的 GWL_USERDATA 的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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