From 0642a66a86923c4f3badc5307733360cc7af7988 Mon Sep 17 00:00:00 2001 From: misaz Date: Wed, 23 Jul 2025 23:18:01 +0200 Subject: [PATCH 1/2] Add support for custom DebuggerDisplay instead of directy referencing field name --- src/Microsoft.Windows.CsWin32/Generator.TypeDef.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Windows.CsWin32/Generator.TypeDef.cs b/src/Microsoft.Windows.CsWin32/Generator.TypeDef.cs index 001a829b..d827e344 100644 --- a/src/Microsoft.Windows.CsWin32/Generator.TypeDef.cs +++ b/src/Microsoft.Windows.CsWin32/Generator.TypeDef.cs @@ -239,12 +239,22 @@ SyntaxList AddOrReplaceMembers(SyntaxList x is PropertyDeclarationSyntax && ((PropertyDeclarationSyntax)x).Identifier.ValueText == "DebuggerDisplay").Any()) + { + debuggerDisplay = "{DebuggerDisplay,nq}"; + } + else + { + debuggerDisplay = "{" + fieldName + "}"; + } + structModifiers = structModifiers.Add(TokenWithSpace(SyntaxKind.ReadOnlyKeyword)).Add(TokenWithSpace(SyntaxKind.PartialKeyword)); StructDeclarationSyntax result = StructDeclaration(name.Identifier) .WithBaseList(BaseList(SingletonSeparatedList(SimpleBaseType(GenericName(nameof(IEquatable), TypeArgumentList().WithGreaterThanToken(TokenWithLineFeed(SyntaxKind.GreaterThanToken))).AddTypeArgumentListArguments(name)))).WithColonToken(TokenWithSpace(SyntaxKind.ColonToken))) .WithMembers(members) .WithModifiers(structModifiers) - .AddAttributeLists(AttributeList().WithCloseBracketToken(TokenWithLineFeed(SyntaxKind.CloseBracketToken)).AddAttributes(DebuggerDisplay("{" + fieldName + "}"))); + .AddAttributeLists(AttributeList().WithCloseBracketToken(TokenWithLineFeed(SyntaxKind.CloseBracketToken)).AddAttributes(DebuggerDisplay(debuggerDisplay))); result = this.AddApiDocumentation(name.Identifier.ValueText, result); return result; From 66554c091a675b368d89b03c5e75e6325713e6ab Mon Sep 17 00:00:00 2001 From: misaz Date: Wed, 23 Jul 2025 23:18:55 +0200 Subject: [PATCH 2/2] Add custom DebuggerDisplay for HRESULT showing humman readable error message for error code --- src/Microsoft.Windows.CsWin32/templates/HRESULT.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Microsoft.Windows.CsWin32/templates/HRESULT.cs b/src/Microsoft.Windows.CsWin32/templates/HRESULT.cs index ba0d3a68..14d86119 100644 --- a/src/Microsoft.Windows.CsWin32/templates/HRESULT.cs +++ b/src/Microsoft.Windows.CsWin32/templates/HRESULT.cs @@ -61,6 +61,19 @@ internal HRESULT ThrowOnFailure(IntPtr errorInfo = default) return this; } + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + private string DebuggerDisplay + { + get + { +#if NET7_0_OR_GREATER + return string.Format("{0} {1}", ToString(), global::System.Runtime.InteropServices.Marshal.GetPInvokeErrorMessage((int)this.Value)); +#else + return string.Format("{0} {1}", ToString(), new global::System.ComponentModel.Win32Exception((int)this.Value).Message); +#endif + } + } + public override string ToString() => string.Format(global::System.Globalization.CultureInfo.InvariantCulture, "0x{0:X8}", this.Value); internal string ToString(string format, IFormatProvider formatProvider) => ((uint)this.Value).ToString(format, formatProvider);