Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Common/src/Interop/Windows/Interop.Libraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal static class Libraries
internal const string NCrypt = "ncrypt.dll";
internal const string NtDll = "ntdll.dll";
internal const string OleAut32 = "oleaut32.dll";
internal const string PerfCounter = "perfcounter.dll";
internal const string RoBuffer = "api-ms-win-core-winrt-robuffer-l1-1-0.dll";
internal const string Secur32 = "secur32.dll";
internal const string Shell32 = "shell32.dll";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.InteropServices;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]
internal static extern unsafe bool ConvertStringSecurityDescriptorToSecurityDescriptor(
string StringSecurityDescriptor,
int StringSDRevision,
out SafeLocalMemHandle pSecurityDescriptor,
IntPtr SecurityDescriptorSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ internal static extern bool GetTokenInformation(
SafeLocalAllocHandle TokenInformation,
uint TokenInformationLength,
out uint ReturnLength);

[DllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool GetTokenInformation(
IntPtr TokenHandle,
uint TokenInformationClass,
IntPtr TokenInformation,
uint TokenInformationLength,
out uint ReturnLength);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using System.Text;

internal partial class Interop
{
Expand All @@ -11,6 +12,9 @@ internal partial class Kernel32
[DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, EntryPoint = "GetComputerNameW")]
private static extern unsafe int GetComputerName(char* lpBuffer, ref uint nSize);

[DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, BestFitMapping = false)]
public static extern bool GetComputerName(StringBuilder lpBuffer, int[] nSize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Instead of adding this new overload that takes a StringBuilder for lpBuffer, why not use the existing internal helper method below that returns a string (and avoids the StringBuilder allocation and marshaling costs)? Then update the calling code in PerformanceCounterLib.cs to something like the following:

internal static string ComputerName
{
    get
    {
        if (s_computerName == null)
        {
            lock (InternalSyncObject)
            {
                if (s_computerName == null)
                {
                    s_computerName = Interop.Kernel32.GetComputerName() ?? string.Empty;
                }
            }
        }

        return s_computerName;
    }
}

Note that the above maintains the previous behavior of setting s_computerName to an empty string if the call to GetComputerName fails, which could happen previously when StringBuilder.ToString() is called on an empty builder.


// maximum length of the NETBIOS name (not including NULL)
private const int MAX_COMPUTERNAME_LENGTH = 15;

Expand Down
16 changes: 16 additions & 0 deletions src/Common/src/Interop/Windows/kernel32/Interop.LoadLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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.Runtime.InteropServices;

internal partial class Interop
{
internal partial class Kernel32
{
[DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LoadLibrary(string libFilename);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using System.Threading;

internal partial class Interop
{
internal partial class Kernel32
{
internal sealed class ProcessWaitHandle : WaitHandle
{
internal ProcessWaitHandle(SafeProcessHandle processHandle)
{
SafeWaitHandle waitHandle = null;
SafeProcessHandle currentProcHandle = Interop.Kernel32.GetCurrentProcess();
bool succeeded = Interop.Kernel32.DuplicateHandle(
currentProcHandle,
processHandle,
currentProcHandle,
out waitHandle,
0,
false,
Interop.Kernel32.HandleOptions.DUPLICATE_SAME_ACCESS);

if (!succeeded)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}

this.SetSafeWaitHandle(waitHandle);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
internal partial class Interop
{
internal static partial class Kernel32
internal partial class Kernel32
{
[DllImport(Interop.Libraries.Kernel32, ExactSpelling = true, SetLastError = true, EntryPoint = "WaitForSingleObject")]
public static extern int WaitForSingleObject(SafeWaitHandle handle, int timeout);
[DllImport(Libraries.Kernel32, ExactSpelling=true, SetLastError=true)]
internal static extern int WaitForSingleObject(SafeWaitHandle handle, int timeout);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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.Runtime.InteropServices;

internal partial class Interop
{
internal partial class PerfCounter
{
#pragma warning disable BCL0015 // Invalid Pinvoke call
[DllImport(Libraries.PerfCounter, CharSet = CharSet.Unicode)]
public static unsafe extern int FormatFromRawValue(
uint dwCounterType,
uint dwFormat,
ref long pTimeBase,
Interop.Kernel32.PerformanceCounterOptions.PDH_RAW_COUNTER pRawValue1,
Interop.Kernel32.PerformanceCounterOptions.PDH_RAW_COUNTER pRawValue2,
Interop.Kernel32.PerformanceCounterOptions.PDH_FMT_COUNTERVALUE pFmtValue
);
#pragma warning restore BCL0015
}
}
23 changes: 23 additions & 0 deletions src/Common/src/Microsoft/Win32/SafeHandles/SafeLocalMemHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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;

namespace Microsoft.Win32.SafeHandles
{
internal sealed class SafeLocalMemHandle : SafeHandleZeroOrMinusOneIsInvalid
{
internal SafeLocalMemHandle() : base(true) { }

internal SafeLocalMemHandle(IntPtr existingHandle, bool ownsHandle) : base(ownsHandle)
{
SetHandle(existingHandle);
}

protected override bool ReleaseHandle()
{
return Interop.Kernel32.LocalFree(handle) == IntPtr.Zero;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Diagnostics.PerformanceCounter.Tests", "tests\System.Diagnostics.PerformanceCounter.Tests.csproj", "{296074B7-1CC9-497E-8C1E-FC5C985C75C6}"
ProjectSection(ProjectDependencies) = postProject
{9758106C-5D4E-475D-B5E7-B7ABC46B1DDA} = {9758106C-5D4E-475D-B5E7-B7ABC46B1DDA}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Diagnostics.PerformanceCounter", "src\System.Diagnostics.PerformanceCounter.csproj", "{9758106C-5D4E-475D-B5E7-B7ABC46B1DDA}"
ProjectSection(ProjectDependencies) = postProject
{5BDC8641-E3EE-47B5-BE7B-2D2275402412} = {5BDC8641-E3EE-47B5-BE7B-2D2275402412}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Diagnostics.PerformanceCounter", "ref\System.Diagnostics.PerformanceCounter.csproj", "{5BDC8641-E3EE-47B5-BE7B-2D2275402412}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{1A2F9F4A-A032-433E-B914-ADD5992BB178}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E107E9C1-E893-4E87-987E-04EF0DCEAEFD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{2E666815-2EDB-464B-9DF6-380BF4789AD4}"
EndProject
Global
Expand All @@ -12,15 +26,25 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5BDC8641-E3EE-47B5-BE7B-2D2275402412}.Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
{5BDC8641-E3EE-47B5-BE7B-2D2275402412}.Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
{5BDC8641-E3EE-47B5-BE7B-2D2275402412}.Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
{5BDC8641-E3EE-47B5-BE7B-2D2275402412}.Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
{296074B7-1CC9-497E-8C1E-FC5C985C75C6}.Debug|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Debug|Any CPU
{296074B7-1CC9-497E-8C1E-FC5C985C75C6}.Debug|Any CPU.Build.0 = netcoreapp-Windows_NT-Debug|Any CPU
{296074B7-1CC9-497E-8C1E-FC5C985C75C6}.Release|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Release|Any CPU
{296074B7-1CC9-497E-8C1E-FC5C985C75C6}.Release|Any CPU.Build.0 = netcoreapp-Windows_NT-Release|Any CPU
{9758106C-5D4E-475D-B5E7-B7ABC46B1DDA}.Debug|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Debug|Any CPU
{9758106C-5D4E-475D-B5E7-B7ABC46B1DDA}.Debug|Any CPU.Build.0 = netcoreapp-Windows_NT-Debug|Any CPU
{9758106C-5D4E-475D-B5E7-B7ABC46B1DDA}.Release|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Release|Any CPU
{9758106C-5D4E-475D-B5E7-B7ABC46B1DDA}.Release|Any CPU.Build.0 = netcoreapp-Windows_NT-Release|Any CPU
{5BDC8641-E3EE-47B5-BE7B-2D2275402412}.Debug|Any CPU.ActiveCfg = netstandard-Debug|Any CPU
{5BDC8641-E3EE-47B5-BE7B-2D2275402412}.Debug|Any CPU.Build.0 = netstandard-Debug|Any CPU
{5BDC8641-E3EE-47B5-BE7B-2D2275402412}.Release|Any CPU.ActiveCfg = netstandard-Release|Any CPU
{5BDC8641-E3EE-47B5-BE7B-2D2275402412}.Release|Any CPU.Build.0 = netstandard-Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{296074B7-1CC9-497E-8C1E-FC5C985C75C6} = {1A2F9F4A-A032-433E-B914-ADD5992BB178}
{9758106C-5D4E-475D-B5E7-B7ABC46B1DDA} = {E107E9C1-E893-4E87-987E-04EF0DCEAEFD}
{5BDC8641-E3EE-47B5-BE7B-2D2275402412} = {2E666815-2EDB-464B-9DF6-380BF4789AD4}
EndGlobalSection
EndGlobal
5 changes: 3 additions & 2 deletions src/System.Diagnostics.PerformanceCounter/dir.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\dir.props" />
<PropertyGroup>
<AssemblyVersion>4.0.1.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyKey>Open</AssemblyKey>
</PropertyGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netcoreapp;
netfx;
netstandard;
</BuildConfigurations>
</PropertyGroup>
</Project>
</Project>
Loading