Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Use DllImportGenerator in System.Diagnostics.PerformanceCounter
  • Loading branch information
elinor-fung committed Nov 10, 2021
commit cd8d6fe7a93e50c062ab3eec4c3d053d1d6ed33c
2 changes: 1 addition & 1 deletion eng/generators.targets
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
and '$(IsFrameworkSupportFacade)' != 'true'
and '$(IsSourceProject)' == 'true'
and '$(MSBuildProjectExtension)' == '.csproj'
and '$(TargetFrameworkIdentifier)' == '.NETStandard'" />
and ('$(TargetFrameworkIdentifier)' == '.NETStandard' or '$(TargetFrameworkIdentifier)' == '.NETFramework')" />
Copy link
Member

Choose a reason for hiding this comment

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

Bah! Sorry @jkoritzinsky looks like I did miss that case <shame />

</ItemGroup>

<!-- Use this complex ItemGroup-based filtering to add the ProjectReference to make sure dotnet/runtime stays compatible with NuGet Static Graph Restore. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]
internal static extern bool ConvertStringSecurityDescriptorToSecurityDescriptor(
string StringSecurityDescriptor,
int StringSDRevision,
out SafeLocalAllocHandle pSecurityDescriptor,
IntPtr SecurityDescriptorSize);
[GeneratedDllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial bool ConvertStringSecurityDescriptorToSecurityDescriptor(
string StringSecurityDescriptor,
int StringSDRevision,
out SafeLocalAllocHandle pSecurityDescriptor,
IntPtr SecurityDescriptorSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Interop.Libraries.Advapi32, EntryPoint = "GetSecurityDescriptorLength", CallingConvention = CallingConvention.Winapi,
CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern /*DWORD*/ uint GetSecurityDescriptorLength(IntPtr byteArray);
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "GetSecurityDescriptorLength", CharSet = CharSet.Unicode, ExactSpelling = true)]
Copy link
Member

Choose a reason for hiding this comment

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

I guess we are just removing CallingConvention = CallingConvention.Winapi from everywhere as it's default and archaic.

internal static partial uint GetSecurityDescriptorLength(IntPtr byteArray);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Interop.Libraries.Advapi32, SetLastError = false)]
internal static extern uint LsaNtStatusToWinError(uint status);
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = false)]
internal static partial uint LsaNtStatusToWinError(uint status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32)]
internal static extern int RegCloseKey(IntPtr hKey);
[GeneratedDllImport(Libraries.Advapi32)]
internal static partial int RegCloseKey(IntPtr hKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegEnumKeyExW", ExactSpelling = true)]
internal static extern int RegEnumKeyEx(
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegEnumKeyExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegEnumKeyEx(
SafeRegistryHandle hKey,
int dwIndex,
char[] lpName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegEnumValueW", ExactSpelling = true)]
internal static extern int RegEnumValue(
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegEnumValueW", CharSet = CharSet.Unicode, ExactSpelling = true)]
Copy link
Member

Choose a reason for hiding this comment

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

are we assuming BestFitMapping = true is fine in these?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, these are all using CharSet.Unicode, so BestFitMapping shouldn't matter.

Copy link
Member

Choose a reason for hiding this comment

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

It seems like there are several variations of DllImport/GeneratedDllImport properties that are not meaningful or invalid but just ignored. I wonder whether it would make sense for the generator to flag/reject those so the ycan be cleaned up during conversion? I guess it is a minor thing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Certainly something we should keep in mind. We've been trying to remove support for sometimes-non-meaningful things (like BestFitMapping) from GeneratedDllImport, so hopefully it will be in a much better replace than DllImport in terms of weird properties and combinations.

internal static partial int RegEnumValue(
SafeRegistryHandle hKey,
int dwIndex,
char[] lpValueName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryInfoKeyW")]
internal static extern int RegQueryInfoKey(
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegQueryInfoKeyW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegQueryInfoKey(
SafeRegistryHandle hKey,
[Out] char[]? lpClass,
int[]? lpcbClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryValueExW", ExactSpelling = true)]
internal static extern int RegQueryValueEx(
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegQueryValueExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegQueryValueEx(
SafeRegistryHandle hKey,
string? lpValueName,
int[]? lpReserved,
ref int lpType,
[Out] byte[]? lpData,
byte[]? lpData,
ref int lpcbData);

[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryValueExW", ExactSpelling = true)]
internal static extern int RegQueryValueEx(
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegQueryValueExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegQueryValueEx(
SafeRegistryHandle hKey,
string? lpValueName,
int[]? lpReserved,
ref int lpType,
ref int lpData,
ref int lpcbData);

[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryValueExW", ExactSpelling = true)]
internal static extern int RegQueryValueEx(
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegQueryValueExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegQueryValueEx(
SafeRegistryHandle hKey,
string? lpValueName,
int[]? lpReserved,
ref int lpType,
ref long lpData,
ref int lpcbData);

[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryValueExW", ExactSpelling = true)]
internal static extern int RegQueryValueEx(
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegQueryValueExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegQueryValueEx(
SafeRegistryHandle hKey,
string? lpValueName,
int[]? lpReserved,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32, ExactSpelling = true, SetLastError = true)]
internal static extern bool FreeLibrary(IntPtr hModule);
[GeneratedDllImport(Libraries.Kernel32, ExactSpelling = true, SetLastError = true)]
internal static partial bool FreeLibrary(IntPtr hModule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, EntryPoint = "GetComputerNameW", ExactSpelling = true)]
private static unsafe extern int GetComputerName(char* lpBuffer, uint* nSize);
[GeneratedDllImport(Libraries.Kernel32, EntryPoint = "GetComputerNameW", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static unsafe partial int GetComputerName(char* lpBuffer, uint* nSize);

// maximum length of the NETBIOS name (not including NULL)
private const int MAX_COMPUTERNAME_LENGTH = 15;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32)]
internal static extern IntPtr GetCurrentProcess();
[GeneratedDllImport(Libraries.Kernel32)]
internal static partial IntPtr GetCurrentProcess();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32)]
internal static extern uint GetCurrentProcessId();
[GeneratedDllImport(Libraries.Kernel32)]
internal static partial uint GetCurrentProcessId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
[DllImport(Interop.Libraries.Kernel32)]
internal static extern IntPtr GetCurrentThread();
[GeneratedDllImport(Interop.Libraries.Kernel32)]
internal static partial IntPtr GetCurrentThread();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LoadLibrary(string libFilename);
[GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)]
public static partial IntPtr LoadLibrary(string libFilename);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32, ExactSpelling = true, SetLastError = true)]
internal static extern int WaitForSingleObject(SafeWaitHandle handle, int timeout);
[GeneratedDllImport(Libraries.Kernel32, ExactSpelling = true, SetLastError = true)]
internal static partial int WaitForSingleObject(SafeWaitHandle handle, int timeout);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ internal static partial class Interop
{
internal static partial class Pdh
{
[DllImport(Libraries.Pdh, CharSet = CharSet.Unicode)]
public static extern int PdhFormatFromRawValue(
[GeneratedDllImport(Libraries.Pdh, CharSet = CharSet.Unicode)]
public static partial int PdhFormatFromRawValue(
uint dwCounterType,
uint dwFormat,
ref long pTimeBase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ internal static partial class Interop
{
internal static partial class PerfCounter
{
[DllImport(Libraries.Advapi32, ExactSpelling = true)]
internal static extern uint PerfStopProvider(
[GeneratedDllImport(Libraries.Advapi32, ExactSpelling = true)]
internal static partial uint PerfStopProvider(
IntPtr hProvider
);

Expand Down Expand Up @@ -53,13 +53,15 @@ internal struct PerfCounterSetInstanceStruct
internal uint InstanceNameSize;
}

// TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we annotate blittable types used in interop in CoreLib (like Guid)
[DllImport(Libraries.Advapi32, ExactSpelling = true)]
internal static extern uint PerfStartProvider(
ref Guid ProviderGuid,
PERFLIBREQUEST ControlCallback,
out SafePerfProviderHandle phProvider
);

// TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we annotate blittable types used in interop in CoreLib (like Guid)
[DllImport(Libraries.Advapi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
internal static extern unsafe PerfCounterSetInstanceStruct* PerfCreateInstance(
SafePerfProviderHandle hProvider,
Expand All @@ -68,21 +70,21 @@ out SafePerfProviderHandle phProvider
uint dwInstance
);

[DllImport(Libraries.Advapi32, ExactSpelling = true)]
internal static extern unsafe uint PerfSetCounterSetInfo(
[GeneratedDllImport(Libraries.Advapi32, ExactSpelling = true)]
internal static unsafe partial uint PerfSetCounterSetInfo(
SafePerfProviderHandle hProvider,
PerfCounterSetInfoStruct* pTemplate,
uint dwTemplateSize
);

[DllImport(Libraries.Advapi32, ExactSpelling = true)]
internal static extern unsafe uint PerfDeleteInstance(
[GeneratedDllImport(Libraries.Advapi32, ExactSpelling = true)]
internal static unsafe partial uint PerfDeleteInstance(
SafePerfProviderHandle hProvider,
PerfCounterSetInstanceStruct* InstanceBlock
);

[DllImport(Libraries.Advapi32, ExactSpelling = true)]
internal static extern unsafe uint PerfSetCounterRefValue(
[GeneratedDllImport(Libraries.Advapi32, ExactSpelling = true)]
internal static unsafe partial uint PerfSetCounterRefValue(
SafePerfProviderHandle hProvider,
PerfCounterSetInstanceStruct* pInstance,
uint CounterId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class SspiCli
{
[DllImport(Interop.Libraries.SspiCli)]
internal static extern int LsaDeregisterLogonProcess(IntPtr LsaHandle);
[GeneratedDllImport(Interop.Libraries.SspiCli)]
internal static partial int LsaDeregisterLogonProcess(IntPtr LsaHandle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ internal static partial SECURITY_STATUS SspiEncodeStringsAsAuthIdentity(
string password,
out SafeSspiAuthDataHandle authData);

// TODO: Switch to use GeneratedDllImport once we annotate blittable types used in interop in CoreLib (like Guid)
// TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we annotate blittable types used in interop in CoreLib (like Guid)
[DllImport(Interop.Libraries.SspiCli, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static extern SECURITY_STATUS SetCredentialsAttributesW(
in CredHandle handlePtr,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

//
// Types in this file are used for generated p/invokes (docs/design/features/source-generator-pinvokes.md).
// See the DllImportGenerator experiment in https://github.com/dotnet/runtimelab.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

//
// Types in this file are used for generated p/invokes (docs/design/features/source-generator-pinvokes.md).
// See the DllImportGenerator experiment in https://github.com/dotnet/runtimelab.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
and instead use runtime checks.
-->
<TargetFrameworks>$(NetCoreAppMinimum);$(NetFrameworkMinimum)</TargetFrameworks>
<EnableDllImportGenerator>true</EnableDllImportGenerator>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)ILLink.Substitutions.AggressiveTrimming.xml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ Microsoft.Win32.SystemEvents</PackageDescription>
<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.CompilerServices.Unsafe" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Threading" />
<Reference Include="System.Threading.Thread" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<NoWarn>$(NoWarn);CA1838;CA1847</NoWarn>
<!-- Suppressions to avoid ifdefs:
CA1845: Use span-based 'string.Concat' and 'AsSpan' instead of 'Substring'
CA1846: Prefer 'AsSpan' over 'Substring' when span-based overloads are available -->
<NoWarn>$(NoWarn);CA1845;CA1846</NoWarn>
Copy link
Member

Choose a reason for hiding this comment

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

What's causing these analyzers to fire now and they didn't before?

Copy link
Member Author

Choose a reason for hiding this comment

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

The reference to System.Memory (used by some parts of the generated p/invoke stubs).

Copy link
Member

Choose a reason for hiding this comment

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

I see; then the analyzer sees that these APIs exist and the analyzers start running. Ok, thanks.

<IsPackable>true</IsPackable>
<PackageDescription>Provides the System.Diagnostics.EventLog class, which allows the applications to use the windows event log service.

Expand Down Expand Up @@ -125,8 +129,10 @@ System.Diagnostics.EventLog</PackageDescription>
<Reference Include="System.Diagnostics.Tools" />
<Reference Include="System.Diagnostics.TraceSource" />
<Reference Include="System.IO.FileSystem" />
<Reference Include="System.Memory" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.CompilerServices.Unsafe" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Resources.ResourceManager" />
<Reference Include="System.Security.Principal.Windows" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableDllImportGenerator>true</EnableDllImportGenerator>
</PropertyGroup>
<ItemGroup>
<Compile Include="EventInstanceTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ System.Diagnostics.PerformanceCounter</PackageDescription>
<Reference Include="System.Resources.ResourceManager" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.CompilerServices.Unsafe" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Security.Claims" />
<Reference Include="System.Security.Principal" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
<IgnoreForCI Condition="'$(TargetOS)' == 'Browser'">true</IgnoreForCI>
<EnableDllImportGenerator>true</EnableDllImportGenerator>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CoreLibSharedDir)System\PasteArguments.cs"
Expand Down
5 changes: 5 additions & 0 deletions src/libraries/System.Management/src/System.Management.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);0618</NoWarn>
<!-- Suppressins to avoid ifdefs:
CA1845: Use span-based 'string.Concat' and 'AsSpan' instead of 'Substring'. -->
<NoWarn>$(NoWarn);CA1845</NoWarn>
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0</TargetFrameworks>
<IsPackable>true</IsPackable>
Expand Down Expand Up @@ -70,9 +73,11 @@ System.Management.SelectQuery</PackageDescription>
<Reference Include="System.ComponentModel.Primitives" />
<Reference Include="System.ComponentModel.TypeConverter" />
<Reference Include="System.Diagnostics.Debug" />
<Reference Include="System.Memory" />
<Reference Include="System.ObjectModel" />
<Reference Include="System.Resources.ResourceManager" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.CompilerServices.Unsafe" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Threading" />
Expand Down
Loading