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

Switch over miscellaneous DllImports to GeneratedDllImport. #62353

Merged
merged 13 commits into from Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions eng/generators.targets
Expand Up @@ -20,13 +20,15 @@
('@(Reference)' != ''
and @(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices')))
or ('@(ProjectReference)' != ''
and @(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))))" />
and @(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)')))
or ('$(NetCoreAppCurrentTargetFrameworkMoniker)' == '$(TargetFrameworkMoniker)'
and '$(DisableImplicitAssemblyReferences)' == 'false'))" />
<EnabledGenerators Include="DllImportGenerator"
Condition="'$(EnableDllImportGenerator)' == ''
and '$(IsFrameworkSupportFacade)' != 'true'
and '$(IsSourceProject)' == 'true'
and '$(MSBuildProjectExtension)' == '.csproj'
and ('$(TargetFrameworkIdentifier)' == '.NETStandard' or '$(TargetFrameworkIdentifier)' == '.NETFramework')" />
and ('$(TargetFrameworkIdentifier)' == '.NETStandard' or '$(TargetFrameworkIdentifier)' == '.NETFramework' or ('$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionLessThan($(TargetFrameworkVersion), '$(NetCoreAppCurrentVersion)'))))" />
</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
4 changes: 2 additions & 2 deletions src/libraries/Common/src/Interop/Android/Interop.Logcat.cs
Expand Up @@ -8,8 +8,8 @@ internal static partial class Interop
{
internal static partial class Logcat
{
[DllImport(Libraries.Liblog)]
private static extern void __android_log_print(LogLevel level, string? tag, string format, string args, IntPtr ptr);
[GeneratedDllImport(Libraries.Liblog, CharSet = CharSet.Ansi)]
private static partial void __android_log_print(LogLevel level, string? tag, string format, string args, IntPtr ptr);

internal static void AndroidLogPrint(LogLevel level, string? tag, string message) =>
__android_log_print(level, tag, "%s", message, IntPtr.Zero);
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/Common/src/Interop/Interop.ICU.iOS.cs
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Globalization
{
[DllImport(Libraries.GlobalizationNative, EntryPoint = "GlobalizationNative_LoadICUData")]
internal static extern int LoadICUData(string path);
[GeneratedDllImport(Libraries.GlobalizationNative, EntryPoint = "GlobalizationNative_LoadICUData", CharSet = CharSet.Ansi)]
internal static partial int LoadICUData(string path);
}
}
Expand Up @@ -8,8 +8,8 @@ internal static partial class Interop
{
internal static partial class Sys
{
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_SearchPath")]
internal static extern string? SearchPath(NSSearchPathDirectory folderId);
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_SearchPath", CharSet = CharSet.Ansi)]
internal static partial string? SearchPath(NSSearchPathDirectory folderId);

internal enum NSSearchPathDirectory
{
Expand Down
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Sys
{
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_iOSSupportVersion")]
internal static extern string iOSSupportVersion();
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_iOSSupportVersion", CharSet = CharSet.Ansi)]
internal static partial string iOSSupportVersion();
}
}
Expand Up @@ -10,8 +10,8 @@ internal static partial class Interop
{
internal static partial class Sys
{
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetNodeName", SetLastError = true)]
private static extern unsafe int GetNodeName(char* name, out int len);
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetNodeName", SetLastError = true)]
private static unsafe partial int GetNodeName(char* name, out int len);

internal static unsafe string GetNodeName()
{
Expand Down
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Sys
{
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MemSet")]
[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MemSet")]
internal static extern unsafe void* MemSet(void *s, int c, UIntPtr n);
}
}
Expand Up @@ -9,10 +9,13 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool ChangeServiceConfig2(SafeServiceHandle serviceHandle, uint infoLevel, ref SERVICE_DESCRIPTION serviceDesc);
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "ChangeServiceConfig2W", ExactSpelling = true, SetLastError = true)]
public static partial bool ChangeServiceConfig2(SafeServiceHandle serviceHandle, uint infoLevel, ref SERVICE_DESCRIPTION serviceDesc);

[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
#pragma warning disable DLLIMPORTGENANALYZER015 // Use 'GeneratedDllImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
// TODO: [DllImportGenerator] Switch to use GeneratedDllImport once we support non-blittable types.
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be better to reference tracking issue

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the comment pattern we've used even migrating over the rest of the DllImports, so I'd prefer to match that.

[DllImport(Libraries.Advapi32, EntryPoint = "ChangeServiceConfig2W", ExactSpelling = true, SetLastError = true)]
public static extern bool ChangeServiceConfig2(SafeServiceHandle serviceHandle, uint infoLevel, ref SERVICE_DELAYED_AUTOSTART_INFO serviceDesc);
#pragma warning restore DLLIMPORTGENANALYZER015
}
}
Expand Up @@ -9,7 +9,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr CreateService(SafeServiceHandle databaseHandle, string serviceName, string displayName, int access, int serviceType,
int startType, int errorControl, string binaryPath, string loadOrderGroup, IntPtr pTagId, string dependencies,
string servicesStartName, string password);
Expand Down
Expand Up @@ -9,7 +9,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool DeleteService(SafeServiceHandle serviceHandle);
[GeneratedDllImport(Libraries.Advapi32, SetLastError = true)]
public static partial bool DeleteService(SafeServiceHandle serviceHandle);
}
}
Expand Up @@ -11,7 +11,7 @@ internal static partial class Kernel32
{
internal const uint CTMF_INCLUDE_APPCONTAINER = 0x00000001;

[DllImport(Interop.Libraries.Kernel32, SetLastError = true)]
internal static extern bool CheckTokenMembershipEx(SafeAccessTokenHandle TokenHandle, byte[] SidToCheck, uint Flags, ref bool IsMember);
[GeneratedDllImport(Interop.Libraries.Kernel32, SetLastError = true)]
internal static partial bool CheckTokenMembershipEx(SafeAccessTokenHandle TokenHandle, byte[] SidToCheck, uint Flags, ref bool IsMember);
}
}
Expand Up @@ -11,40 +11,40 @@ internal static partial class Kernel32
{
internal const int WAIT_FAILED = unchecked((int)0xFFFFFFFF);

[DllImport(Libraries.Kernel32)]
internal static extern uint WaitForMultipleObjectsEx(uint nCount, IntPtr lpHandles, BOOL bWaitAll, uint dwMilliseconds, BOOL bAlertable);
[GeneratedDllImport(Libraries.Kernel32)]
internal static partial uint WaitForMultipleObjectsEx(uint nCount, IntPtr lpHandles, BOOL bWaitAll, uint dwMilliseconds, BOOL bAlertable);

[DllImport(Libraries.Kernel32)]
internal static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
[GeneratedDllImport(Libraries.Kernel32)]
internal static partial uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);

[DllImport(Libraries.Kernel32)]
internal static extern uint SignalObjectAndWait(IntPtr hObjectToSignal, IntPtr hObjectToWaitOn, uint dwMilliseconds, BOOL bAlertable);
[GeneratedDllImport(Libraries.Kernel32)]
internal static partial uint SignalObjectAndWait(IntPtr hObjectToSignal, IntPtr hObjectToWaitOn, uint dwMilliseconds, BOOL bAlertable);

[DllImport(Libraries.Kernel32)]
internal static extern void Sleep(uint milliseconds);
[GeneratedDllImport(Libraries.Kernel32)]
internal static partial void Sleep(uint milliseconds);

internal const uint CREATE_SUSPENDED = 0x00000004;
internal const uint STACK_SIZE_PARAM_IS_A_RESERVATION = 0x00010000;

[DllImport(Libraries.Kernel32)]
internal static extern unsafe SafeWaitHandle CreateThread(
[GeneratedDllImport(Libraries.Kernel32)]
internal static unsafe partial SafeWaitHandle CreateThread(
IntPtr lpThreadAttributes,
IntPtr dwStackSize,
delegate* unmanaged<IntPtr, uint> lpStartAddress,
IntPtr lpParameter,
uint dwCreationFlags,
out uint lpThreadId);

[DllImport(Libraries.Kernel32)]
internal static extern uint ResumeThread(SafeWaitHandle hThread);
[GeneratedDllImport(Libraries.Kernel32)]
internal static partial uint ResumeThread(SafeWaitHandle hThread);

[DllImport(Libraries.Kernel32)]
internal static extern IntPtr GetCurrentThread();
[GeneratedDllImport(Libraries.Kernel32)]
internal static partial IntPtr GetCurrentThread();

internal const int DUPLICATE_SAME_ACCESS = 2;

[DllImport(Libraries.Kernel32, SetLastError = true)]
internal static extern bool DuplicateHandle(
[GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
internal static partial bool DuplicateHandle(
IntPtr hSourceProcessHandle,
IntPtr hSourceHandle,
IntPtr hTargetProcessHandle,
Expand All @@ -66,10 +66,10 @@ internal enum ThreadPriority : int
ErrorReturn = 0x7FFFFFFF
}

[DllImport(Libraries.Kernel32)]
internal static extern ThreadPriority GetThreadPriority(SafeWaitHandle hThread);
[GeneratedDllImport(Libraries.Kernel32)]
internal static partial ThreadPriority GetThreadPriority(SafeWaitHandle hThread);

[DllImport(Libraries.Kernel32)]
internal static extern bool SetThreadPriority(SafeWaitHandle hThread, int nPriority);
[GeneratedDllImport(Libraries.Kernel32)]
internal static partial bool SetThreadPriority(SafeWaitHandle hThread, int nPriority);
}
}
Expand Up @@ -14,7 +14,7 @@ public enum SystemParametersAction : uint
SPI_GETNONCLIENTMETRICS = 0x29
}

[GeneratedDllImport(Libraries.User32, CharSet = CharSet.Unicode, ExactSpelling = true)]
[GeneratedDllImport(Libraries.User32)]
public static unsafe partial bool SystemParametersInfoW(SystemParametersAction uiAction, uint uiParam, void* pvParam, uint fWinIni);
}
}
Expand Up @@ -65,10 +65,10 @@ internal static partial class Interop
[SuppressUnmanagedCodeSecurityAttribute]
internal static partial class Kernel32
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
internal static extern int GetCurrentThreadId();
[GeneratedDllImport(nameof(Kernel32))]
internal static partial int GetCurrentThreadId();

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
internal static extern uint GetCurrentProcessId();
[GeneratedDllImport(nameof(Kernel32))]
internal static partial uint GetCurrentProcessId();
}
}
Expand Up @@ -6,6 +6,7 @@
<PackageDescription>.NET hosting infrastructure for Systemd Services.</PackageDescription>
<!-- Use targeting pack references instead of granular ones in the project file. -->
<DisableImplicitAssemblyReferences>false</DisableImplicitAssemblyReferences>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
Expand All @@ -20,4 +21,7 @@
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Hosting\src\Microsoft.Extensions.Hosting.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<Reference Include="System.Runtime.InteropServices" />
</ItemGroup>
</Project>
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.Extensions.Hosting.Systemd
/// <summary>
/// Helper methods for systemd Services.
/// </summary>
public static class SystemdHelpers
public static partial class SystemdHelpers
{
private static bool? _isSystemdService;

Expand Down Expand Up @@ -46,7 +46,7 @@ private static bool CheckParentIsSystemd()
return false;
}

[DllImport("libc", EntryPoint = "getppid")]
private static extern int GetParentPid();
[GeneratedDllImport("libc", EntryPoint = "getppid")]
private static partial int GetParentPid();
}
}
Expand Up @@ -4,6 +4,7 @@
<DefineConstants Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true'">$(DefineConstants);Unix</DefineConstants>
<TestRuntime>true</TestRuntime>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<EnableDllImportGenerator>true</EnableDllImportGenerator>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
Expand Down