From c4f4d360e3ca85f425a240d8e447ffe7bfbf69e2 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Tue, 16 Mar 2021 20:39:35 -0600 Subject: [PATCH] Offer implicit conversion from nint/nuint to LPARAM/WPARAM Closes #182 --- src/Microsoft.Windows.CsWin32/Generator.cs | 8 +++++--- test/GenerationSandbox.Tests/GeneratedForm.cs | 10 ++++++++++ test/GenerationSandbox.Tests/NativeMethods.txt | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.Windows.CsWin32/Generator.cs b/src/Microsoft.Windows.CsWin32/Generator.cs index 1b54adbc..e8679ca9 100644 --- a/src/Microsoft.Windows.CsWin32/Generator.cs +++ b/src/Microsoft.Windows.CsWin32/Generator.cs @@ -103,10 +103,12 @@ public class Generator : IDisposable private static readonly AttributeListSyntax DefaultDllImportSearchPathsAttributeList = AttributeList().AddAttributes( Attribute(IdentifierName("DefaultDllImportSearchPaths")).AddArgumentListArguments(AttributeArgument(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName(nameof(DllImportSearchPath)), IdentifierName(nameof(DllImportSearchPath.System32)))))); - private static readonly HashSet StringTypeDefNames = new HashSet(StringComparer.Ordinal) + private static readonly HashSet ImplicitConversionTypeDefs = new HashSet(StringComparer.Ordinal) { "PWSTR", "PSTR", + "LPARAM", + "WPARAM", }; private static readonly HashSet SpecialTypeDefNames = new HashSet(StringComparer.Ordinal) @@ -2784,8 +2786,8 @@ private StructDeclarationSyntax DeclareTypeDefStruct(TypeDefinition typeDef) .WithSemicolonToken(Token(SyntaxKind.SemicolonToken))); // public static explicit operator HWND(int value) => new HWND(value); - // Except make converting char* or byte* to typedefs representing strings implicit. - SyntaxToken explicitOrImplicitModifier = Token(StringTypeDefNames.Contains(name.Identifier.ValueText) ? SyntaxKind.ImplicitKeyword : SyntaxKind.ExplicitKeyword); + // Except make converting char* or byte* to typedefs representing strings, and LPARAM/WPARAM to nint/nuint, implicit. + SyntaxToken explicitOrImplicitModifier = Token(ImplicitConversionTypeDefs.Contains(name.Identifier.ValueText) ? SyntaxKind.ImplicitKeyword : SyntaxKind.ExplicitKeyword); members = members.Add(ConversionOperatorDeclaration(explicitOrImplicitModifier, name) .AddParameterListParameters(Parameter(valueParameter.Identifier).WithType(fieldInfo.FieldType)) .WithExpressionBody(ArrowExpressionClause(ObjectCreationExpression(name).AddArgumentListArguments(Argument(valueParameter)))) diff --git a/test/GenerationSandbox.Tests/GeneratedForm.cs b/test/GenerationSandbox.Tests/GeneratedForm.cs index fb970390..044e2d7d 100644 --- a/test/GenerationSandbox.Tests/GeneratedForm.cs +++ b/test/GenerationSandbox.Tests/GeneratedForm.cs @@ -17,4 +17,14 @@ private static void IEnumDebugPropertyInfo(IEnumDebugPropertyInfo info) HRESULT result = info.Next(span, out uint initialized); info.Clone(out IEnumDebugPropertyInfo ppepi); } + + private static void LPARAM_From_NInt() + { + LPARAM p = 1; + } + + private static void WPARAM_From_NInt() + { + WPARAM p = 1; + } } diff --git a/test/GenerationSandbox.Tests/NativeMethods.txt b/test/GenerationSandbox.Tests/NativeMethods.txt index 5e23f86e..f4580594 100644 --- a/test/GenerationSandbox.Tests/NativeMethods.txt +++ b/test/GenerationSandbox.Tests/NativeMethods.txt @@ -9,3 +9,4 @@ MainAVIHeader EnumWindows GetWindowTextLength GetWindowText +WPARAM