Skip to content

Commit

Permalink
Fixed #1008.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebattista committed Oct 11, 2022
1 parent 3f0e912 commit 603b80e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
8 changes: 7 additions & 1 deletion sources/MetadataUtils/ConstantWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void AddGuid(string name, string args)
this.Writer.WriteLine();
}

public void AddValue(string type, string name, string valueText)
public void AddValue(string type, string name, string valueText, string context = "")
{
this.namesToValues[name] = valueText;

Expand All @@ -83,6 +83,12 @@ public void AddValue(string type, string name, string valueText)
$" [{extraAttributes}]");
}

if (type == "string" && context == "ansi")
{
this.Writer.WriteLine(
$" [NativeEncoding(\"{context}\"]");
}

this.Writer.WriteLine(
$" public const {type} {name} = {valueText};");

Expand Down
9 changes: 6 additions & 3 deletions sources/MetadataUtils/ConstantsScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,15 @@ private void AddCtlCodeConstant(string originalNamespace, string name, string de
this.writtenConstants.Add(name, "uint");
}

private void AddConstantValue(string originalNamespace, string type, string name, string valueText)
private void AddConstantValue(string originalNamespace, string type, string name, string valueText, string context = "")
{
if (this.writtenConstants.ContainsKey(name))
{
return;
}

var writer = this.GetConstantWriter(originalNamespace, name);
writer.AddValue(type, name, valueText);
writer.AddValue(type, name, valueText, context);

this.writtenConstants.Add(name, type);
}
Expand Down Expand Up @@ -773,8 +773,10 @@ private void ScrapeConstantsFromTraversedFiles(Dictionary<string, string> traver
valueText = valueText.Substring(2);
}

bool isUtf16 = false;
if (valueText.StartsWith("TEXT("))
{
isUtf16 = true;
valueText = valueText.Substring("TEXT(".Length);
if (valueText.EndsWith(')'))
{
Expand All @@ -783,13 +785,14 @@ private void ScrapeConstantsFromTraversedFiles(Dictionary<string, string> traver
}
else if (valueText.StartsWith("L\""))
{
isUtf16 = true;
valueText = valueText.Substring(1);
}

// Strings can't be part of enums so go ahead and add the constant directly
if (valueText.StartsWith('"'))
{
this.AddConstantValue(currentNamespace, "string", name, valueText);
this.AddConstantValue(currentNamespace, "string", name, valueText, isUtf16 ? "utf-16" : "ansi");
continue;
}

Expand Down
22 changes: 22 additions & 0 deletions sources/Win32MetadataInterop/NativeEncodingAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Diagnostics;

namespace Windows.Win32.Interop
{
/// <summary>Defines the encoding of a string as it was defined in the native signature.</summary>
//[Conditional("DEBUG")]
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class NativeEncodingAttribute : Attribute
{
/// <summary>Initializes a new instance of the <see cref="NativeEncodingAttribute" /> class.</summary>
/// <param name="name">The encoding of a string as it was defined in the native signature.</param>
public NativeEncodingAttribute(String name)
{
this.Name = name;
}

/// <summary>Gets the encoding of a string as it was defined in the native signature.</summary>
public string Name { get; }
}
}

0 comments on commit 603b80e

Please sign in to comment.