我正在使用此代码直接从控制台读取。
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)]