SetWindowLongPtr and /Wp64

Security Briefs

Syndication

Help, readers.

#include <windows.h>
void main() {
  LONG_PTR x = 0;
  SetWindowLongPtr(0, 0, x);
}

Ok, forget the fact that the above code doesn't do anything useful. But you'd expect it to compile without a warning, right? Well, compile it with the default Visual C++ options /Wp64 at a warning level of 4 and you're treated to the following:

cl -c -Wp64 -W4 test.cpp
warning C4244: 'argument' : conversion from 'LONG_PTR' to 'LONG', possible loss of data

I couldn't figure out why SetWindowLongPtr wouldn't actually take a LONG_PTR. Based on this warning it appears to take a LONG.

So I had a peek at the WINUSER.H header file at the actual function declaration. Turns out that there's two definitions, depending on whether a preprocessor switch (_WIN64) is defined. I'll summarize:

#ifdef _WIN64
LONG_PTR SetWindowLongPtr(
    HWND hWnd,
    int nIndex,
    LONG_PTR dwNewLong
);
#else
#define SetWindowLongPtr SetWindowLong
#endif

I searched all the header files in Visual Studio to see if any of them actually define _WIN64, but none do, so I'm assuming this is something defined at compile time by a 64 bit compiler.

Is it just me, or are the header files somewhat broken in VS.NET 2003? The /Wp64 switch is supposed to spit out warnings if you're doing stuff that would cause you trouble with a 64 bit compiler. But calling SetWindowLongPtr is the right thing to do for 64-bit compatibility.

Anybody else run into this? Did you just give up and turn off the /Wp64 switch, or did you find a cleaner workaround? Or am I just doing something really stupid?


Posted Jan 13 2005, 03:21 PM by keith-brown
Filed under:

Comments

Keith Brown wrote re: SetWindowLongPtr and /Wp64
on 01-13-2005 4:11 PM
Fritz pointed out in a private email that there's a discussion of this issue on the off-topic list:

http://groups.yahoo.com/group/win_tech_off_topic/message/36066

Looks like I'm not alone...
Clement wrote re: SetWindowLongPtr and /Wp64
on 03-20-2005 6:46 AM
Me too! Same issue.
I hit this.
Searched google with "SetWindowLongPtr" and found your blog.
If you found anything, be sure to update us on your blog.

Thanks,
Clement
Jason Doucette wrote re: SetWindowLongPtr and /Wp64
on 04-15-2005 5:43 AM
Keith,

The website you pointed out already explains the cause of the problem, but here's another source that speaks about it:
http://msdn.microsoft.com/msdnmag/issues/01/08/bugslayer/
"The final new warning switch is /Wp64 to help you find 64-bit portability errors. I've found /Wp64 to be quite helpful, except that the SetWindowLongPtr/GetWindowLongPtr macros, which are supposed to aid 32- and 64-bit access, are defined wrong when doing 32-bit compiles. So with /Wp64, you will have extra warnings around them unless you turn them off with the #pragma warning directives."

I get around the issue merely by using #pragma warning directives to disable the warning for that single line of code. I break apart any lines of code with multiple function calls (such as SetWindowLongPtr being a parameter into another function) to ensure I surround ONLY that one line of code, so as not to hide another legit warning of the same type.

Take care,

Jason
RUF wrote re: SetWindowLongPtr and /Wp64
on 04-20-2006 10:15 AM
What about SetWindowLongPtr(0, 0, (LONG)(UINT_PTR)x); ??
accord wrote re: SetWindowLongPtr and /Wp64
on 07-06-2006 2:41 PM
"What about SetWindowLongPtr(0, 0, (LONG)(UINT_PTR)x); ??"
This will wrong for 64 bit code...
Andrew wrote re: SetWindowLongPtr and /Wp64
on 01-03-2007 9:18 AM
You can turn off the /Wp64 switch and use more suitable tools for code analyze. For example Viva64:
http://www.viva64.com
Billy wrote re: SetWindowLongPtr and /Wp64
on 03-12-2007 11:03 AM
SetWindowLongPtr(0, 0, (__int3264)(LONG_PTR)x);

It took me forever to figure that out...
Douglas wrote re: SetWindowLongPtr and /Wp64
on 06-07-2007 8:53 AM
Good job Billy! I still have hair left.
Tony wrote re: SetWindowLongPtr and /Wp64
on 06-18-2007 1:57 AM
I have another problem,when I use SetWindowLongPtr,the GetLastError() show "1413",which means invalid index,Anybody can help me?
Arthur wrote re: SetWindowLongPtr and /Wp64
on 07-07-2008 7:59 AM

Thank you Billy! I finally got rid of that one annoying warning message I was getting!

Add a Comment

(required)  
(optional)
(required)  
Remember Me?