Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for possible System.OverflowException with IntPtr.ToInt32() on Win 8 or Win Server 2012 #1996

Merged
merged 6 commits into from
Jun 21, 2015
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion MahApps.Metro/Behaviours/BorderlessWindowBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ private bool _ModifyStyle(Standard.WS removeStyle, Standard.WS addStyle)
{
return false;
}
var dwStyle = (Standard.WS)Standard.NativeMethods.GetWindowLongPtr(this.handle, Standard.GWL.STYLE).ToInt32();
var intPtr = Standard.NativeMethods.GetWindowLongPtr(this.handle, Standard.GWL.STYLE);
var dwStyle = (Standard.WS)(uint)(Environment.Is64BitProcess ? intPtr.ToInt64() : intPtr.ToInt32());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're casting the result of ToInt64() back to a uint so you'll still end up getting an OverflowException?

var dwNewStyle = (dwStyle & ~removeStyle) | addStyle;
if (dwStyle == dwNewStyle) {
return false;
Expand Down
13 changes: 8 additions & 5 deletions MahApps.Metro/Microsoft.Windows.Shell/WindowChromeWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ private IntPtr _HandleSetTextOrIcon(WM uMsg, IntPtr wParam, IntPtr lParam, out b
private IntPtr _HandleRestoreWindow(WM uMsg, IntPtr wParam, IntPtr lParam, out bool handled)
{
WINDOWPLACEMENT wpl = NativeMethods.GetWindowPlacement(_hwnd);
if (SC.RESTORE == (SC)wParam.ToInt32() && wpl.showCmd == SW.SHOWMAXIMIZED && _MinimizeAnimation)
var sc = (SC)(Environment.Is64BitProcess ? wParam.ToInt64() : wParam.ToInt32());
if (SC.RESTORE == sc && wpl.showCmd == SW.SHOWMAXIMIZED && _MinimizeAnimation)
{
bool modified = _ModifyStyle(WS.DLGFRAME, 0);

Expand Down Expand Up @@ -742,7 +743,7 @@ private IntPtr _HandleNCRButtonUp(WM uMsg, IntPtr wParam, IntPtr lParam, out boo
{
// Emulate the system behavior of clicking the right mouse button over the caption area
// to bring up the system menu.
if (HT.CAPTION == (HT)wParam.ToInt32())
if (HT.CAPTION == (HT)(Environment.Is64BitProcess ? wParam.ToInt64() : wParam.ToInt32()))
{
SystemCommands.ShowSystemMenuPhysicalCoordinates(_window, new Point(Utility.GET_X_LPARAM(lParam), Utility.GET_Y_LPARAM(lParam)));
}
Expand All @@ -759,7 +760,7 @@ private IntPtr _HandleSize(WM uMsg, IntPtr wParam, IntPtr lParam, out bool handl
// maximized. Not forcing this update will eventually cause the
// default caption to be drawn.
WindowState? state = null;
if (wParam.ToInt32() == SIZE_MAXIMIZED)
if ((Environment.Is64BitProcess ? wParam.ToInt64() : wParam.ToInt32()) == SIZE_MAXIMIZED)
{
state = WindowState.Maximized;
}
Expand Down Expand Up @@ -1022,7 +1023,8 @@ private IntPtr _HandleMove(WM uMsg, IntPtr wParam, IntPtr lParam, out bool handl
private bool _ModifyStyle(WS removeStyle, WS addStyle)
{
Assert.IsNotDefault(_hwnd);
var dwStyle = (WS)NativeMethods.GetWindowLongPtr(_hwnd, GWL.STYLE).ToInt32();
var intPtr = NativeMethods.GetWindowLongPtr(_hwnd, GWL.STYLE);
var dwStyle = (WS)(uint)(Environment.Is64BitProcess ? intPtr.ToInt64() : intPtr.ToInt32());
var dwNewStyle = (dwStyle & ~removeStyle) | addStyle;
if (dwStyle == dwNewStyle)
{
Expand Down Expand Up @@ -1081,7 +1083,8 @@ private void _UpdateSystemMenu(WindowState? assumeState)
IntPtr hmenu = NativeMethods.GetSystemMenu(_hwnd, false);
if (IntPtr.Zero != hmenu)
{
var dwStyle = (WS)NativeMethods.GetWindowLongPtr(_hwnd, GWL.STYLE).ToInt32();
var intPtr = NativeMethods.GetWindowLongPtr(_hwnd, GWL.STYLE);
var dwStyle = (WS)(uint)(Environment.Is64BitProcess ? intPtr.ToInt64() : intPtr.ToInt32());

bool canMinimize = Utility.IsFlagSet((int)dwStyle, (int)WS.MINIMIZEBOX);
bool canMaximize = Utility.IsFlagSet((int)dwStyle, (int)WS.MAXIMIZEBOX);
Expand Down
2 changes: 1 addition & 1 deletion MahApps.Metro/Native/UnsafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ internal struct Win32Point
// See: http://stackoverflow.com/questions/7913325/win-api-in-c-get-hi-and-low-word-from-intptr/7913393#7913393
internal static Point GetPoint(IntPtr ptr)
{
uint xy = unchecked(IntPtr.Size == 8 ? (uint)ptr.ToInt64() : (uint)ptr.ToInt32());
uint xy = unchecked(Environment.Is64BitProcess ? (uint)ptr.ToInt64() : (uint)ptr.ToInt32());
int x = unchecked((short)xy);
int y = unchecked((short)(xy >> 16));
return new Point(x, y);
Expand Down
2 changes: 1 addition & 1 deletion samples/MetroDemo/MetroDemo.NET45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
<Prefer32Bit>false</Prefer32Bit>
<OutputPath>bin\NET45\Release\</OutputPath>
<IntermediateOutputPath>obj\NET45\Release\</IntermediateOutputPath>
</PropertyGroup>
Expand Down