Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 76c8587

Browse files
authored
Fix ServiceController name population perf (#32072)
* Fix ServiceController name population perf * Split tests * Remove dead field * Remove new use of DangerousGetHandle * SafeHandle all the things! * VSB #1 * VSB #2 * Fix GLE * Initialize machineName in ctor * Test for empty name ex * Null names * Inadvertent edit * Unix build * Move interop into class * Reverse SafeHandle for HAllocGlobal * Fix tests * Disable test for NETFX * CR feedback * Pattern matching on VSB * Direct call * typo
1 parent 901ff58 commit 76c8587

23 files changed

+472
-314
lines changed

src/Common/src/CoreLib/System/Text/ValueStringBuilder.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,22 @@ public void EnsureCapacity(int capacity)
4848
Grow(capacity - _chars.Length);
4949
}
5050

51+
/// <summary>
52+
/// Get a pinnable reference to the builder.
53+
/// Does not ensure there is a null char after <see cref="Length"/>
54+
/// This overload is pattern matched in the C# 7.3+ compiler so you can omit
55+
/// the explicit method call, and write eg "fixed (char* c = builder)"
56+
/// </summary>
57+
public ref char GetPinnableReference()
58+
{
59+
return ref MemoryMarshal.GetReference(_chars);
60+
}
61+
5162
/// <summary>
5263
/// Get a pinnable reference to the builder.
5364
/// </summary>
5465
/// <param name="terminate">Ensures that the builder has a null char after <see cref="Length"/></param>
55-
public ref char GetPinnableReference(bool terminate = false)
66+
public ref char GetPinnableReference(bool terminate)
5667
{
5768
if (terminate)
5869
{

src/Common/src/Interop/Windows/Interop.Errors.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ internal partial class Errors
5757
internal const int ERROR_IO_INCOMPLETE = 0x3E4;
5858
internal const int ERROR_IO_PENDING = 0x3E5;
5959
internal const int ERROR_NO_TOKEN = 0x3f0;
60+
internal const int ERROR_SERVICE_DOES_NOT_EXIST = 0x424;
6061
internal const int ERROR_DLL_INIT_FAILED = 0x45A;
6162
internal const int ERROR_COUNTER_TIMEOUT = 0x461;
6263
internal const int ERROR_NO_ASSOCIATION = 0x483;

src/Common/src/Interop/Windows/advapi32/Interop.ChangeServiceConfig2.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.Win32.SafeHandles;
56
using System;
67
using System.Runtime.InteropServices;
78

@@ -10,9 +11,9 @@ internal partial class Interop
1011
internal partial class Advapi32
1112
{
1213
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
13-
public static extern bool ChangeServiceConfig2(IntPtr serviceHandle, uint infoLevel, ref SERVICE_DESCRIPTION serviceDesc);
14+
public static extern bool ChangeServiceConfig2(SafeServiceHandle serviceHandle, uint infoLevel, ref SERVICE_DESCRIPTION serviceDesc);
1415

1516
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
16-
public static extern bool ChangeServiceConfig2(IntPtr serviceHandle, uint infoLevel, ref SERVICE_DELAYED_AUTOSTART_INFO serviceDesc);
17+
public static extern bool ChangeServiceConfig2(SafeServiceHandle serviceHandle, uint infoLevel, ref SERVICE_DELAYED_AUTOSTART_INFO serviceDesc);
1718
}
1819
}

src/Common/src/Interop/Windows/advapi32/Interop.ControlService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.Win32.SafeHandles;
56
using System;
67
using System.Runtime.InteropServices;
78

@@ -10,7 +11,7 @@ internal partial class Interop
1011
internal partial class Advapi32
1112
{
1213
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
13-
internal extern unsafe static bool ControlService(IntPtr serviceHandle, int control, SERVICE_STATUS* pStatus);
14+
internal extern unsafe static bool ControlService(SafeServiceHandle serviceHandle, int control, SERVICE_STATUS* pStatus);
1415

1516
}
1617
}

src/Common/src/Interop/Windows/advapi32/Interop.CreateService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.Win32.SafeHandles;
56
using System;
67
using System.Runtime.InteropServices;
78

@@ -10,7 +11,7 @@ internal partial class Interop
1011
internal partial class Advapi32
1112
{
1213
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
13-
public extern static IntPtr CreateService(IntPtr databaseHandle, string serviceName, string displayName, int access, int serviceType,
14+
public extern static IntPtr CreateService(SafeServiceHandle databaseHandle, string serviceName, string displayName, int access, int serviceType,
1415
int startType, int errorControl, string binaryPath, string loadOrderGroup, IntPtr pTagId, string dependencies,
1516
string servicesStartName, string password);
1617

src/Common/src/Interop/Windows/advapi32/Interop.DeleteService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.Win32.SafeHandles;
56
using System;
67
using System.Runtime.InteropServices;
78

@@ -10,6 +11,6 @@ internal partial class Interop
1011
internal partial class Advapi32
1112
{
1213
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
13-
public extern static bool DeleteService(IntPtr serviceHandle);
14+
public extern static bool DeleteService(SafeServiceHandle serviceHandle);
1415
}
1516
}

src/Common/src/Interop/Windows/advapi32/Interop.EnumDependentServices.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.Win32.SafeHandles;
56
using System;
67
using System.Runtime.InteropServices;
78

@@ -11,7 +12,7 @@ internal partial class Advapi32
1112
{
1213
[DllImport(Libraries.Advapi32, EntryPoint = "EnumDependentServicesW", CharSet = CharSet.Unicode, SetLastError = true)]
1314
internal extern static bool EnumDependentServices(
14-
IntPtr serviceHandle,
15+
SafeServiceHandle serviceHandle,
1516
int serviceState,
1617
IntPtr bufferOfENUM_SERVICE_STATUS,
1718
int bufSize,

src/Common/src/Interop/Windows/advapi32/Interop.EnumServicesStatusEx.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.Win32.SafeHandles;
56
using System;
67
using System.Runtime.InteropServices;
78

@@ -11,7 +12,7 @@ internal partial class Advapi32
1112
{
1213
[DllImport(Libraries.Advapi32, EntryPoint = "EnumServicesStatusExW", CharSet = CharSet.Unicode, SetLastError = true)]
1314
internal extern static bool EnumServicesStatusEx(
14-
IntPtr databaseHandle,
15+
SafeServiceHandle databaseHandle,
1516
int infolevel,
1617
int serviceType,
1718
int serviceState,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Win32.SafeHandles;
6+
using System;
7+
using System.Buffers;
8+
using System.ComponentModel;
9+
using System.Runtime.InteropServices;
10+
using System.Text;
11+
12+
internal partial class Interop
13+
{
14+
internal partial class Advapi32
15+
{
16+
[DllImport(Libraries.Advapi32, EntryPoint = "GetServiceDisplayNameW", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
17+
internal static extern unsafe bool GetServiceDisplayName(SafeServiceHandle SCMHandle, string serviceName, char* displayName, ref int displayNameLength);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Win32.SafeHandles;
6+
using System;
7+
using System.Buffers;
8+
using System.ComponentModel;
9+
using System.Runtime.InteropServices;
10+
using System.Text;
11+
12+
internal partial class Interop
13+
{
14+
internal partial class Advapi32
15+
{
16+
[DllImport(Libraries.Advapi32, EntryPoint = "GetServiceKeyNameW", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
17+
internal static extern unsafe bool GetServiceKeyName(SafeServiceHandle SCMHandle, string displayName, char* KeyName, ref int KeyNameLength);
18+
}
19+
}

0 commit comments

Comments
 (0)