Skip to content

Commit

Permalink
Remove GetWindowLong and SetWindowLong from UnsafeNativeMethods… (#2447)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetrou authored and RussKie committed Jan 8, 2020
1 parent 4119b73 commit 0f67967
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,7 @@ public void ShowDropDown(Control parent)
finally
{
User32.SetWindowLong(this, User32.GWL.HWNDPARENT, IntPtr.Zero);

// sometimes activation goes to LALA land - if our parent control is still around, remind it to take focus.
if (parent != null && parent.Visible)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ internal void ShowDesignerActionPanel(IComponent relatedComponent, DesignerActio
{
Debug.WriteLineIf(s_designeActionPanelTraceSwitch.TraceVerbose, "Assigning owner to mainParentWindow");
Debug.WriteLineIf(DesignerActionUI.DropDownVisibilityDebug.TraceVerbose, "Assigning owner to mainParentWindow");
UnsafeNativeMethods.SetWindowLong(new HandleRef(designerActionHost, designerActionHost.Handle), NativeMethods.GWL_HWNDPARENT, new HandleRef(_mainParentWindow, _mainParentWindow.Handle));
User32.SetWindowLong(designerActionHost, User32.GWL.HWNDPARENT, new HandleRef(_mainParentWindow, _mainParentWindow.Handle));
}

_cancelClose = true;
Expand Down Expand Up @@ -872,7 +872,7 @@ protected override void OnClosing(ToolStripDropDownClosingEventArgs e)
}

// what's the owner of the windows being activated?
IntPtr parent = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, hwndActivating), NativeMethods.GWL_HWNDPARENT);
IntPtr parent = User32.GetWindowLong(new HandleRef(this, hwndActivating), User32.GWL.HWNDPARENT);
// is it currently disabled (ie, the activating windows is in modal mode)
if (!IsWindowEnabled(parent))
{
Expand Down Expand Up @@ -959,7 +959,7 @@ private static bool WindowOwnsWindow(IntPtr hWndOwner, IntPtr hWndDescendant)
{
Debug.WriteLine("\t\tOWNER: " + GetControlInformation(hWndOwner));
Debug.WriteLine("\t\tOWNEE: " + GetControlInformation(hWndDescendant));
IntPtr claimedOwnerHwnd = UnsafeNativeMethods.GetWindowLong(new HandleRef(null, hWndDescendant), NativeMethods.GWL_HWNDPARENT);
IntPtr claimedOwnerHwnd = User32.GetWindowLong(hWndDescendant, User32.GWL.HWNDPARENT);
Debug.WriteLine("OWNEE's CLAIMED OWNER: " + GetControlInformation(claimedOwnerHwnd));
}
#endif
Expand All @@ -971,7 +971,7 @@ private static bool WindowOwnsWindow(IntPtr hWndOwner, IntPtr hWndDescendant)

while (hWndDescendant != IntPtr.Zero)
{
hWndDescendant = UnsafeNativeMethods.GetWindowLong(new HandleRef(null, hWndDescendant), NativeMethods.GWL_HWNDPARENT);
hWndDescendant = User32.GetWindowLong(hWndDescendant, User32.GWL.HWNDPARENT);
if (hWndDescendant == IntPtr.Zero)
{
Debug.WriteLineIf(DesignerActionUI.DropDownVisibilityDebug.TraceVerbose, "NOPE.");
Expand Down Expand Up @@ -1031,7 +1031,7 @@ internal static string GetControlInformation(IntPtr hwnd)
}
private bool IsWindowEnabled(IntPtr handle)
{
int style = (int)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_STYLE);
int style = (int)User32.GetWindowLong(new HandleRef(this, handle), User32.GWL.STYLE);
return (style & (int)User32.WS.DISABLED) == 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

internal static partial class Interop
{
Expand Down Expand Up @@ -32,5 +31,12 @@ public static IntPtr GetWindowLong(IHandle hWnd, GWL nIndex)
GC.KeepAlive(hWnd);
return result;
}

public static IntPtr GetWindowLong(HandleRef hWnd, GWL nIndex)
{
IntPtr result = GetWindowLong(hWnd.Handle, nIndex);
GC.KeepAlive(hWnd.Wrapper);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public static IntPtr SetWindowLong(IHandle hWnd, GWL nIndex, IHandle dwNewLong)
return result;
}

public static IntPtr SetWindowLong(IHandle hWnd, GWL nIndex, HandleRef dwNewLong)
{
IntPtr result = SetWindowLong(hWnd.Handle, nIndex, dwNewLong.Handle);
GC.KeepAlive(hWnd);
GC.KeepAlive(dwNewLong.Wrapper);
return result;
}

public static IntPtr SetWindowLong(IHandle hWnd, GWL nIndex, WNDPROC dwNewLong)
{
IntPtr pointer = Marshal.GetFunctionPointerForDelegate(dwNewLong);
Expand All @@ -55,5 +63,21 @@ public static IntPtr SetWindowLong(IntPtr hWnd, GWL nIndex, WNDPROC dwNewLong)
GC.KeepAlive(dwNewLong);
return result;
}

public static IntPtr SetWindowLong(HandleRef hWnd, GWL nIndex, WNDPROCINT dwNewLong)
{
IntPtr pointer = Marshal.GetFunctionPointerForDelegate(dwNewLong);
IntPtr result = SetWindowLong(hWnd.Handle, nIndex, pointer);
GC.KeepAlive(hWnd.Wrapper);
return result;
}

public static IntPtr SetWindowLong(HandleRef hWnd, GWL nIndex, HandleRef dwNewLong)
{
IntPtr result = SetWindowLong(hWnd.Handle, nIndex, dwNewLong.Handle);
GC.KeepAlive(hWnd.Wrapper);
GC.KeepAlive(dwNewLong.Wrapper);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ public const int

public const int
GCL_WNDPROC = (-24),
GWL_WNDPROC = (-4),
GWL_HWNDPARENT = (-8),
GWL_STYLE = (-16),
GWL_EXSTYLE = (-20),
GWL_ID = (-12),
GMR_VISIBLE = 0,
GMR_DAYSTATE = 1,
GDI_ERROR = (unchecked((int)0xFFFFFFFF)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,59 +309,6 @@ public static StringBuilder GetModuleFileNameLongPath(HandleRef hModule)
[DllImport(ExternDll.User32, EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
public extern static IntPtr SendCallbackMessage(HandleRef hWnd, int Msg, IntPtr wParam, IntPtr lParam);

//GetWindowLong won't work correctly for 64-bit: we should use GetWindowLongPtr instead. On
//32-bit, GetWindowLongPtr is just #defined as GetWindowLong. GetWindowLong really should
//take/return int instead of IntPtr/HandleRef, but since we're running this only for 32-bit
//it'll be OK.
public static IntPtr GetWindowLong(HandleRef hWnd, int nIndex)
{
if (IntPtr.Size == 4)
{
return GetWindowLong32(hWnd, nIndex);
}
return GetWindowLongPtr64(hWnd, nIndex);
}

[DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "GetWindowLong")]
public static extern IntPtr GetWindowLong32(HandleRef hWnd, int nIndex);

[DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "GetWindowLongPtr")]
public static extern IntPtr GetWindowLongPtr64(HandleRef hWnd, int nIndex);

//SetWindowLong won't work correctly for 64-bit: we should use SetWindowLongPtr instead. On
//32-bit, SetWindowLongPtr is just #defined as SetWindowLong. SetWindowLong really should
//take/return int instead of IntPtr/HandleRef, but since we're running this only for 32-bit
//it'll be OK.
public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, HandleRef dwNewLong)
{
if (IntPtr.Size == 4)
{
return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
}
return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
}

[DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "SetWindowLong")]
public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, HandleRef dwNewLong);

[DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "SetWindowLongPtr")]
public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, HandleRef dwNewLong);

public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, NativeMethods.WndProc wndproc)
{
if (IntPtr.Size == 4)
{
return SetWindowLongPtr32(hWnd, nIndex, wndproc);
}
return SetWindowLongPtr64(hWnd, nIndex, wndproc);
}

[DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "SetWindowLong")]
public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, NativeMethods.WndProc wndproc);

[DllImport(ExternDll.User32, CharSet = CharSet.Auto, EntryPoint = "SetWindowLongPtr")]
public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, NativeMethods.WndProc wndproc);

[DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern bool SetForegroundWindow(HandleRef hWnd);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using static Interop;

namespace System.Windows.Forms
{
Expand All @@ -28,7 +29,7 @@ public void DisableThreadWindows(bool disable, bool onlyWinForms)
if (MainForm != null && MainForm.IsHandleCreated)
{
// Get ahold of the parenting control
IntPtr parentHandle = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, MainForm.Handle), NativeMethods.GWL_HWNDPARENT);
IntPtr parentHandle = Interop.User32.GetWindowLong(new HandleRef(this, MainForm.Handle), User32.GWL.HWNDPARENT);

parentControl = Control.FromHandle(parentHandle);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ private unsafe void RunMessageLoopInner(msoloop reason, ApplicationContext conte
// If the owner window of the dialog is still enabled, disable it now.
// This can happen if the owner window is from a different thread or
// process.
hwndOwner = UnsafeNativeMethods.GetWindowLong(new HandleRef(_currentForm, _currentForm.Handle), NativeMethods.GWL_HWNDPARENT);
hwndOwner = User32.GetWindowLong(_currentForm, User32.GWL.HWNDPARENT);
if (hwndOwner != IntPtr.Zero)
{
if (User32.IsWindowEnabled(hwndOwner).IsTrue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ private static void RaiseThreadExit()
internal static void ParkHandle(HandleRef handle, DpiAwarenessContext dpiAwarenessContext = DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED)
{
Debug.Assert(UnsafeNativeMethods.IsWindow(handle), "Handle being parked is not a valid window handle");
Debug.Assert(((int)UnsafeNativeMethods.GetWindowLong(handle, NativeMethods.GWL_STYLE) & (int)User32.WS.CHILD) != 0, "Only WS_CHILD windows should be parked.");
Debug.Assert(((int)User32.GetWindowLong(handle, User32.GWL.STYLE) & (int)User32.WS.CHILD) != 0, "Only WS_CHILD windows should be parked.");

ThreadContext cxt = GetContextForHandle(handle);
if (cxt != null)
Expand Down
10 changes: 5 additions & 5 deletions src/System.Windows.Forms/src/System/Windows/Forms/AxHost.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -1416,7 +1416,7 @@ private bool CheckSubclassing()
}

IntPtr handle = Handle;
IntPtr currentWndproc = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_WNDPROC);
IntPtr currentWndproc = User32.GetWindowLong(new HandleRef(this, handle), User32.GWL.WNDPROC);
if (currentWndproc == wndprocAddr)
{
return true;
Expand All @@ -1432,7 +1432,7 @@ private bool CheckSubclassing()
// we need to resubclass outselves now...
Debug.Assert(!OwnWindow(), "why are we here if we own our window?");
WindowReleaseHandle();
UnsafeNativeMethods.SetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_WNDPROC, new HandleRef(this, currentWndproc));
User32.SetWindowLong(new HandleRef(this, handle), User32.GWL.WNDPROC, new HandleRef(this, currentWndproc));
WindowAssignHandle(handle, axState[assignUniqueID]);
InformOfNewHandle();
axState[manualUpdate] = true;
Expand Down Expand Up @@ -3559,7 +3559,7 @@ private void DetachAndForward(ref Message m)
DetachWindow();
if (handle != IntPtr.Zero)
{
IntPtr wndProc = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_WNDPROC);
IntPtr wndProc = User32.GetWindowLong(new HandleRef(this, handle), User32.GWL.WNDPROC);
m.Result = User32.CallWindowProcW(wndProc, handle, m.WindowMessage(), m.WParam, m.LParam);
}
}
Expand All @@ -3576,7 +3576,7 @@ private void DetachWindow()
private void InformOfNewHandle()
{
Debug.Assert(IsHandleCreated, "we got to have a handle to be here...");
wndprocAddr = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_WNDPROC);
wndprocAddr = User32.GetWindowLong(this, User32.GWL.WNDPROC);
}

private void AttachWindow(IntPtr hwnd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ public DialogResult ShowDialog(IWin32Window owner)
s_helpMsg = User32.RegisterWindowMessageW("commdlg_help");
}

NativeMethods.WndProc ownerProc = new NativeMethods.WndProc(OwnerWndProc);
User32.WNDPROCINT ownerProc = new User32.WNDPROCINT(OwnerWndProc);
_hookedWndProc = Marshal.GetFunctionPointerForDelegate(ownerProc);
Debug.Assert(IntPtr.Zero == _defOwnerWndProc, "The previous subclass wasn't properly cleaned up");

IntPtr userCookie = IntPtr.Zero;
try
{
// UnsafeNativeMethods.[Get|Set]WindowLong is smart enough to call SetWindowLongPtr on 64-bit OS
_defOwnerWndProc = UnsafeNativeMethods.SetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC, ownerProc);
_defOwnerWndProc = User32.SetWindowLong(new HandleRef(this, hwndOwner), User32.GWL.WNDPROC, ownerProc);

if (Application.UseVisualStyles)
{
Expand All @@ -225,10 +225,10 @@ public DialogResult ShowDialog(IWin32Window owner)
}
finally
{
IntPtr currentSubClass = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC);
IntPtr currentSubClass = User32.GetWindowLong(new HandleRef(this, hwndOwner), User32.GWL.WNDPROC);
if (_defOwnerWndProc != IntPtr.Zero || currentSubClass != _hookedWndProc)
{
UnsafeNativeMethods.SetWindowLong(new HandleRef(this, hwndOwner), NativeMethods.GWL_WNDPROC, new HandleRef(this, _defOwnerWndProc));
User32.SetWindowLong(new HandleRef(this, hwndOwner), User32.GWL.WNDPROC, new HandleRef(this, _defOwnerWndProc));
}

ThemingScope.Deactivate(userCookie);
Expand Down
16 changes: 8 additions & 8 deletions src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3974,11 +3974,11 @@ private int WindowExStyle
{
get
{
return unchecked((int)(long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_EXSTYLE));
return unchecked((int)(long)User32.GetWindowLong(this, User32.GWL.EXSTYLE));
}
set
{
UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_EXSTYLE, new HandleRef(null, (IntPtr)value));
User32.SetWindowLong(this, User32.GWL.EXSTYLE, (IntPtr)value);
}
}

Expand All @@ -3989,11 +3989,11 @@ internal int WindowStyle
{
get
{
return unchecked((int)(long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE));
return unchecked((int)(long)User32.GetWindowLong(this, User32.GWL.STYLE));
}
set
{
UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE, new HandleRef(null, (IntPtr)value));
User32.SetWindowLong(this, User32.GWL.STYLE, new HandleRef(null, (IntPtr)value));
}
}

Expand Down Expand Up @@ -5135,7 +5135,7 @@ protected virtual void DestroyHandle()
}
}

if (0 != ((int)User32.WS_EX.MDICHILD & (int)(long)UnsafeNativeMethods.GetWindowLong(new HandleRef(_window, InternalHandle), NativeMethods.GWL_EXSTYLE)))
if (0 != ((int)User32.WS_EX.MDICHILD & (int)(long)User32.GetWindowLong(new HandleRef(_window, InternalHandle), User32.GWL.EXSTYLE)))
{
UnsafeNativeMethods.DefMDIChildProc(InternalHandle, WindowMessages.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
Expand Down Expand Up @@ -11317,8 +11317,8 @@ private void SetWindowFont()

private void SetWindowStyle(int flag, bool value)
{
int styleFlags = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE)));
UnsafeNativeMethods.SetWindowLong(new HandleRef(this, Handle), NativeMethods.GWL_STYLE, new HandleRef(null, (IntPtr)(value ? styleFlags | flag : styleFlags & ~flag)));
int styleFlags = unchecked((int)((long)User32.GetWindowLong(this, User32.GWL.STYLE)));
User32.SetWindowLong(this, User32.GWL.STYLE, new HandleRef(null, (IntPtr)(value ? styleFlags | flag : styleFlags & ~flag)));
}

/// <summary>
Expand Down Expand Up @@ -11738,7 +11738,7 @@ private void WmClose(ref Message m)
lastParentHandle = parentHandle;
parentHandle = User32.GetParent(parentHandle);

int style = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(null, lastParentHandle), NativeMethods.GWL_STYLE)));
int style = unchecked((int)((long)User32.GetWindowLong(lastParentHandle, User32.GWL.STYLE)));

if ((style & (int)User32.WS.CHILD) == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1636,10 +1636,10 @@ private void WmDropDown(ref Message m)
IntPtr handle = SendMessage(NativeMethods.DTM_GETMONTHCAL, 0, 0);
if (handle != IntPtr.Zero)
{
int style = unchecked((int)((long)UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_EXSTYLE)));
int style = unchecked((int)((long)User32.GetWindowLong(new HandleRef(this, handle), User32.GWL.EXSTYLE)));
style |= (int)(User32.WS_EX.LAYOUTRTL | User32.WS_EX.NOINHERITLAYOUT);
style &= ~(int)(User32.WS_EX.RIGHT | User32.WS_EX.RTLREADING);
UnsafeNativeMethods.SetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_EXSTYLE, new HandleRef(this, (IntPtr)style));
User32.SetWindowLong(new HandleRef(this, handle), User32.GWL.EXSTYLE, new HandleRef(this, (IntPtr)style));
}
}
OnDropDown(EventArgs.Empty);
Expand Down
Loading

0 comments on commit 0f67967

Please sign in to comment.