Skip to content

Commit 403ef7f

Browse files
committed
Merge pull request #920 from rassilon/issue918
Fix #918 by adding extension method to cast from IntPtr to Int32
2 parents d516b67 + 48c2550 commit 403ef7f

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

CefSharp.WinForms/Internals/ParentFormMessageInterceptor.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Drawing;
77
using System.Runtime.InteropServices;
88
using System.Windows.Forms;
9+
using CefSharp.Internals;
910

1011
namespace CefSharp.WinForms.Internals
1112
{
@@ -149,8 +150,14 @@ protected override void WndProc(ref Message m)
149150
}
150151
case NativeMethods.WM_MOVE:
151152
{
152-
x = (m.LParam.ToInt32() & 0xffff);
153-
y = ((m.LParam.ToInt32() >> 16) & 0xffff);
153+
// Convert IntPtr into 32bit int safely without
154+
// exceptions:
155+
int dwLParam = m.LParam.CastToInt32();
156+
157+
// Extract coordinates from lo/hi word:
158+
x = dwLParam & 0xffff;
159+
y = (dwLParam >> 16) & 0xffff;
160+
154161
isMovingMessage = true;
155162
break;
156163
}

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<Compile Include="CefFileDialogMode.cs" />
9393
<Compile Include="DefaultResourceHandlerFactory.cs" />
9494
<Compile Include="IJavascriptCallback.cs" />
95+
<Compile Include="Internals\IntPtrExtensions.cs" />
9596
<Compile Include="Internals\JavascriptCallbackProxy.cs" />
9697
<Compile Include="Internals\JavascriptCallback.cs" />
9798
<Compile Include="Internals\JavascriptCallbackEndpointBehavior.cs" />
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace CefSharp.Internals
7+
{
8+
public static class IntPtrExtensions
9+
{
10+
/// <summary>
11+
/// Do an unchecked conversion from IntPtr to int
12+
/// so overflow exceptions don't get thrown.
13+
/// </summary>
14+
/// <param name="intPtr"></param>
15+
/// <returns></returns>
16+
public static int CastToInt32(this IntPtr intPtr)
17+
{
18+
return unchecked((int)intPtr.ToInt64());
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)