Skip to content

Commit

Permalink
Use DllImportGenerator in System.Diagnostics.PerformanceCounter (#61389)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung committed Nov 11, 2021
1 parent 108fb82 commit 2da44f3
Show file tree
Hide file tree
Showing 39 changed files with 85 additions and 127 deletions.
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')" />
</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)]
internal static partial uint GetSecurityDescriptorLength(IntPtr byteArray);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,24 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static partial bool GetTokenInformation(
#else
[DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static extern bool GetTokenInformation(
#endif
SafeAccessTokenHandle TokenHandle,
uint TokenInformationClass,
SafeLocalAllocHandle TokenInformation,
uint TokenInformationLength,
out uint ReturnLength);

#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static partial bool GetTokenInformation(
#else
[DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static extern bool GetTokenInformation(
#endif
IntPtr TokenHandle,
uint TokenInformationClass,
SafeLocalAllocHandle TokenInformation,
uint TokenInformationLength,
out uint ReturnLength);

#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
internal static partial bool GetTokenInformation(
#else
[DllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool GetTokenInformation(
#endif
IntPtr TokenHandle,
uint TokenInformationClass,
IntPtr TokenInformation,
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 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 @@ -14,13 +14,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegConnectRegistryW", CharSet = CharSet.Unicode)]
internal static partial int RegConnectRegistry(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegConnectRegistryW", CharSet = CharSet.Unicode, BestFitMapping = false)]
internal static extern int RegConnectRegistry(
#endif
string machineName,
SafeRegistryHandle key,
out SafeRegistryHandle result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ 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,
[Out] char[] lpName,
ref int lpcbName,
int[]? lpReserved,
[Out] char[]? lpClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ 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)]
internal static partial int RegEnumValue(
SafeRegistryHandle hKey,
int dwIndex,
char[] lpValueName,
[Out] char[] lpValueName,
ref int lpcbValueName,
IntPtr lpReserved_MustBeZero,
int[]? lpType,
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 @@ -10,27 +10,17 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, EntryPoint = "CreateFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
[GeneratedDllImport(Libraries.Kernel32, EntryPoint = "CreateFileMappingW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial SafeMemoryMappedFileHandle CreateFileMapping(
#else
[DllImport(Libraries.Kernel32, EntryPoint = "CreateFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeMemoryMappedFileHandle CreateFileMapping(
#endif
SafeFileHandle hFile,
ref SECURITY_ATTRIBUTES lpFileMappingAttributes,
int flProtect,
int dwMaximumSizeHigh,
int dwMaximumSizeLow,
string? lpName);

#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, EntryPoint = "CreateFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
[GeneratedDllImport(Libraries.Kernel32, EntryPoint = "CreateFileMappingW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial SafeMemoryMappedFileHandle CreateFileMapping(
#else
[DllImport(Libraries.Kernel32, EntryPoint = "CreateFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeMemoryMappedFileHandle CreateFileMapping(
#endif
IntPtr hFile,
ref SECURITY_ATTRIBUTES lpFileMappingAttributes,
int flProtect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
internal static partial bool DuplicateHandle(
#else
[DllImport(Libraries.Kernel32, SetLastError = true)]
internal static extern bool DuplicateHandle(
#endif
IntPtr hSourceProcessHandle,
SafeHandle hSourceHandle,
IntPtr hTargetProcess,
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,13 +8,8 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial bool GetProcessTimes(
#else
[DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool GetProcessTimes(
#endif
SafeProcessHandle handle, out long creation, out long exit, out long kernel, out long user);
}
}
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 @@ -9,13 +9,8 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, EntryPoint = "MapViewOfFile", CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial SafeMemoryMappedViewHandle MapViewOfFile(
#else
[DllImport(Libraries.Kernel32, EntryPoint = "MapViewOfFile", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeMemoryMappedViewHandle MapViewOfFile(
#endif
SafeMemoryMappedFileHandle hFileMappingObject,
int dwDesiredAccess,
int dwFileOffsetHigh,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, EntryPoint = "OpenFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
[GeneratedDllImport(Libraries.Kernel32, EntryPoint = "OpenFileMappingW", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial SafeMemoryMappedFileHandle OpenFileMapping(
#else
[DllImport(Libraries.Kernel32, EntryPoint = "OpenFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeMemoryMappedFileHandle OpenFileMapping(
#endif
int dwDesiredAccess,
[MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
string lpName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial SafeProcessHandle OpenProcess(
#else
[DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeProcessHandle OpenProcess(
#endif
int access, bool inherit, int processId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
internal static partial bool UnmapViewOfFile(IntPtr lpBaseAddress);
#else
[DllImport(Libraries.Kernel32, SetLastError = true)]
internal static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ internal static partial class Interop
{
internal static partial class Kernel32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, ExactSpelling = true, SetLastError = true)]
internal static partial UIntPtr VirtualQuery(
#else
[DllImport(Libraries.Kernel32, ExactSpelling = true, SetLastError = true)]
internal static extern UIntPtr VirtualQuery(
#endif
SafeHandle lpAddress,
ref MEMORY_BASIC_INFORMATION lpBuffer,
UIntPtr dwLength);
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 int WaitForSingleObject(SafeWaitHandle handle, int timeout);
[GeneratedDllImport(Libraries.Kernel32, ExactSpelling = true, SetLastError = true)]
internal static partial int WaitForSingleObject(SafeWaitHandle handle, int timeout);
}
}
Loading

0 comments on commit 2da44f3

Please sign in to comment.