精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
服务方向
联系方式
国外程序员真较真,转换警告也要讨论,要么换函数,要么换类型,标准作法是32位下不要用64位函数。
It seems that ::SetWindowLongPtr() and ::GetWindowLongPtr() are defined to just map to set/get window long in 32-bit windows. The problem with this is that, even though I use the correct casts and functions, I still get a warning from VC++ about possible loss of data (because I stuff a pointer into this field). 在32位窗口软件里,似乎:: SetWindowLongPtr()和:: GetWindowLongPtr()被定义为仅映射到设置/获取窗口长整数。这样做的问题是,即使我使用正确的强制类型转换和函数,我仍然会从VC ++中收到有关可能的数据丢失的警告(因为我在该字段中塞了一个指针)。
Is there any way of writing code that calls ::SetWindowLongPtr() and ::GetWindowLongPtr() that doesn't issue this warning, without turning off the warning using command line options or #pragma warning? 有没有办法避免警告,在不关闭警告选项前提下?
Followup: looks like Microsoft had trouble too. Add 后续行动:看起来微软也遇到了麻烦。加
#include <atlbase.h>
#include <atlwin.h>
and the casts are quiet. atlwin.h redefines SetWindowLongPtr and GetWindowLongPtr. 转换不出警告。 atlwin.h重新定义了SetWindowLongPtr和GetWindowLongPtr。
Until the PSDK team fix the windows headers for those two functions, you'll just have pad those functions with "#pragma warning(disable)"s, or use another API (GetProp/SetProp/RemoveProp?) 在PSDK团队修复这两个功能的Windows标头之前,您只需使用“ #pragma warning(disable)”来填充这些功能,或者使用另一个API(GetProp / SetProp / RemoveProp?)
Me, I make sure the window styles are set at RegisterClassEx/CreateWindowEx time, and use GetProp/SetProp to store the context of a window. I know there are times when GetWindowLongPtr/SetWindowLongPtr are absolutely necessary, but I haven't encountered a time when I absolutely needed those two functions. 我,我确保将窗口样式设置为RegisterClassEx / CreateWindowEx时,并使用GetProp / SetProp存储窗口的上下文。我知道有时候确实绝对需要GetWindowLongPtr / SetWindowLongPtr,但是我没有遇到绝对需要这两个函数的时候。
You're probably gettings warnings because the casts won't work in 64-bit code. To disable: Project + Properties, Configuration Properties, C/C++, General, Detect 64-bit Portability issues = No. 您可能会收到警告,因为强制转换无法在64位代码中运行。禁用:项目+属性,配置属性,C / C ++,常规,检测64位可移植性问题=否。
I'm sorry, but that doesn't answer my question at all. 对不起,但这根本无法回答我的问题。
The functions ::SetWindowLongPtr() and ::GetWindowLongPtr() are explicitly defined to take, and return, LONG_PTR values. Thus, casting between that and a pointer is intended to work. That's what the functions are for, else I could just use the old versions of the functions.
However, when I compile this code, I still get warnings about casts; one warning about casting a LONG_PTR to a LONG, and one warning about casting a LONG to a pointer, so these functions do not seem to actually behave the way they're documented. 函数:: SetWindowLongPtr()和:: GetWindowLongPtr()被显式定义以获取并返回LONG_PTR值。因此,在该指针和指针之间进行强制转换是可行的。这就是功能的用途,否则我只能使用旧版本的功能。 LONG_PTR 到 LONG有警告,好像这些功能实际上表现和文档不一样。,
if (msg == WM_NCCREATE) {
C_Window * w = (CreateParams *)((CREATESTRUCT *)lParam)->lpCreateParams;
::SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG_PTR)w);
}
C_Window * cw = (C_Window *)::GetWindowLongPtr(hWnd, GWL_USERDATA);
The way I read the documentation, the LONG_PTR type should already avoid the warning, but the implementations of the functions do not appear to match documentation. 在阅读文档的方式中,LONG_PTR类型应该已经避免了警告,但是函数的实现似乎与文档不匹配。
I am not interested in turning off the warnings, as I already described in my initial question. 正如我在最初的问题中已经描述的那样,我对关闭警告不感兴趣。
Cast your parameter to LONG_PTR to avoid the warning. 将参数强制转换为LONG_PTR,以避免出现警告。
I missed the Ptr postfix; creature of habit. LONG_PTR is #defined as __w64 long (in basetsd.h) if _WIN64 isn't defined. If I fake _WIN64, the casts are quiet. Removing __w64 from the #define has no effect. 我不用Ptr后缀,改下自己用。如果未定义_WIN64,则将LONG_PTR#定义为__w64 long(在basetsd.h中)。如果我伪造_WIN64,则转换很安静。从#define中删除__w64无效。