Skip to content

Commit 3ccd6f5

Browse files
Use generic Marshal.PtrToStructure (#4917)
* Use generic Marshal.PtrToStructure * Use generic Marshal.PtrToStructure for new UIA events
1 parent 7fa90f6 commit 3ccd6f5

File tree

15 files changed

+36
-77
lines changed

15 files changed

+36
-77
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DpiChangedEventArgs.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// different DPI, or the dpi of the current monitor changes.
1111

1212
using System.ComponentModel;
13+
using System.Runtime.InteropServices;
1314
using System.Security;
1415
using System.Threading;
1516
using System.Windows.Interop;
@@ -48,7 +49,7 @@ internal HwndDpiChangedEventArgs(double oldDpiX, double oldDpiY, double newDpiX,
4849
{
4950
OldDpi = new DpiScale(oldDpiX / DpiUtil.DefaultPixelsPerInch, oldDpiY / DpiUtil.DefaultPixelsPerInch);
5051
NewDpi = new DpiScale(newDpiX / DpiUtil.DefaultPixelsPerInch, newDpiY / DpiUtil.DefaultPixelsPerInch);
51-
NativeMethods.RECT suggestedRect = (NativeMethods.RECT)UnsafeNativeMethods.PtrToStructure(lParam, typeof(NativeMethods.RECT));
52+
NativeMethods.RECT suggestedRect = Marshal.PtrToStructure<NativeMethods.RECT>(lParam);
5253
this.SuggestedRect = new Rect((double)suggestedRect.left, (double)suggestedRect.top, (double)suggestedRect.Width, (double)suggestedRect.Height);
5354
}
5455

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputProcessorProfiles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ internal ArrayList InputLanguageList
160160
for (int i = 0; i < nCount; i++)
161161
{
162162
// Unmarshal each langid from short array.
163-
short langid = (short)Marshal.PtrToStructure((IntPtr)((Int64)langids + sizeOfShort * i), typeof(short));
163+
short langid = Marshal.PtrToStructure<short>((IntPtr)((Int64)langids + sizeOfShort * i));
164164
arrayLang.Add(new CultureInfo(langid));
165165
}
166166

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ private void Process_WM_WINDOWPOSCHANGING(UIElement rootUIElement, IntPtr hwnd,
13681368

13691369
// Get WINDOWPOS structure data from lParam; it contains information about the window's
13701370
// new size and position.
1371-
NativeMethods.WINDOWPOS windowPos = (NativeMethods.WINDOWPOS)UnsafeNativeMethods.PtrToStructure(lParam, typeof(NativeMethods.WINDOWPOS));
1371+
NativeMethods.WINDOWPOS windowPos = Marshal.PtrToStructure<NativeMethods.WINDOWPOS>(lParam);
13721372

13731373
bool sizeChanged = false;
13741374

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndTarget.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ private bool HandleDpiChangedMessage(IntPtr wParam, IntPtr lParam)
876876
if (oldDpi != newDpi)
877877
{
878878
var nativeRect =
879-
UnsafeNativeMethods.PtrToStructure<NativeMethods.RECT>(lParam);
879+
Marshal.PtrToStructure<NativeMethods.RECT>(lParam);
880880
var suggestedRect =
881881
new Rect(nativeRect.left, nativeRect.top, nativeRect.Width, nativeRect.Height);
882882

@@ -1870,7 +1870,7 @@ private void UpdateWindowPos(IntPtr lParam)
18701870
// size or position changed. If so, we need to pass this information to
18711871
// the render thread.
18721872
//
1873-
NativeMethods.WINDOWPOS windowPos = (NativeMethods.WINDOWPOS)UnsafeNativeMethods.PtrToStructure(lParam, typeof(NativeMethods.WINDOWPOS));
1873+
NativeMethods.WINDOWPOS windowPos = Marshal.PtrToStructure<NativeMethods.WINDOWPOS>(lParam);
18741874
bool isMove = (windowPos.flags & NativeMethods.SWP_NOMOVE) == 0;
18751875
bool isSize = (windowPos.flags & NativeMethods.SWP_NOSIZE) == 0;
18761876
bool positionChanged = (isMove || isSize);

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ImageSourceConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private unsafe Stream GetBitmapStream(byte[] rawData)
261261
//
262262
// http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q175261
263263
//
264-
OBJECTHEADER pHeader = (OBJECTHEADER)Marshal.PtrToStructure(addr, typeof(OBJECTHEADER));
264+
OBJECTHEADER pHeader = Marshal.PtrToStructure<OBJECTHEADER>(addr);
265265

266266
//
267267
// "PBrush" should be the 6 chars after position 12 as well.

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ int IWICBitmapSource.CopyPixels(IntPtr prc, int cbStride, int cbPixels, IntPtr p
18471847
}
18481848
else
18491849
{
1850-
rc = (Int32Rect)Marshal.PtrToStructure(prc, typeof(Int32Rect));
1850+
rc = Marshal.PtrToStructure<Int32Rect>(prc);
18511851
}
18521852

18531853
int rectHeight, rectWidth;

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Printing/printdlgexmarshaler.cs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,15 @@ out IntPtr devModeHandle
235235
//
236236
if (!Is64Bit())
237237
{
238-
NativeMethods.PRINTDLGEX32 pdex = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure(
239-
unmanagedBuffer,
240-
typeof(NativeMethods.PRINTDLGEX32));
238+
NativeMethods.PRINTDLGEX32 pdex = Marshal.PtrToStructure<NativeMethods.PRINTDLGEX32>(unmanagedBuffer);
241239
devModeHandle = pdex.hDevMode;
242240
devNamesHandle = pdex.hDevNames;
243241
flags = pdex.Flags;
244242
pageRangePtr = pdex.lpPageRanges;
245243
}
246244
else
247245
{
248-
NativeMethods.PRINTDLGEX64 pdex = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure(
249-
unmanagedBuffer,
250-
typeof(NativeMethods.PRINTDLGEX64));
246+
NativeMethods.PRINTDLGEX64 pdex = Marshal.PtrToStructure<NativeMethods.PRINTDLGEX64>(unmanagedBuffer);
251247
devModeHandle = pdex.hDevMode;
252248
devNamesHandle = pdex.hDevNames;
253249
flags = pdex.Flags;
@@ -261,9 +257,7 @@ out IntPtr devModeHandle
261257
if (((flags & NativeMethods.PD_PAGENUMS) == NativeMethods.PD_PAGENUMS) &&
262258
(pageRangePtr != IntPtr.Zero))
263259
{
264-
NativeMethods.PRINTPAGERANGE pageRangeStruct = (NativeMethods.PRINTPAGERANGE)Marshal.PtrToStructure(
265-
pageRangePtr,
266-
typeof(NativeMethods.PRINTPAGERANGE));
260+
NativeMethods.PRINTPAGERANGE pageRangeStruct = Marshal.PtrToStructure<NativeMethods.PRINTPAGERANGE>(pageRangePtr);
267261

268262
pageRange = new PageRange((int)pageRangeStruct.nFromPage, (int)pageRangeStruct.nToPage);
269263
}
@@ -282,9 +276,7 @@ out IntPtr devModeHandle
282276
{
283277
pDevNames = UnsafeNativeMethods.GlobalLock(devNamesHandle);
284278

285-
NativeMethods.DEVNAMES devNames = (NativeMethods.DEVNAMES)Marshal.PtrToStructure(
286-
pDevNames,
287-
typeof(NativeMethods.DEVNAMES));
279+
NativeMethods.DEVNAMES devNames = Marshal.PtrToStructure<NativeMethods.DEVNAMES>(pDevNames);
288280
int devNamesOffset = checked(devNames.wDeviceOffset * Marshal.SystemDefaultCharSize);
289281
printerName = Marshal.PtrToStringAuto(pDevNames + devNamesOffset);
290282
}
@@ -380,9 +372,7 @@ string printQueueName
380372
{
381373
pDevMode = UnsafeNativeMethods.GlobalLock(devModeHandle);
382374

383-
NativeMethods.DEVMODE devMode = (NativeMethods.DEVMODE)Marshal.PtrToStructure(
384-
pDevMode,
385-
typeof(NativeMethods.DEVMODE));
375+
NativeMethods.DEVMODE devMode = Marshal.PtrToStructure<NativeMethods.DEVMODE>(pDevMode);
386376
devModeData = new byte[devMode.dmSize + devMode.dmDriverExtra];
387377
Marshal.Copy(pDevMode, devModeData, 0, devModeData.Length);
388378
}
@@ -428,16 +418,12 @@ IntPtr unmanagedBuffer
428418
//
429419
if (!Is64Bit())
430420
{
431-
NativeMethods.PRINTDLGEX32 pdex = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure(
432-
unmanagedBuffer,
433-
typeof(NativeMethods.PRINTDLGEX32));
421+
NativeMethods.PRINTDLGEX32 pdex = Marshal.PtrToStructure<NativeMethods.PRINTDLGEX32>(unmanagedBuffer);
434422
result = pdex.dwResultAction;
435423
}
436424
else
437425
{
438-
NativeMethods.PRINTDLGEX64 pdex = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure(
439-
unmanagedBuffer,
440-
typeof(NativeMethods.PRINTDLGEX64));
426+
NativeMethods.PRINTDLGEX64 pdex = Marshal.PtrToStructure<NativeMethods.PRINTDLGEX64>(unmanagedBuffer);
441427
result = pdex.dwResultAction;
442428
}
443429

@@ -668,18 +654,14 @@ IntPtr unmanagedBuffer
668654
//
669655
if (!Is64Bit())
670656
{
671-
NativeMethods.PRINTDLGEX32 pdex = (NativeMethods.PRINTDLGEX32)Marshal.PtrToStructure(
672-
unmanagedBuffer,
673-
typeof(NativeMethods.PRINTDLGEX32));
657+
NativeMethods.PRINTDLGEX32 pdex = Marshal.PtrToStructure<NativeMethods.PRINTDLGEX32>(unmanagedBuffer);
674658
devModeHandle = pdex.hDevMode;
675659
devNamesHandle = pdex.hDevNames;
676660
pageRangePtr = pdex.lpPageRanges;
677661
}
678662
else
679663
{
680-
NativeMethods.PRINTDLGEX64 pdex = (NativeMethods.PRINTDLGEX64)Marshal.PtrToStructure(
681-
unmanagedBuffer,
682-
typeof(NativeMethods.PRINTDLGEX64));
664+
NativeMethods.PRINTDLGEX64 pdex = Marshal.PtrToStructure<NativeMethods.PRINTDLGEX64>(unmanagedBuffer);
683665
devModeHandle = pdex.hDevMode;
684666
devNamesHandle = pdex.hDevNames;
685667
pageRangePtr = pdex.lpPageRanges;

src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/FileDialog.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ protected override IntPtr HookProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr l
589589
// }
590590
//
591591
// Convert the pointer to our OFNOTIFY stored in lparam to an object using PtrToStructure.
592-
NativeMethods.OFNOTIFY notify = (NativeMethods.OFNOTIFY)UnsafeNativeMethods.PtrToStructure(lParam, typeof(NativeMethods.OFNOTIFY));
592+
NativeMethods.OFNOTIFY notify = Marshal.PtrToStructure<NativeMethods.OFNOTIFY>(lParam);
593593

594594
// WM_NOTIFY indicates that the dialog is sending us a notification message.
595595
// notify.hdr_code is an int defining which notification is being received.
@@ -623,8 +623,7 @@ protected override IntPtr HookProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr l
623623

624624
// Retrieve the OPENFILENAME structure from the OFNOTIFY structure
625625
// so we can access the CharBuffer inside it.
626-
NativeMethods.OPENFILENAME_I ofn = (NativeMethods.OPENFILENAME_I)
627-
UnsafeNativeMethods.PtrToStructure(notify.lpOFN, typeof(NativeMethods.OPENFILENAME_I));
626+
NativeMethods.OPENFILENAME_I ofn = Marshal.PtrToStructure<NativeMethods.OPENFILENAME_I>(notify.lpOFN);
628627

629628

630629
// Get the buffer size required to store the selected file names.
@@ -1162,7 +1161,7 @@ internal string[] FileNamesInternal
11621161
/// </returns>
11631162
private bool DoFileOk(IntPtr lpOFN)
11641163
{
1165-
NativeMethods.OPENFILENAME_I ofn = (NativeMethods.OPENFILENAME_I)UnsafeNativeMethods.PtrToStructure(lpOFN, typeof(NativeMethods.OPENFILENAME_I));
1164+
NativeMethods.OPENFILENAME_I ofn = Marshal.PtrToStructure<NativeMethods.OPENFILENAME_I>(lpOFN);
11661165

11671166
// While processing the results we get from the OPENFILENAME struct,
11681167
// we will adjust several properties of our own class to reflect the

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ImmComposition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ private IntPtr OnWmImeRequest_ReconvertString(IntPtr lParam, ref bool handled, b
13581358
string surrounding = GetSurroundingText(range, out offsetStart);
13591359

13601360
// Create RECONVERTSTRING structure from lParam.
1361-
NativeMethods.RECONVERTSTRING reconv = (NativeMethods.RECONVERTSTRING)Marshal.PtrToStructure(lParam, typeof(NativeMethods.RECONVERTSTRING));
1361+
NativeMethods.RECONVERTSTRING reconv = Marshal.PtrToStructure<NativeMethods.RECONVERTSTRING>(lParam);
13621362

13631363
reconv.dwSize = requestSize;
13641364
reconv.dwVersion = 0; // must be 0
@@ -1520,7 +1520,7 @@ private IntPtr OnWmImeRequest_ConfirmReconvertString(IntPtr lParam, ref bool han
15201520
return IntPtr.Zero;
15211521
}
15221522

1523-
NativeMethods.RECONVERTSTRING reconv = (NativeMethods.RECONVERTSTRING)Marshal.PtrToStructure(lParam, typeof(NativeMethods.RECONVERTSTRING));
1523+
NativeMethods.RECONVERTSTRING reconv = Marshal.PtrToStructure<NativeMethods.RECONVERTSTRING>(lParam);
15241524

15251525
// If the entire string in RECONVERTSTRING has been changed, we don't handle it.
15261526
if (_reconv.dwStrLen != reconv.dwStrLen)

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Shell/WindowChromeWorker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ private IntPtr _HandleNCCalcSize(WM uMsg, IntPtr wParam, IntPtr lParam, out bool
588588
#else
589589
Thickness windowResizeBorderThicknessDevice = DpiHelper.LogicalThicknessToDevice(SystemParameters2.Current.WindowResizeBorderThickness, dpi.DpiScaleX, dpi.DpiScaleY);
590590
#endif
591-
var rcClientArea = (RECT)Marshal.PtrToStructure(lParam, typeof(RECT));
591+
var rcClientArea = Marshal.PtrToStructure<RECT>(lParam);
592592
if (Utility.IsFlagSet((int)_chromeInfo.NonClientFrameEdges, (int)NonClientFrameEdges.Top))
593593
{
594594
rcClientArea.Top += (int)windowResizeBorderThicknessDevice.Top;
@@ -751,7 +751,7 @@ private IntPtr _HandleWindowPosChanged(WM uMsg, IntPtr wParam, IntPtr lParam, ou
751751
// reliably use it to react to the window being shown or hidden.
752752

753753
Assert.IsNotDefault(lParam);
754-
var wp = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));
754+
var wp = Marshal.PtrToStructure<WINDOWPOS>(lParam);
755755

756756
// We only care to take action when the window dimensions are changing.
757757
// Otherwise, we may get a StackOverflowException.

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Standard/MessageWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private static IntPtr _WndProc(IntPtr hwnd, WM msg, IntPtr wParam, IntPtr lParam
150150

151151
if (msg == WM.CREATE)
152152
{
153-
var createStruct = (CREATESTRUCT)Marshal.PtrToStructure(lParam, typeof(CREATESTRUCT));
153+
var createStruct = Marshal.PtrToStructure<CREATESTRUCT>(lParam);
154154
GCHandle gcHandle = GCHandle.FromIntPtr(createStruct.lpCreateParams);
155155
hwndWrapper = (MessageWindow)gcHandle.Target;
156156
s_windowLookup.Add(hwnd, hwndWrapper);

src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/documents/PeoplePickerWrapper.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,7 @@ internal DsObjectNamesWrapper(System.IO.MemoryStream dataStream)
331331

332332
//Get a DsObjectNames structure out of the pointer we
333333
//were handed.
334-
_dsObjectNames =
335-
(UnsafeNativeMethods.DsObjectNames)Marshal.PtrToStructure(
336-
_ptrToDsObjectNames, typeof(UnsafeNativeMethods.DsObjectNames));
337-
334+
_dsObjectNames = Marshal.PtrToStructure<UnsafeNativeMethods.DsObjectNames>(_ptrToDsObjectNames);
338335
}
339336

340337
/// <summary>
@@ -456,12 +453,9 @@ private UnsafeNativeMethods.DsObject GetDsObjectForIndex(int index)
456453
index * _sizeOfDsObject);
457454

458455
//Marshal that to a DsObject structure.
459-
UnsafeNativeMethods.DsObject dsObject =
460-
(UnsafeNativeMethods.DsObject)Marshal.PtrToStructure(offset,
461-
typeof(UnsafeNativeMethods.DsObject));
456+
UnsafeNativeMethods.DsObject dsObject = Marshal.PtrToStructure<UnsafeNativeMethods.DsObject>(offset);
462457

463458
return dsObject;
464-
465459
}
466460

467461
/// <summary>

src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/NativeMethodsCLR.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4628,11 +4628,11 @@ public object ToObject() {
46284628
return (val != IntPtr.Zero);
46294629

46304630
case (int)tagVT.VT_VARIANT:
4631-
VARIANT varStruct = (VARIANT)UnsafeNativeMethods.PtrToStructure(val, typeof(VARIANT));
4631+
VARIANT varStruct = Marshal.PtrToStructure<VARIANT>(val);
46324632
return varStruct.ToObject();
46334633
case (int)tagVT.VT_CLSID:
46344634
//Debug.Fail("PtrToStructure will not work with System.Guid...");
4635-
Guid guid =(Guid)UnsafeNativeMethods.PtrToStructure(val, typeof(Guid));
4635+
Guid guid = Marshal.PtrToStructure<Guid>(val);
46364636
return guid;
46374637

46384638
case (int)tagVT.VT_FILETIME:

src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsCLR.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,6 @@ namespace MS.Win32
4646

4747
internal partial class UnsafeNativeMethods {
4848

49-
// For some reason "PtrToStructure" requires super high permission.
50-
public static object PtrToStructure(IntPtr lparam, Type cls) {
51-
return Marshal.PtrToStructure(lparam, cls);
52-
}
53-
54-
/// <summary>
55-
/// Generic PtrToStructure(T) - because Marshal.PtrToStructure(T) doesn't
56-
/// seem to be available in our build environment.
57-
/// </summary>
58-
/// <typeparam name="T"></typeparam>
59-
/// <param name="lParam"></param>
60-
/// <returns></returns>
61-
public static T PtrToStructure<T>(IntPtr lParam)
62-
{
63-
return (T)Marshal.PtrToStructure(lParam, typeof(T));
64-
}
65-
6649
// For some reason "StructureToPtr" requires super high permission.
6750
public static void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld)
6851
{

0 commit comments

Comments
 (0)