8000 replace marshal.sizeof(...) calls for primitive types with sizeof con… · dotnet/wpf@d669eb7 · GitHub
[go: up one dir, main page]

Skip to content

Commit d669eb7

Browse files
committed
replace marshal.sizeof(...) calls for primitive types with sizeof constants
1 parent e8e3875 commit d669eb7

File tree

15 files changed

+47
-73
lines changed

15 files changed

+47
-73
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/GestureRecognizer/NativeRecognizer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ StylusPointDescription reformatDescription
505505
Debug.Assert(propertyGuids.Length == StylusPointDescription.RequiredCountOfProperties);
506506

507507
// Get the packet description
508-
packetDescription.cbPacketSize = (uint)(propertyGuids.Length * Marshal.SizeOf(typeof(Int32)));
508+
packetDescription.cbPacketSize = (uint)(propertyGuids.Length * sizeof(Int32));
509509
packetDescription.cPacketProperties = (uint)propertyGuids.Length;
510510

511511
//
@@ -554,7 +554,7 @@ StylusPointDescription reformatDescription
554554
int packetCount = rawPackets.Length;
555555
if (packetCount != 0)
10000 556556
{
557-
countOfBytes = packetCount * Marshal.SizeOf(typeof(Int32));
557+
countOfBytes = packetCount * sizeof(Int32);
558558
packets = Marshal.AllocCoTaskMem(countOfBytes);
559559
Marshal.Copy(rawPackets, 0, packets, packetCount);
560560
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,10 @@ internal ArrayList InputLanguageList
155155

156156
ArrayList arrayLang = new ArrayList();
157157

158-
int sizeOfShort = Marshal.SizeOf(typeof(short));
159-
160158
for (int i = 0; i < nCount; i++)
161159
{
162160
// Unmarshal each langid from short array.
163-
short langid = Marshal.PtrToStructure<short>((IntPtr)((Int64)langids + sizeOfShort * i));
161+
short langid = Marshal.PtrToStructure<short>((IntPtr)((Int64)langids + sizeof(short) * i));
164162
arrayLang.Add(new CultureInfo(langid));
165163
}
166164

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
using SR=MS.Internal.PresentationCore.SR;
2424
using UnsafeNativeMethods=MS.Win32.PresentationCore.UnsafeNativeMethods;
25+
using System.Runtime.CompilerServices;
2526

2627
namespace System.Windows.Media
2728
{
@@ -169,7 +170,7 @@ internal PixelFormat(Guid guidPixelFormat)
169170
// Compare only the first 15 bytes of the GUID. If the first
170171
// 15 bytes match the WIC pixel formats, then the 16th byte
171172
// will be the format enum value.
172-
Debug.Assert(Marshal.SizeOf(typeof(Guid)) == 16);
173+
Debug.Assert(Unsafe.SizeOf<Guid>() == 16);
173174
int compareCount = 15;
174175

175176
bool fBuiltIn = true;

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/AppSecurityManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ private static bool IsZoneElevationSettingPrompt(Uri target)
350350
_secMgr.ProcessUrlAction(targetString,
351351
NativeMethods.URLACTION_FEATURE_ZONE_ELEVATION,
352352
(byte*)&policy,
353-
Marshal.SizeOf(typeof(int)),
353+
sizeof(int),
354354
null,
355355
0,
356356
NativeMethods.PUAF_NOUI,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,7 @@ IntPtr unmanagedBuffer
693693
bool
694694
Is64Bit()
695695
{
696-
IntPtr temp = IntPtr.Zero;
697-
return Marshal.SizeOf(temp) == 8;
696+
return IntPtr.Size == sizeof(long);
698697
}
699698

700699
/// <summary>

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/WindowsRuntime/Generated/WinRT/Marshalers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static unsafe MarshalerArray CreateMarshalerArray(string[] array)
148148
try
149149
{
150150
var length = array.Length;
151-
m._array = Marshal.AllocCoTaskMem(length * Marshal.SizeOf<IntPtr>());
151+
m._array = Marshal.AllocCoTaskMem(length * IntPtr.Size);
152152
m._marshalers = new MarshalString[length];
153153
var elements = (IntPtr*)m._array.ToPointer();
154154
for (int i = 0; i < length; i++)
@@ -215,7 +215,7 @@ public static unsafe (int length, IntPtr data) FromManagedArray(string[] array)
215215
try
216216
{
217217
var length = array.Length;
218-
data = Marshal.AllocCoTaskMem(length * Marshal.SizeOf<IntPtr>());
218+
data = Marshal.AllocCoTaskMem(length * IntPtr.Size);
219219
var elements = (IntPtr*)data;
220220
for (i = 0; i < length; i++)
221221
{

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ private void OnWmImeComposition(IntPtr hwnd, IntPtr lParam, ref bool handled)
430430
size = UnsafeNativeMethods.ImmGetCompositionString(new HandleRef(this, himc), NativeMethods.GCS_RESULTSTR, IntPtr.Zero, 0);
431431
if (size > 0)
432432
{
433-
result = new char[size / Marshal.SizeOf(typeof(short))];
433+
result = new char[size / sizeof(short)];
434434

435435
// 3rd param is out and contains actual result of this call.
436436
// suppress Presharp 6031.
@@ -447,7 +447,7 @@ private void OnWmImeComposition(IntPtr hwnd, IntPtr lParam, ref bool handled)
447447
size = UnsafeNativeMethods.ImmGetCompositionString(new HandleRef(this, himc), NativeMethods.GCS_COMPSTR, IntPtr.Zero, 0);
448448
if (size > 0)
449449
{
450-
composition = new char[size / Marshal.SizeOf(typeof(short))];
450+
composition = new char[size / sizeof(short)];
451451
// 3rd param is out and contains actual result of this call.
452452
// suppress Presharp 6031.
453453
#pragma warning suppress 6031
@@ -477,7 +477,7 @@ private void OnWmImeComposition(IntPtr hwnd, IntPtr lParam, ref bool handled)
477477
size = UnsafeNativeMethods.ImmGetCompositionString(new HandleRef(this, himc), NativeMethods.GCS_COMPATTR, IntPtr.Zero, 0);
478478
if (size > 0)
479479
{
480-
attributes = new byte[size / Marshal.SizeOf(typeof(byte))];
480+
attributes = new byte[size / sizeof(byte)];
481481
// 3rd param is out and contains actual result of this call.
482482
// suppress Presharp 6031.
483483
#pragma warning suppress 6031
@@ -1349,7 +1349,7 @@ private IntPtr OnWmImeRequest_ReconvertString(IntPtr lParam, ref bool handled, b
13491349

13501350
string target = range.Text;
13511351

1352-
int requestSize = Marshal.SizeOf(typeof(NativeMethods.RECONVERTSTRING)) + (target.Length * Marshal.SizeOf(typeof(short))) + ((_maxSrounding + 1) * Marshal.SizeOf(typeof(short)) * 2);
1352+
int requestSize = Marshal.SizeOf(typeof(NativeMethods.RECONVERTSTRING)) + (target.Length * sizeof(short)) + ((_maxSrounding + 1) * sizeof(short) * 2);
13531353
IntPtr lret = new IntPtr(requestSize);
13541354

13551355
if (lParam != IntPtr.Zero)
@@ -1365,9 +1365,9 @@ private IntPtr OnWmImeRequest_ReconvertString(IntPtr lParam, ref bool handled, b
13651365
reconv.dwStrLen = surrounding.Length; // in char count
13661366
reconv.dwStrOffset = Marshal.SizeOf(typeof(NativeMethods.RECONVERTSTRING)); // in byte count
13671367
reconv.dwCompStrLen = target.Length; // in char count
1368-
reconv.dwCompStrOffset = offsetStart * Marshal.SizeOf(typeof(short)); // in byte count
1368+
reconv.dwCompStrOffset = offsetStart * sizeof(short); // in byte count
13691369
reconv.dwTargetStrLen = target.Length; // in char count
1370-
reconv.dwTargetStrOffset = offsetStart * Marshal.SizeOf(typeof(short)); // in byte count
1370+
reconv.dwTargetStrOffset = offsetStart * sizeof(short); // in byte count
13711371

13721372
if (!fDocFeed)
13731373
{
@@ -1543,7 +1543,7 @@ private IntPtr OnWmImeRequest_ConfirmReconvertString(IntPtr lParam, ref bool han
15431543
ITextPointer start = range.Start.CreatePointer(LogicalDirection.Backward);
15441544

15451545
// Move the start point to new dwCompStrOffset.
1546-
start = MoveToNextCharPos(start, (reconv.dwCompStrOffset - _reconv.dwCompStrOffset) / Marshal.SizeOf(typeof(short)));
1546+
start = MoveToNextCharPos(start, (reconv.dwCompStrOffset - _reconv.dwCompStrOffset) / sizeof(short));
15471547
// Create the end position and move this as dwCompStrLen.
15481548
ITextPointer end = start.CreatePointer(LogicalDirection.Forward);
15491549
end = MoveToNextCharPos(end, reconv.dwCompStrLen);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void GetInputScopes(out IntPtr ppinputscopes, out int count)
6262
count = _inputScope.Names.Count;
6363
try
6464
{
65-
ppinputscopes = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Int32)) * count);
65+
ppinputscopes = Marshal.AllocCoTaskMem(sizeof(Int32) * count);
6666
}
6767
catch (OutOfMemoryException)
6868
{
@@ -72,12 +72,12 @@ public void GetInputScopes(out IntPtr ppinputscopes, out int count)
7272
for (int i = 0; i < count; i++)
7373
{
7474
Marshal.WriteInt32(ppinputscopes, offset, (Int32)((InputScopeName)_inputScope.Names[i]).NameValue);
75-
offset += Marshal.SizeOf(typeof(Int32));
75+
offset += sizeof(Int32);
7676
}
7777
}
7878
else
7979
{
80-
ppinputscopes = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Int32)) * 1);
80+
ppinputscopes = Marshal.AllocCoTaskMem(sizeof(Int32) * 1);
8181
Marshal.WriteInt32(ppinputscopes, (Int32)InputScopeNameValue.Default);
8282
count = 1;
8383
}
@@ -90,7 +90,7 @@ public int GetPhrase(out IntPtr ppbstrPhrases, out int count)
9090
count = _inputScope == null ? 0 : _inputScope.PhraseList.Count;
9191
try
9292
{
93-
ppbstrPhrases = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(IntPtr))*count);
93+
ppbstrPhrases = Marshal.AllocCoTaskMem(IntPtr.Size * count);
9494
}
9595
catch (OutOfMemoryException)
9696
{
@@ -111,13 +111,13 @@ public int GetPhrase(out IntPtr ppbstrPhrases, out int count)
111111
for (int j=0; j < i; j++)
112112
{
113113
Marshal.FreeBSTR(Marshal.ReadIntPtr(ppbstrPhrases, offset));
114-
offset += Marshal.SizeOf(typeof(IntPtr));
114+
offset += IntPtr.Size;
115115
}
116116
throw new COMException(SR.InputScopeAttribute_E_OUTOFMEMORY, NativeMethods.E_OUTOFMEMORY);
117117
}
118118

119119
Marshal.WriteIntPtr(ppbstrPhrases , offset, pbstr);
120-
offset += Marshal.SizeOf(typeof(IntPtr));
120+
offset += IntPtr.Size;
121121
}
122122

123123
return count > 0 ? NativeMethods.S_OK : NativeMethods.S_FALSE;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,7 +2403,7 @@ public static string[] CommandLineToArgvW(string cmdLine)
24032403

24042404
for (int i = 0; i < numArgs; i++)
24052405
{
2406-
IntPtr currArg = Marshal.ReadIntPtr(argv, i * Marshal.SizeOf(typeof(IntPtr)));
2406+
IntPtr currArg = Marshal.ReadIntPtr(argv, i * IntPtr.Size);
24072407
result[i] = Marshal.PtrToStringUni(currArg);
24082408
}
24092409

@@ -2628,15 +2628,15 @@ public static HRESULT DwmSetWindowAttributeSystemBackdropType(IntPtr hwnd, DWMSB
26282628
{
26292629
Assert.IsTrue(Utility.IsWindows11_22H2OrNewer);
26302630
var dwmWindowAttribute = (int)dwBackdropType;
2631-
return _DwmSetWindowAttribute(hwnd, DWMWA.SYSTEMBACKDROP_TYPE, ref dwmWindowAttribute, Marshal.SizeOf(typeof(int)));
2631+
return _DwmSetWindowAttribute(hwnd, DWMWA.SYSTEMBACKDROP_TYPE, ref dwmWindowAttribute, sizeof(int));
26322632
}
26332633

26342634
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
26352635
public static HRESULT DwmSetWindowAttributeUseImmersiveDarkMode(IntPtr hwnd, bool useImmersiveDarkMode)
26362636
{
26372637
Assert.IsTrue(Utility.IsWindows11_22H2OrNewer);
26382638
var pvAttribute = useImmersiveDarkMode ? 0x1 : 0x0;
2639-
return _DwmSetWindowAttribute(hwnd, DWMWA.USE_IMMERSIVE_DARK_MODE, ref pvAttribute, Marshal.SizeOf(typeof(int)));
2639+
return _DwmSetWindowAttribute(hwnd, DWMWA.USE_IMMERSIVE_DARK_MODE, ref pvAttribute, sizeof(int));
26402640
}
26412641

26422642
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Collections.Generic;
1010
using System.DirectoryServices;
1111
using System.Globalization;
12+
using System.Runtime.CompilerServices;
1213
using System.Runtime.InteropServices;
1314
using System.Runtime.InteropServices.ComTypes;
1415
using System.Windows.TrustUI;
@@ -513,7 +514,7 @@ public void Dispose()
513514
/// DsObjects array.
514515
/// </summary>
515516
private static readonly int _dsObjectArrayFieldOffset =
516-
Marshal.SizeOf(typeof(Guid)) + Marshal.SizeOf(typeof(UInt32));
517+
Unsafe.SizeOf<Guid>() + sizeof(UInt32);
517518

518519
/// <summary>
519520
/// The size of a DsObject.

0 commit comments

Comments
 (0)
0