除了GWL_USERDATA以外,还有什么方法可以存储对象指针? [英] What's an alternative to GWL_USERDATA for storing an object pointer?

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

问题描述

在我研究的Windows应用程序中,我们有一个直接位于Win32上方的自定义框架(不要问).创建窗口时,通常的做法是通过SetWindowLong(hwnd, GWL_USERDATA, this)this放置在窗口的用户数据区域中,这使我们可以拥有类似MFC的回调或紧密集成的WndProc.问题在于,由于LONG仅32位宽,因此无法在Win64上运行.在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 Win64, 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天全站免登陆