Skip to content

Commit

Permalink
Move from IntPtr to nint to avoid runtime casting errors. (#5791)
Browse files Browse the repository at this point in the history
We still had a number of potential and actual overflow scenarios. In order to make this more resilient, move SendMessage, PostMessage, and Get/SetWindowLong from IntPtr to nint.

This also makes things slightly faster as we aren't creating IntPtr structs and invoking the cast methods on it.

Note that IntPtr implicitly converts to nint. I've removed most of the actual IntPtr usages. I've also removed most of the HandleRef overloads and vetted the callers to do the right thing in regards to avoiding finalizer issues.

There is more we can do here, but this is a big first step.

Also clean up DateTimePicker and move SYSTEMTIME converters to SYSTEMTIME.

I've tried to remove all (IntPtr) casts in SendMessage calls.
  • Loading branch information
JeremyKuhne authored Sep 21, 2021
1 parent 314350b commit 834d0a0
Show file tree
Hide file tree
Showing 132 changed files with 2,640 additions and 2,990 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public void ShowDropDown(Control parent)
IntPtr hWndCapture = User32.GetCapture();
if (hWndCapture != IntPtr.Zero)
{
User32.SendMessageW(hWndCapture, User32.WM.CANCELMODE, IntPtr.Zero, IntPtr.Zero);
User32.SendMessageW(hWndCapture, User32.WM.CANCELMODE);
User32.ReleaseCapture();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static void ApplyTreeViewThemeStyles(TreeView treeView)
IntPtr hwnd = treeView.Handle;
ComCtl32.TVS_EX exstyle = (ComCtl32.TVS_EX)User32.SendMessageW(hwnd, (User32.WM)ComCtl32.TVM.GETEXTENDEDSTYLE);
exstyle |= ComCtl32.TVS_EX.DOUBLEBUFFER | ComCtl32.TVS_EX.FADEINOUTEXPANDOS;
User32.SendMessageW(hwnd, (User32.WM)ComCtl32.TVM.SETEXTENDEDSTYLE, IntPtr.Zero, (IntPtr)exstyle);
User32.SendMessageW(hwnd, (User32.WM)ComCtl32.TVM.SETEXTENDEDSTYLE, 0, (nint)exstyle);
}

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) => UITypeEditorEditStyle.DropDown;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ public override bool OnMouseMove(Glyph g, MouseButtons button, Point mouseLoc)
Rectangle oldBorderRect = BehaviorService.ControlRectInAdornerWindow(control);
bool needToUpdate = true;
// The ResizeBehavior can easily get into a situation where we are fighting with a layout engine. E.g., We resize control to 50px, LayoutEngine lays out and finds 50px was too small and resized back to 100px. This is what should happen, but it looks bad in the designer. To avoid the flicker we temporarily turn off painting while we do the resize.
User32.SendMessageW(control, User32.WM.SETREDRAW, PARAM.FromBool(false));
User32.SendMessageW(control, User32.WM.SETREDRAW, (nint)BOOL.FALSE);
try
{
bool fRTL = false;
Expand Down Expand Up @@ -772,7 +772,7 @@ public override bool OnMouseMove(Glyph g, MouseButtons button, Point mouseLoc)
finally
{
// While we were resizing we discarded painting messages to reduce flicker. We now turn painting back on and manually refresh the controls.
User32.SendMessageW(control, User32.WM.SETREDRAW, PARAM.FromBool(true));
User32.SendMessageW(control, User32.WM.SETREDRAW, (nint)BOOL.TRUE);
//update the control
if (needToUpdate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ protected virtual bool OnKeyCancel(object sender)
IntPtr hwnd = User32.WindowFromPoint(p);
if (hwnd != IntPtr.Zero)
{
User32.SendMessageW(hwnd, User32.WM.SETCURSOR, hwnd, (IntPtr)User32.HT.CLIENT);
User32.SendMessageW(hwnd, User32.WM.SETCURSOR, hwnd, (nint)User32.HT.CLIENT);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1950,7 +1950,7 @@ protected unsafe virtual void WndProc(ref Message m)

// We don't really want the focus, but we want to focus the designer. Below we handle WM_SETFOCUS
// and do the right thing.
User32.SendMessageW(Control.Handle, User32.WM.SETFOCUS, IntPtr.Zero, IntPtr.Zero);
User32.SendMessageW(Control.Handle, User32.WM.SETFOCUS);

// We simulate doubleclick for things that don't...
if (button == MouseButtons.Left && IsDoubleClick(x, y))
Expand Down Expand Up @@ -1984,7 +1984,7 @@ protected unsafe virtual void WndProc(ref Message m)

if (_toolPassThrough)
{
User32.SendMessageW(Control.Parent.Handle, (User32.WM)m.Msg, m.WParam, (IntPtr)GetParentPointFromLparam(m.LParam));
User32.SendMessageW(Control.Parent.Handle, (User32.WM)m.Msg, m.WParam, GetParentPointFromLparam(m.LParam));
return;
}

Expand Down Expand Up @@ -2033,7 +2033,7 @@ protected unsafe virtual void WndProc(ref Message m)
Control.Parent.Handle,
(User32.WM)m.Msg,
m.WParam,
(IntPtr)GetParentPointFromLparam(m.LParam));
GetParentPointFromLparam(m.LParam));
return;
}

Expand Down Expand Up @@ -2080,7 +2080,7 @@ protected unsafe virtual void WndProc(ref Message m)
Control.Parent.Handle,
(User32.WM)m.Msg,
m.WParam,
(IntPtr)GetParentPointFromLparam(m.LParam));
GetParentPointFromLparam(m.LParam));
_toolPassThrough = false;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private unsafe void ForceDesignerRedraw(bool focus)
{
if (_designer != null && _designer.IsHandleCreated)
{
User32.SendMessageW(_designer.Handle, User32.WM.NCACTIVATE, PARAM.FromBool(focus), IntPtr.Zero);
User32.SendMessageW(_designer.Handle, User32.WM.NCACTIVATE, PARAM.FromBool(focus));
User32.RedrawWindow(_designer.Handle, null, IntPtr.Zero, User32.RDW.FRAME);
}
}
Expand Down Expand Up @@ -256,7 +256,7 @@ protected override void WndProc(ref Message m)
if ((msg == User32.WM.VSCROLL) || (msg == User32.WM.HSCROLL))
{
// Send a message to ourselves to scroll
User32.SendMessageW(_designerRegion.Handle, msg, (IntPtr)PARAM.ToInt((int)wScrollNotify, 0), IntPtr.Zero);
User32.SendMessageW(_designerRegion.Handle, msg, PARAM.ToInt((int)wScrollNotify, 0));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,30 +438,33 @@ public static bool GenerateSnapShotWithWM_PRINT(Control control, ref Image image
IntPtr hWnd = control.Handle;
image = new Bitmap(Math.Max(control.Width, MINCONTROLBITMAPSIZE), Math.Max(control.Height, MINCONTROLBITMAPSIZE), PixelFormat.Format32bppPArgb);

//Have to do this BEFORE we set the testcolor.
// Have to do this BEFORE we set the testcolor.
if (control.BackColor == Color.Transparent)
{
using (Graphics g = Graphics.FromImage(image))
{
g.Clear(SystemColors.Control);
}
using Graphics g = Graphics.FromImage(image);
g.Clear(SystemColors.Control);
}

// To validate that the control responded to the wm_print message, we pre-populate the bitmap with a colored center pixel. We assume that the control _did not_ respond to wm_print if these center pixel is still this value
// To validate that the control responded to the wm_print message, we pre-populate the bitmap with a
// colored center pixel. We assume that the control _did not_ respond to wm_print if these center pixel
// is still this value.

Color testColor = Color.FromArgb(255, 252, 186, 238);
((Bitmap)image).SetPixel(image.Width / 2, image.Height / 2, testColor);
using (Graphics g = Graphics.FromImage(image))
{
IntPtr hDc = g.GetHdc();
//send the actual wm_print message
User32.SendMessageW(hWnd, User32.WM.PRINT, hDc, (IntPtr)(User32.PRF.CHILDREN | User32.PRF.CLIENT | User32.PRF.ERASEBKGND | User32.PRF.NONCLIENT));
User32.SendMessageW(
hWnd,
User32.WM.PRINT,
hDc,
(nint)(User32.PRF.CHILDREN | User32.PRF.CLIENT | User32.PRF.ERASEBKGND | User32.PRF.NONCLIENT));
g.ReleaseHdc(hDc);
}

//now check to see if our center pixel was cleared, if not then our wm_print failed
// Now check to see if our center pixel was cleared, if not then our WM_PRINT failed
if (((Bitmap)image).GetPixel(image.Width / 2, image.Height / 2).Equals(testColor))
{
//wm_print failed
return false;
}

Expand Down Expand Up @@ -863,9 +866,7 @@ private static int ScaleLogicalToDeviceUnitsX(int unit)
=> DpiHelper.IsScalingRequired ? DpiHelper.LogicalToDeviceUnitsX(unit) : unit;

private static ComCtl32.TVS_EX TreeView_GetExtendedStyle(IntPtr handle)
{
return (ComCtl32.TVS_EX)User32.SendMessageW(handle, (User32.WM)ComCtl32.TVM.GETEXTENDEDSTYLE);
}
=> (ComCtl32.TVS_EX)User32.SendMessageW(handle, (User32.WM)ComCtl32.TVM.GETEXTENDEDSTYLE);

/// <summary>
/// Modify a WinForms TreeView control to use the new Explorer style theme
Expand All @@ -884,7 +885,7 @@ public static void ApplyTreeViewThemeStyles(TreeView treeView)
UxTheme.SetWindowTheme(hwnd, "Explorer", null);
ComCtl32.TVS_EX exstyle = TreeView_GetExtendedStyle(hwnd);
exstyle |= ComCtl32.TVS_EX.DOUBLEBUFFER | ComCtl32.TVS_EX.FADEINOUTEXPANDOS;
User32.SendMessageW(hwnd, (User32.WM)ComCtl32.TVM.SETEXTENDEDSTYLE, IntPtr.Zero, (IntPtr)exstyle);
User32.SendMessageW(hwnd, (User32.WM)ComCtl32.TVM.SETEXTENDEDSTYLE, 0, (nint)exstyle);
}

/// <summary>
Expand All @@ -900,7 +901,11 @@ public static void ApplyListViewThemeStyles(ListView listView)

IntPtr hwnd = listView.Handle;
UxTheme.SetWindowTheme(hwnd, "Explorer", null);
User32.SendMessageW(hwnd, (User32.WM)ComCtl32.LVM.SETEXTENDEDLISTVIEWSTYLE, (IntPtr)ComCtl32.LVS_EX.DOUBLEBUFFER, (IntPtr)ComCtl32.LVS_EX.DOUBLEBUFFER);
User32.SendMessageW(
hwnd,
(User32.WM)ComCtl32.LVM.SETEXTENDEDLISTVIEWSTYLE,
(nint)ComCtl32.LVS_EX.DOUBLEBUFFER,
(nint)ComCtl32.LVS_EX.DOUBLEBUFFER);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1206,9 +1206,9 @@ private void OnDesignerActivate(object source, EventArgs evevent)
private unsafe void OnDesignerDeactivate(object sender, EventArgs e)
{
Control control = Control;
if (control != null && control.IsHandleCreated)
if (control is not null && control.IsHandleCreated)
{
User32.SendMessageW(control.Handle, User32.WM.NCACTIVATE);
User32.SendMessageW(control.Handle, User32.WM.NCACTIVATE, (nint)BOOL.FALSE);
User32.RedrawWindow(control.Handle, null, IntPtr.Zero, User32.RDW.FRAME);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private unsafe void OnDesignerActivate(object source, EventArgs evevent)
Control control = Control;
if (control != null && control.IsHandleCreated)
{
User32.SendMessageW(control.Handle, User32.WM.NCACTIVATE, (IntPtr)1, IntPtr.Zero);
User32.SendMessageW(control.Handle, User32.WM.NCACTIVATE, (nint)BOOL.TRUE);
User32.RedrawWindow(control.Handle, null, IntPtr.Zero, User32.RDW.FRAME);
}
}
Expand All @@ -392,7 +392,7 @@ private unsafe void OnDesignerDeactivate(object sender, EventArgs e)
Control control = Control;
if (control != null && control.IsHandleCreated)
{
User32.SendMessageW(control.Handle, User32.WM.NCACTIVATE, IntPtr.Zero, IntPtr.Zero);
User32.SendMessageW(control.Handle, User32.WM.NCACTIVATE, (nint)BOOL.FALSE);
User32.RedrawWindow(control.Handle, null, IntPtr.Zero, User32.RDW.FRAME);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private void UpdateControlName(string name)
if (lb.IsHandleCreated && lb.Items.Count == 0)
{
User32.SendMessageW(lb, (User32.WM)User32.LB.RESETCONTENT);
User32.SendMessageW(lb, (User32.WM)User32.LB.ADDSTRING, IntPtr.Zero, name);
User32.SendMessageW(lb, (User32.WM)User32.LB.ADDSTRING, 0, name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ private View View
}
}

/// <summary>
/// We override GetHitTest to make the header in report view UI-active.
/// </summary>
protected unsafe override bool GetHitTest(Point point)
{
// We override GetHitTest to make the header in report view UI-active.

ListView listView = (ListView)Component;
if (listView.View == View.Details)
{
Expand All @@ -85,7 +84,7 @@ protected unsafe override bool GetHitTest(Point point)
{
User32.MapWindowPoints(IntPtr.Zero, headerHwnd, &point, 1);
_hdrhit.pt = point;
User32.SendMessageW(headerHwnd, (User32.WM)ComCtl32.HDM.HITTEST, IntPtr.Zero, ref _hdrhit);
User32.SendMessageW(headerHwnd, (User32.WM)ComCtl32.HDM.HITTEST, 0, ref _hdrhit);
if (_hdrhit.flags == ComCtl32.HHT.ONDIVIDER)
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ private void UpdateSortedListView(MaskDescriptorComparer.SortType sortType)
// Since we need to pre-process each item before inserting it in the ListView, it is better to remove all items
// from it first and then add the sorted ones back (no replace). Stop redrawing while we change the list.

User32.SendMessageW(_listViewCannedMasks, User32.WM.SETREDRAW, PARAM.FromBool(false));
User32.SendMessageW(_listViewCannedMasks, User32.WM.SETREDRAW, (nint)BOOL.FALSE);

try
{
Expand Down Expand Up @@ -518,7 +518,7 @@ private void UpdateSortedListView(MaskDescriptorComparer.SortType sortType)
finally
{
// Resume redraw.
User32.SendMessageW(_listViewCannedMasks, User32.WM.SETREDRAW, PARAM.FromBool(true));
User32.SendMessageW(_listViewCannedMasks, User32.WM.SETREDRAW, (nint)BOOL.TRUE);
_listViewCannedMasks.Invalidate();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -891,17 +891,13 @@ public void DoOleDragDrop(DragEventArgs de)

try
{
name = null;
if (comp.Site != null)
{
name = comp.Site.Name;
}
name = comp.Site?.Name;

Control oldDesignerControl = null;
if (updateLocation)
{
oldDesignerControl = client.GetDesignerControl();
User32.SendMessageW(oldDesignerControl.Handle, User32.WM.SETREDRAW);
User32.SendMessageW(oldDesignerControl.Handle, User32.WM.SETREDRAW, (nint)BOOL.FALSE);
}

Point dropPt = client.GetDesignerControl().PointToClient(new Point(de.X, de.Y));
Expand Down Expand Up @@ -950,10 +946,10 @@ public void DoOleDragDrop(DragEventArgs de)
}
}

if (oldDesignerControl != null)
if (oldDesignerControl is not null)
{
//((ComponentDataObject)dataObj).ShowControls();
User32.SendMessageW(oldDesignerControl.Handle, User32.WM.SETREDRAW, (IntPtr)1);
User32.SendMessageW(oldDesignerControl.Handle, User32.WM.SETREDRAW, (nint)BOOL.TRUE);
oldDesignerControl.Invalidate(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ protected override bool GetHitTest(Point pt)
return true;
}

// The scroll bars on a form are "live"
//
// The scroll bars on a form are "live".
ScrollableControl f = (ScrollableControl)Control;
if (f.IsHandleCreated && f.AutoScroll)
{
int hitTest = (int)(long)User32.SendMessageW(f.Handle, User32.WM.NCHITTEST, IntPtr.Zero, PARAM.FromLowHigh(pt.X, pt.Y));
int hitTest = (int)User32.SendMessageW(f.Handle, User32.WM.NCHITTEST, 0, PARAM.FromLowHigh(pt.X, pt.Y));
if (hitTest == (int)User32.HT.VSCROLL || hitTest == (int)User32.HT.HSCROLL)
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,12 +909,12 @@ private void EnterInSituEdit()
tb.KeyDown += new KeyEventHandler(OnKeyDown);
tb.SelectAll();
Control baseComponent = null;
if (_designerHost != null)
if (_designerHost is not null)
{
baseComponent = (Control)_designerHost.RootComponent;
User32.SendMessageW(baseComponent.Handle, User32.WM.SETREDRAW, IntPtr.Zero, IntPtr.Zero);
User32.SendMessageW(baseComponent.Handle, User32.WM.SETREDRAW, (nint)BOOL.FALSE);
tb.Focus();
User32.SendMessageW(baseComponent.Handle, User32.WM.SETREDRAW, (IntPtr)1, IntPtr.Zero);
User32.SendMessageW(baseComponent.Handle, User32.WM.SETREDRAW, (nint)BOOL.TRUE);
}
}
finally
Expand Down Expand Up @@ -994,15 +994,13 @@ internal void FocusEditor(ToolStripItem currentItem)
/// </summary>
private void FocusForm()
{
if (_component.Site.GetService(typeof(ISplitWindowService)) is DesignerFrame designerFrame)
if (_component.Site.GetService(typeof(ISplitWindowService)) is DesignerFrame designerFrame
&& _designerHost is not null)
{
if (_designerHost != null)
{
Control baseComponent = (Control)_designerHost.RootComponent;
User32.SendMessageW(baseComponent.Handle, User32.WM.SETREDRAW, IntPtr.Zero, IntPtr.Zero);
designerFrame.Focus();
User32.SendMessageW(baseComponent.Handle, User32.WM.SETREDRAW, (IntPtr)1, IntPtr.Zero);
}
Control baseComponent = (Control)_designerHost.RootComponent;
User32.SendMessageW(baseComponent.Handle, User32.WM.SETREDRAW, (nint)BOOL.FALSE);
designerFrame.Focus();
User32.SendMessageW(baseComponent.Handle, User32.WM.SETREDRAW, (nint)BOOL.TRUE);
}
}

Expand Down
Loading

0 comments on commit 834d0a0

Please sign in to comment.