7

I'm using the following code:

const int GWL_STYLE = (-16);

const UInt32 WS_POPUP = 0x80000000;
const UInt32 WS_CHILD = 0x40000000;

[DllImport("user32.dll", SetLastError = true)]
static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, UInt32 dwNewLong);

and somewhere...

SetWindowLong(this.Handle, GWL_STYLE,
             ((GetWindowLong(this.Handle, GWL_STYLE) & ~(WS_POPUP)) | WS_CHILD));

Will this run properly on both 32-bit and 64-bit machines?

If not, if I compile my application to be ran as a x86 process, will it still work fine on a 64-bit machine?

And how can I rewrite the following code to be OK in both 32-bit and 64-bit machines?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Żubrówka
  • 730
  • 1
  • 10
  • 24
  • 2
    possible duplicate of [How do I pinvoke to GetWindowLongPtr and SetWindowLongPtr on 32-bit platforms?](http://stackoverflow.com/questions/3343724/how-do-i-pinvoke-to-getwindowlongptr-and-setwindowlongptr-on-32-bit-platforms) Hans's answer is excellent and spot-on, per usual. – Cody Gray - on strike Feb 14 '12 at 21:01
  • Possible duplicate of [How do I pinvoke to GetWindowLongPtr and SetWindowLongPtr on 32-bit platforms?](https://stackoverflow.com/questions/3343724/how-do-i-pinvoke-to-getwindowlongptr-and-setwindowlongptr-on-32-bit-platforms) – Tereza Tomcova Jul 21 '17 at 13:21

1 Answers1

5

I guess you are wondering if you chose the type UInt32 correctly. The answer is yes. The docs explicitly say it is always 32 bit value: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx

Your code is correct.

usr
  • 168,620
  • 35
  • 240
  • 369
  • 4
    Right, but it also says: *"**Note** This function has been superseded by the `SetWindowLongPtr` function. To write code that is compatible with both 32-bit and 64-bit versions of Windows, use the `SetWindowLongPtr` function."* So simply getting the declaration for `SetWindowLong` correct is not sufficient for 64-bit versions of Windows. – Cody Gray - on strike Feb 14 '12 at 21:00
  • SetWindowLong still works fine on 64-bit platforms as long as you're not passing anything that's officially pointer-sized (e.g., window procedures). – ChrisV Feb 14 '12 at 21:04
  • 2
    @Chris: Yes, but the `SetWindowLong` function is *frequently* used to pass things like pointers... It seems like a much better idea to write the code correctly the first time so you don't have to remember to worry about it later when you decide to reuse the same definition, but this time pass a pointer. – Cody Gray - on strike Feb 14 '12 at 21:09
  • @Cody: I agree completely. Easier to use SetWindowLongPtr all around. – ChrisV Feb 14 '12 at 21:27
  • 2
    @CodyGray: SetWindowLongPtr is a macro on 32-bit platforms so you won't get very far trying to P/Invoke it. – arx Feb 14 '12 at 21:42
  • @arx: Where in the world did I say to P/Invoke `SetWindowLongPtr`? The point was and is that this answer is incorrect. You should not use `SetWindowLong` on a 32-bit platform. For 64-bit platforms, you need to use `SetWindowLongPtr`. You have to write code to do this. I would have posted an answer of my own, but the question [has already been answered](http://stackoverflow.com/questions/3343724/how-do-i-pinvoke-to-getwindowlongptr-and-setwindowlongptr-on-32-bit-platforms), so I voted to close as a duplicate. – Cody Gray - on strike Feb 15 '12 at 20:59
  • 1
    @CodyGray: You quoted "to write code that is compatible with both 32-bit and 64-bit versions of Windows, use the SetWindowLongPtr function". I don't think it's a stretch to imagine someone reading that quote in a C#/pinvoke thread and thinking you intended it as good advice for C#/pinvoke. Although you didn't, it could easily have been misinterpreted (and it plainly was by ChrisV). – arx Feb 15 '12 at 21:19
  • @arx: I was quoting the documentation, not suggesting a plan of action. Again, the point was that the original answer is incorrect. I'm still not sure how you arrived at a conclusion otherwise. You *do* need to P/Invoke `SetWindowLongPtr` on 64-bit Windows. You need to fall back to `SetWindowLong` on 32-bit Windows. These comment boxes aren't long enough for me to write full answers, you know. There are some details that get omitted. – Cody Gray - on strike Feb 15 '12 at 21:54
  • @CodyGray Feel free to add your answer if you feel so – Żubrówka Jun 11 '12 at 11:51