Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse .NET NativeOverlapped struct instead of generating our own #546

Merged
merged 1 commit into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ public class Generator : IDisposable

internal static readonly SyntaxAnnotation IsRetValAnnotation = new SyntaxAnnotation("RetVal");

/// <summary>
/// A map of .NET interop structs to use, keyed by the native structs that should <em>not</em> be generated.
/// </summary>
/// <devremarks>
/// When adding to this dictionary, consider also adding to <see cref="BannedAPIsWithoutMarshaling"/>.
/// </devremarks>
internal static readonly Dictionary<string, TypeSyntax> BclInteropStructs = new Dictionary<string, TypeSyntax>(StringComparer.Ordinal)
{
{ nameof(System.Runtime.InteropServices.ComTypes.FILETIME), ParseTypeName("global::System.Runtime.InteropServices.ComTypes.FILETIME") },
{ nameof(Guid), ParseTypeName("global::System.Guid") },
{ "OLD_LARGE_INTEGER", PredefinedType(Token(SyntaxKind.LongKeyword)) },
{ "LARGE_INTEGER", PredefinedType(Token(SyntaxKind.LongKeyword)) },
{ "ULARGE_INTEGER", PredefinedType(Token(SyntaxKind.ULongKeyword)) },
{ "OVERLAPPED", ParseTypeName("global::System.Threading.NativeOverlapped") },
};

internal static readonly Dictionary<string, TypeSyntax> AdditionalBclInteropStructsMarshaled = new Dictionary<string, TypeSyntax>(StringComparer.Ordinal)
Expand Down Expand Up @@ -372,7 +379,8 @@ private enum FriendlyOverloadOf
.Add("GetLastError", "Do not generate GetLastError. Call Marshal.GetLastWin32Error() instead. Learn more from https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error")
.Add("OLD_LARGE_INTEGER", "Use the C# long keyword instead.")
.Add("LARGE_INTEGER", "Use the C# long keyword instead.")
.Add("ULARGE_INTEGER", "Use the C# ulong keyword instead.");
.Add("ULARGE_INTEGER", "Use the C# ulong keyword instead.")
.Add("OVERLAPPED", "Use System.Threading.NativeOverlapped instead.");

internal static ImmutableDictionary<string, string> BannedAPIsWithMarshaling { get; } = BannedAPIsWithoutMarshaling
.Add("VARIANT", "Use `object` instead of VARIANT when in COM interface mode. VARIANT can only be emitted when emitting COM interfaces as structs.");
Expand Down
7 changes: 7 additions & 0 deletions test/GenerationSandbox.Tests/GeneratedForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Win32.SafeHandles;
using Windows.Win32;
using Windows.Win32.Foundation;
Expand Down Expand Up @@ -53,4 +54,10 @@ private static void PreserveSigBasedOnMetadata()
IDirectorySearch ds = null!;
HRESULT hr = ds.GetNextRow(0);
}

private static unsafe void OverlappedAPIs()
{
NativeOverlapped overlapped = default;
PInvoke.WriteFile(default(HANDLE), null, 0, null, &overlapped);
}
}
1 change: 1 addition & 0 deletions test/GenerationSandbox.Tests/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ ShellLink
WER_REPORT_INFORMATION
wglGetProcAddress
WPARAM
WriteFile