Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DllImportGenerator in System.Diagnostics.PerformanceCounter #61389

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -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)]
Copy link
Member

Choose a reason for hiding this comment

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

why remove CharSet = CharSet.Unicode ? is it the default now?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not the default. I removed it because it is not necessary for this p/invoke - no string data being marshalled, no A/W suffix variants - and having it set to Unicode in this case makes it so that we do an extra probe for an entry point named GetTokenInformationW, since when ExactSpelling is false, we check for the W suffix first for Unicode (but no suffix first for Ansi).

Copy link
Member

Choose a reason for hiding this comment

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

this seems like a great thing for the generator to warn on (I say the generator since it's new so it wouldn't be a breaking change, and folks are touching their sources anyway)

Copy link
Member Author

Choose a reason for hiding this comment

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

We're currently discussing how we can make the whole character set situation better with the generator approach: #61326.

One thing we are considering as part of that is removing ExactSpelling (so weird/negative implications of char set like this wouldn't be a thing) and effectively requiring it to always be 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,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 @@ -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