Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit aab338e

Browse files
committed
Avoid unnecessary SetLastError on PInvokes (dotnet/corefx#36544)
These console PInvokes are used in nearly every app. Omitting unnecessary SetLastError will save us from generating marshalling stub for them. Signed-off-by: dotnet-bot <[email protected]>
1 parent c007aec commit aab338e

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

src/System.Private.CoreLib/System.Private.CoreLib.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@
271271
<Compile Include="$(BclSourcesRoot)\System\ValueType.cs" />
272272
<Compile Include="$(BclSourcesRoot)\System\WeakReference.cs" />
273273
<Compile Include="$(BclSourcesRoot)\System\WeakReferenceOfT.cs" />
274+
<Compile Include="shared\Interop\Windows\Kernel32\Interop.HandleTypes.cs" />
274275
<Compile Include="shared\Interop\Windows\Kernel32\Interop.GetStdHandle.cs" />
275276
<Compile Include="shared\Interop\Windows\Kernel32\Interop.LocalAlloc.cs" />
276277
<Compile Include="shared\Interop\Windows\Kernel32\Interop.QueryUnbiasedInterruptTime.cs" />

src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetStdHandle.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ internal partial class Interop
99
{
1010
internal partial class Kernel32
1111
{
12-
internal const int STD_INPUT_HANDLE = -10;
13-
internal const int STD_OUTPUT_HANDLE = -11;
14-
internal const int STD_ERROR_HANDLE = -12;
15-
16-
[DllImport(Libraries.Kernel32, SetLastError = true)]
17-
internal static extern IntPtr GetStdHandle(int nStdHandle);
12+
[DllImport(Libraries.Kernel32)]
13+
internal static extern IntPtr GetStdHandle(int nStdHandle); // param is NOT a handle, but it returns one!
1814
}
1915
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
internal partial class Interop
6+
{
7+
internal partial class Kernel32
8+
{
9+
internal partial class HandleTypes
10+
{
11+
internal const int STD_INPUT_HANDLE = -10;
12+
internal const int STD_OUTPUT_HANDLE = -11;
13+
internal const int STD_ERROR_HANDLE = -12;
14+
}
15+
}
16+
}

src/System.Private.CoreLib/src/Internal/Console.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Internal
1616
public static class Console
1717
{
1818
private static readonly SafeFileHandle _outputHandle =
19-
new SafeFileHandle(Interop.Kernel32.GetStdHandle(Interop.Kernel32.STD_OUTPUT_HANDLE), ownsHandle: false);
19+
new SafeFileHandle(Interop.Kernel32.GetStdHandle(Interop.Kernel32.HandleTypes.STD_OUTPUT_HANDLE), ownsHandle: false);
2020

2121
public static unsafe void Write(string s)
2222
{

0 commit comments

Comments
 (0)