gpt4 book ai didi

.net - ReadConsoleOutputCharacter 不适用于德语变音符号

转载 作者:行者123 更新时间:2023-12-02 00:19:39 35 4
gpt4 key购买 nike

我正在使用此代码直接从控制台读取数据。

    public char? ReadCharacterAt(int x, int y)
{
IntPtr consoleHandle = GetStdHandle(-11);
if (consoleHandle == IntPtr.Zero)
{
return null;
}
var position = new Coord
{
X = (short)x,
Y = (short)y
};
var result = new StringBuilder(1);
uint read = 0;
if (ReadConsoleOutputCharacter(consoleHandle, result, 1, position, out read))
{
return result[0];
}
else
{
return null;
}
}

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadConsoleOutputCharacter(IntPtr hConsoleOutput, [Out] StringBuilder lpCharacter, uint length, Coord bufferCoord, out uint lpNumberOfCharactersRead);

[StructLayout(LayoutKind.Sequential)]
public struct Coord
{
public short X;
public short Y;
}

但是,对于德语变音符号 (äüö),这不会返回正确的字符。我想知道我该如何解决这个问题?

更新

正如 Hans 和 Raymond 所指出的,答案在于 DLL 导入。我所要做的就是将其更改为:

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]

最佳答案

来自 SDK 文档:

This function uses either Unicode characters or 8-bit characters from the console's current code page. The console's code page defaults initially to the system's OEM code page. To change the console's code page, use the SetConsoleCP or SetConsoleOutputCP functions, or use the chcp or mode con cp select= commands.

您将获得 8 位字符,因为您没有在 [DllImport] 属性中指定 CharSet。从技术上讲,您可以通过读入 byte[] 并使用 Console.OutputEncoding.GetString() 进行转换来使其工作。但是不要,使用 CharSet.Unicode 让 Windows 为您进行转换。

关于.net - ReadConsoleOutputCharacter 不适用于德语变音符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11484271/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com