Skip to content

Commit

Permalink
Merge pull request #200 from microsoft/fix182
Browse files Browse the repository at this point in the history
Offer implicit conversion from nint/nuint to LPARAM/WPARAM
  • Loading branch information
AArnott authored Mar 17, 2021
2 parents 7ca692d + c4f4d36 commit f9f0a4c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> StringTypeDefNames = new HashSet<string>(StringComparer.Ordinal)
private static readonly HashSet<string> ImplicitConversionTypeDefs = new HashSet<string>(StringComparer.Ordinal)
{
"PWSTR",
"PSTR",
"LPARAM",
"WPARAM",
};

private static readonly HashSet<string> SpecialTypeDefNames = new HashSet<string>(StringComparer.Ordinal)
Expand Down Expand Up @@ -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))))
Expand Down
10 changes: 10 additions & 0 deletions test/GenerationSandbox.Tests/GeneratedForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions test/GenerationSandbox.Tests/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ MainAVIHeader
EnumWindows
GetWindowTextLength
GetWindowText
WPARAM

0 comments on commit f9f0a4c

Please sign in to comment.