/
Console.Windows.cs
44 lines (38 loc) · 1.35 KB
/
Console.Windows.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Text;
namespace Internal
{
public static partial class Console
{
public static void Write(string s)
{
WriteCore(Interop.Kernel32.GetStdHandle(Interop.Kernel32.HandleTypes.STD_OUTPUT_HANDLE), s);
}
public static partial class Error
{
public static void Write(string s)
{
WriteCore(Interop.Kernel32.GetStdHandle(Interop.Kernel32.HandleTypes.STD_ERROR_HANDLE), s);
}
}
private static unsafe void WriteCore(IntPtr handle, string s)
{
int bufferSize = s.Length * 4;
Span<byte> bytes = bufferSize < 1024 ? stackalloc byte[bufferSize] : new byte[bufferSize];
int cbytes;
fixed (char* pChars = s)
fixed (byte* pBytes = bytes)
{
cbytes = Interop.Kernel32.WideCharToMultiByte(
Interop.Kernel32.GetConsoleOutputCP(),
0, pChars, s.Length, pBytes, bytes.Length, null, null);
}
fixed (byte* pBytes = bytes)
{
Interop.Kernel32.WriteFile(handle, pBytes, cbytes, out _, IntPtr.Zero);
}
}
}
}