Skip to content

Commit

Permalink
VSB #1
Browse files Browse the repository at this point in the history
  • Loading branch information
danmoseley committed Sep 2, 2018
1 parent 04d0c1a commit bd9db82
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/Common/src/CoreLib/System/Text/ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,36 @@
using System;
using System.Buffers;
using System.Runtime.InteropServices;
using System.Text;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, EntryPoint = "GetServiceDisplayNameW", CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
private static extern bool GetServiceDisplayNamePrivate(SafeServiceHandle SCMHandle, string serviceName, char[] displayName, ref int displayNameLength);
private static extern bool GetServiceDisplayNamePrivate(SafeServiceHandle SCMHandle, string serviceName, ref char displayName, ref int displayNameLength);

public static string GetServiceDisplayName(SafeServiceHandle SCMHandle, string serviceName)
public static unsafe string GetServiceDisplayName(SafeServiceHandle SCMHandle, string serviceName)
{
// Get the size of buffer required
int bufLen = 0;
bool success = GetServiceDisplayNamePrivate(SCMHandle, serviceName, null, ref bufLen);
char[] buffer = null;

if (!success && Marshal.GetLastWin32Error() == Interop.Errors.ERROR_INSUFFICIENT_BUFFER)
var builder = new ValueStringBuilder(4096);
int bufLen;
while (true)
{
bufLen++; // Does not include null
buffer = ArrayPool<char>.Shared.Rent(bufLen);
bufLen = builder.Capacity;
if (GetServiceDisplayNamePrivate(SCMHandle, serviceName, ref builder.GetPinnableReference(), ref bufLen))
break;

try
{
success = GetServiceDisplayNamePrivate(SCMHandle, serviceName, buffer, ref bufLen);
if (success)
return new string(buffer, 0, bufLen);
}
finally
int lastError = Marshal.GetLastWin32Error();
if (lastError != Interop.Errors.ERROR_INSUFFICIENT_BUFFER)
{
ArrayPool<char>.Shared.Return(buffer);
return null; // Caller may want to try something else
}

builder.EnsureCapacity(bufLen + 1); // Does not include null
}

return null;
builder.Length = bufLen;
return builder.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<Configurations>net461-Windows_NT-Debug;net461-Windows_NT-Release;netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;netfx-Windows_NT-Debug;netfx-Windows_NT-Release;netstandard-Debug;netstandard-Release;netstandard-Windows_NT-Debug;netstandard-Windows_NT-Release</Configurations>
</PropertyGroup>
<ItemGroup Condition="$(TargetGroup.StartsWith('netcoreapp')) OR ('$(TargetGroup)' == 'netstandard' AND '$(TargetsWindows)' == 'true')">
<Compile Include="$(CommonPath)\CoreLib\System\Text\ValueStringBuilder.cs">
<Link>Common\CoreLib\System\Text\ValueStringBuilder.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
<Link>Common\Interop\Windows\Interop.Libraries.cs</Link>
</Compile>
Expand Down Expand Up @@ -108,8 +111,10 @@
<Reference Include="System.ComponentModel.Primitives" />
<Reference Include="System.Diagnostics.Debug" />
<Reference Include="System.Diagnostics.Tools" />
<Reference Include="System.Memory" />
<Reference Include="System.Resources.ResourceManager" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Threading" />
<Reference Include="System.Threading.Thread" />
Expand Down

0 comments on commit bd9db82

Please sign in to comment.