Skip to content

Commit 4e9478f

Browse files
committed
Reuse .NET NativeOverlapped struct instead of generating our own
Closes #545
1 parent 7e763f9 commit 4e9478f

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/Microsoft.Windows.CsWin32/Generator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,20 @@ public class Generator : IDisposable
4141

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

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

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

377385
internal static ImmutableDictionary<string, string> BannedAPIsWithMarshaling { get; } = BannedAPIsWithoutMarshaling
378386
.Add("VARIANT", "Use `object` instead of VARIANT when in COM interface mode. VARIANT can only be emitted when emitting COM interfaces as structs.");

test/GenerationSandbox.Tests/GeneratedForm.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Runtime.InteropServices;
5+
using System.Threading;
56
using Microsoft.Win32.SafeHandles;
67
using Windows.Win32;
78
using Windows.Win32.Foundation;
@@ -53,4 +54,10 @@ private static void PreserveSigBasedOnMetadata()
5354
IDirectorySearch ds = null!;
5455
HRESULT hr = ds.GetNextRow(0);
5556
}
57+
58+
private static unsafe void OverlappedAPIs()
59+
{
60+
NativeOverlapped overlapped = default;
61+
PInvoke.WriteFile(default(HANDLE), null, 0, null, &overlapped);
62+
}
5663
}

test/GenerationSandbox.Tests/NativeMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ ShellLink
2121
WER_REPORT_INFORMATION
2222
wglGetProcAddress
2323
WPARAM
24+
WriteFile

0 commit comments

Comments
 (0)