Skip to content

Commit

Permalink
Remove BestFitMapping, CallingConvention, and ThrowOnUnmappableChar f…
Browse files Browse the repository at this point in the history
…rom GeneratedDllImportAttribute (#61182)
  • Loading branch information
elinor-fung authored Nov 6, 2021
1 parent 8273939 commit 9dc08dd
Show file tree
Hide file tree
Showing 17 changed files with 14 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "LookupPrivilegeValueW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial bool LookupPrivilegeValue(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "LookupPrivilegeValueW", CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = false)]
internal static extern bool LookupPrivilegeValue(
#endif
[MarshalAs(UnmanagedType.LPTStr)] string? lpSystemName, [MarshalAs(UnmanagedType.LPTStr)] string lpName, out LUID lpLuid);

internal const string SeDebugPrivilege = "SeDebugPrivilege";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ internal static partial class Advapi32
{
// Note: RegCreateKeyEx won't set the last error on failure - it returns
// an error code if it fails.
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegCreateKeyExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegCreateKeyEx(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegCreateKeyExW", BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int RegCreateKeyEx(
#endif
SafeRegistryHandle hKey,
string lpSubKey,
int Reserved,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegDeleteKeyExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegDeleteKeyEx(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegDeleteKeyExW", BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int RegDeleteKeyEx(
#endif
SafeRegistryHandle hKey,
string lpSubKey,
int samDesired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegDeleteValueW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegDeleteValue(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegDeleteValueW", BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int RegDeleteValue(
#endif
SafeRegistryHandle hKey,
string? lpValueName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32)]
internal static partial int RegFlushKey(
#else
[DllImport(Libraries.Advapi32)]
internal static extern int RegFlushKey(
#endif
SafeRegistryHandle hKey);
internal static partial int RegFlushKey(SafeRegistryHandle hKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,16 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegOpenKeyExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegOpenKeyEx(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegOpenKeyExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int RegOpenKeyEx(
#endif
SafeRegistryHandle hKey,
string? lpSubKey,
int ulOptions,
int samDesired,
out SafeRegistryHandle hkResult);


#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegOpenKeyExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegOpenKeyEx(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegOpenKeyExW", BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int RegOpenKeyEx(
#endif
IntPtr hKey,
string? lpSubKey,
int ulOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,69 +12,44 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegSetValueExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegSetValueEx(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegSetValueExW", BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int RegSetValueEx(
#endif
SafeRegistryHandle hKey,
string? lpValueName,
int Reserved,
int dwType,
byte[]? lpData,
int cbData);

#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegSetValueExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegSetValueEx(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegSetValueExW", BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int RegSetValueEx(
#endif
SafeRegistryHandle hKey,
string? lpValueName,
int Reserved,
int dwType,
char[]? lpData,
int cbData);

#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegSetValueExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegSetValueEx(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegSetValueExW", BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int RegSetValueEx(
#endif
SafeRegistryHandle hKey,
string? lpValueName,
int Reserved,
int dwType,
ref int lpData,
int cbData);

#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegSetValueExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegSetValueEx(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegSetValueExW", BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int RegSetValueEx(
#endif
SafeRegistryHandle hKey,
string? lpValueName,
int Reserved,
int dwType,
ref long lpData,
int cbData);

#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "RegSetValueExW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static partial int RegSetValueEx(
#else
[DllImport(Libraries.Advapi32, EntryPoint = "RegSetValueExW", BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int RegSetValueEx(
#endif
SafeRegistryHandle hKey,
string? lpValueName,
int Reserved,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ internal static partial class Kernel32
/// <summary>
/// WARNING: This method does not implicitly handle long paths. Use GetFullPathName or PathHelper.
/// </summary>
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial uint GetFullPathNameW(
#else
[DllImport(Libraries.Kernel32, BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static extern uint GetFullPathNameW(
#endif
ref char lpFileName,
uint nBufferLength,
ref char lpBuffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ internal static partial class Kernel32
/// <summary>
/// WARNING: This method does not implicitly handle long paths. Use GetFullPath/PathHelper.
/// </summary>
#if DLLIMPORTGENERATOR_ENABLED
[GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static partial uint GetLongPathNameW(
#else
[DllImport(Libraries.Kernel32, BestFitMapping = false, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
internal static extern uint GetLongPathNameW(
#endif
ref char lpszShortPath,
ref char lpszLongPath,
uint cchBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

//
// 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.
//
namespace System.Runtime.InteropServices
{
Expand All @@ -20,14 +19,11 @@ namespace System.Runtime.InteropServices
#endif
sealed class GeneratedDllImportAttribute : Attribute
{
public bool BestFitMapping { get; set; }
public CallingConvention CallingConvention { get; set; }
public CharSet CharSet { get; set; }
public string? EntryPoint { get; set; }
public bool ExactSpelling { get; set; }
public bool PreserveSig { get; set; }
public bool SetLastError { get; set; }
public bool ThrowOnUnmappableChar { get; set; }

public GeneratedDllImportAttribute(string dllName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<Reference Include="System.Memory" />
<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" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<PropertyGroup>
<DefineConstants>$(DefineConstants);REGISTRY_ASSEMBLY</DefineConstants>
<TargetFrameworks>$(NetCoreAppCurrent)-windows</TargetFrameworks>
<EnableDllImportGenerator>true</EnableDllImportGenerator>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,11 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu
// documented semanatics of DllImportAttribute:
// - https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.dllimportattribute
DllImportMember userDefinedValues = DllImportMember.None;
bool bestFitMapping = false;
CallingConvention callingConvention = CallingConvention.Winapi;
CharSet charSet = CharSet.Ansi;
string? entryPoint = null;
bool exactSpelling = false; // VB has different and unusual default behavior here.
bool preserveSig = true;
bool setLastError = false;
bool throwOnUnmappableChar = false;

// All other data on attribute is defined as NamedArguments.
foreach (KeyValuePair<string, TypedConstant> namedArg in attrData.NamedArguments)
Expand All @@ -365,14 +362,6 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu
default:
Debug.Fail($"An unknown member was found on {GeneratedDllImport}");
continue;
case nameof(GeneratedDllImportData.BestFitMapping):
userDefinedValues |= DllImportMember.BestFitMapping;
bestFitMapping = (bool)namedArg.Value.Value!;
break;
case nameof(GeneratedDllImportData.CallingConvention):
userDefinedValues |= DllImportMember.CallingConvention;
callingConvention = (CallingConvention)namedArg.Value.Value!;
break;
case nameof(GeneratedDllImportData.CharSet):
userDefinedValues |= DllImportMember.CharSet;
charSet = (CharSet)namedArg.Value.Value!;
Expand All @@ -393,24 +382,17 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu
userDefinedValues |= DllImportMember.SetLastError;
setLastError = (bool)namedArg.Value.Value!;
break;
case nameof(GeneratedDllImportData.ThrowOnUnmappableChar):
userDefinedValues |= DllImportMember.ThrowOnUnmappableChar;
throwOnUnmappableChar = (bool)namedArg.Value.Value!;
break;
}
}

return new GeneratedDllImportData(attrData.ConstructorArguments[0].Value!.ToString())
{
IsUserDefined = userDefinedValues,
BestFitMapping = bestFitMapping,
CallingConvention = callingConvention,
CharSet = charSet,
EntryPoint = entryPoint,
ExactSpelling = exactSpelling,
PreserveSig = preserveSig,
SetLastError = setLastError,
ThrowOnUnmappableChar = throwOnUnmappableChar
};
}

Expand Down Expand Up @@ -452,31 +434,16 @@ private static IncrementalStubGenerationContext CalculateStubInformation(IMethod
// Process the GeneratedDllImport attribute
GeneratedDllImportData stubDllImportData = ProcessGeneratedDllImportAttribute(generatedDllImportAttr!);

if (stubDllImportData.IsUserDefined.HasFlag(DllImportMember.BestFitMapping))
{
generatorDiagnostics.ReportConfigurationNotSupported(generatedDllImportAttr!, nameof(GeneratedDllImportData.BestFitMapping));
}

if (stubDllImportData.IsUserDefined.HasFlag(DllImportMember.ThrowOnUnmappableChar))
{
generatorDiagnostics.ReportConfigurationNotSupported(generatedDllImportAttr!, nameof(GeneratedDllImportData.ThrowOnUnmappableChar));
}

if (stubDllImportData.IsUserDefined.HasFlag(DllImportMember.CallingConvention))
{
generatorDiagnostics.ReportConfigurationNotSupported(generatedDllImportAttr!, nameof(GeneratedDllImportData.CallingConvention));
}

if (lcidConversionAttr != null)
{
// Using LCIDConversion with GeneratedDllImport is not supported
generatorDiagnostics.ReportConfigurationNotSupported(lcidConversionAttr, nameof(TypeNames.LCIDConversionAttribute));
}
List<AttributeSyntax> additionalAttributes = GenerateSyntaxForForwardedAttributes(suppressGCTransitionAttribute, unmanagedCallConvAttribute);

// Create the stub.
var dllImportStub = DllImportStubContext.Create(symbol, stubDllImportData, environment, generatorDiagnostics, ct);

List<AttributeSyntax> additionalAttributes = GenerateSyntaxForForwardedAttributes(suppressGCTransitionAttribute, unmanagedCallConvAttribute);
return new IncrementalStubGenerationContext(environment, dllImportStub, additionalAttributes.ToImmutableArray(), stubDllImportData, generatorDiagnostics.Diagnostics.ToImmutableArray());
}

Expand Down Expand Up @@ -602,18 +569,6 @@ private static AttributeSyntax CreateDllImportAttributeForTarget(GeneratedDllImp
CreateStringExpressionSyntax(targetDllImportData.EntryPoint!))
};

if (targetDllImportData.IsUserDefined.HasFlag(DllImportMember.BestFitMapping))
{
NameEqualsSyntax name = NameEquals(nameof(DllImportAttribute.BestFitMapping));
ExpressionSyntax value = CreateBoolExpressionSyntax(targetDllImportData.BestFitMapping);
newAttributeArgs.Add(AttributeArgument(name, null, value));
}
if (targetDllImportData.IsUserDefined.HasFlag(DllImportMember.CallingConvention))
{
NameEqualsSyntax name = NameEquals(nameof(DllImportAttribute.CallingConvention));
ExpressionSyntax value = CreateEnumExpressionSyntax(targetDllImportData.CallingConvention);
newAttributeArgs.Add(AttributeArgument(name, null, value));
}
if (targetDllImportData.IsUserDefined.HasFlag(DllImportMember.CharSet))
{
NameEqualsSyntax name = NameEquals(nameof(DllImportAttribute.CharSet));
Expand All @@ -638,12 +593,6 @@ private static AttributeSyntax CreateDllImportAttributeForTarget(GeneratedDllImp
ExpressionSyntax value = CreateBoolExpressionSyntax(targetDllImportData.SetLastError);
newAttributeArgs.Add(AttributeArgument(name, null, value));
}
if (targetDllImportData.IsUserDefined.HasFlag(DllImportMember.ThrowOnUnmappableChar))
{
NameEqualsSyntax name = NameEquals(nameof(DllImportAttribute.ThrowOnUnmappableChar));
ExpressionSyntax value = CreateBoolExpressionSyntax(targetDllImportData.ThrowOnUnmappableChar);
newAttributeArgs.Add(AttributeArgument(name, null, value));
}

// Create new attribute
return Attribute(
Expand Down Expand Up @@ -691,13 +640,10 @@ private static GeneratedDllImportData GetTargetDllImportDataFromStubData(Generat
var targetDllImportData = new GeneratedDllImportData(dllImportData.ModuleName)
{
CharSet = dllImportData.CharSet,
BestFitMapping = dllImportData.BestFitMapping,
CallingConvention = dllImportData.CallingConvention,
EntryPoint = dllImportData.EntryPoint,
ExactSpelling = dllImportData.ExactSpelling,
SetLastError = dllImportData.SetLastError,
PreserveSig = dllImportData.PreserveSig,
ThrowOnUnmappableChar = dllImportData.ThrowOnUnmappableChar,
IsUserDefined = dllImportData.IsUserDefined & membersToForward
};

Expand Down
Loading

0 comments on commit 9dc08dd

Please sign in to comment.