c#到Mono的GetConsoleWindow异常

C# to Mono GetConsoleWindow Exception

本文关键字:GetConsoleWindow 异常 Mono      更新时间:2023-10-16

我有一些麻烦设置我的应用程序单声道,它给了我一个GetConsoleWindow异常。我以为这会打开一个新窗口,当你在windows上运行它,但在Mono,在CentOS 6与Gnome它给了我这个例外。

错误代码:

Unhandled Exception: System.EntryPointNotFoundException: GetConsoleWindow at (wrapper managed-to-native) Silverwave.Program:GetConsoleWindow () at Silverwave.Program.Main (System.String[] Args) [0x00000] in <filename unkno wn>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.EntryPointNotFoundException: GetConsol eWindow at (wrapper managed-to-native) Silverwave.Program:GetConsoleWindow () at Silverwave.Program.Main (System.String[] Args) [0x00000] in <filename unkno wn>:0 [root@h2297404 Debug]# Unhandled Exception: -bash: Unhandled: opdracht niet gevonden [root@h2297404 Debug]# System.EntryPointNotFoundException: GetConsoleWindow -bash: System.EntryPointNotFoundException:: opdracht niet gevonden

thanks in advance

控制台窗口是特定于windows系统的(这就是为什么GetConsoleWindow()在。net中不存在,您必须p/调用它)。

Linux系统不公开这个API,所以你不能在Linux上调用它,也没有替代品。

简短的回答-使用控制台。WindowWidth

长答:

https://unix.stackexchange.com/questions/215584/whats-the-name-of-the-environment-variable-with-current-terminal-width

$COLUMNS就是这样一个变量。还有stty -a或者更具体地说是stty size

stty使用

    get_terminal_width_height(STDIN_FILENO, &width, &height)

调用

    ioctl(fd, TIOCGWINSZ, &winsize);

TIOCGWINSZ

用屏幕宽度和高度填充第三个参数指向的winsize结构。winsize结构在' sys/ioctl.h'中定义如下:

struct winsize
{
  unsigned short ws_row;    /* rows, in characters */
  unsigned short ws_col;    /* columns, in characters */
  unsigned short ws_xpixel; /* horizontal size, pixels */
  unsigned short ws_ypixel; /* vertical size, pixels */
};

在mono中叫做

https://github.com/mono/mono/blob/90972a343b81489e333a379e13f3e0018738e705/mono/metadata/console-unix.c L177

导出为"TtySetup"

https://github.com/mono/mono/blob/92a0ac52a7018199f39f01625eb1025baf564743/mono/metadata/icall-def.h L130

在这里导入到c#代码中

https://github.com/mono/mono/blob/88d2b9da2a87b4e5c82abaea4e5110188d49601d/mcs/class/corlib/System/ConsoleDriver.cs L286

namespace System {
    static class ConsoleDriver {
    ...
    [MethodImplAttribute(MethodImplOptions.InternalCall)]
    unsafe internal static extern bool TtySetup (
        string keypadXmit, string teardown, 
        out byte [] control_characters, out int *address);

在这里被称为-https://github.com/mono/mono/blob/88d2b9da2a87b4e5c82abaea4e5110188d49601d/mcs/class/corlib/System/TermInfoDriver.cs L227

不安全的void CheckWindowDimensions ()

https://github.com/mono/mono/blob/88d2b9da2a87b4e5c82abaea4e5110188d49601d/mcs/class/corlib/System/TermInfoDriver.cs L688

可以通过属性WindowWidth:

namespace System {
class TermInfoDriver : IConsoleDriver {
public int WindowWidth {

https://github.com/mono/mono/blob/88d2b9da2a87b4e5c82abaea4e5110188d49601d/mcs/class/corlib/System/IConsoleDriver.cs预示

最终它应该可以通过https://github.com/mono/mono/blob/92a0ac52a7018199f39f01625eb1025baf564743/mcs/class/corlib/System/Console.cs L660

我检查了一下,这是有效的(至少从mono 4.0.2.5开始)。

using System;
class mainclass {
public static int Main(string[] args) {
Console.WriteLine(Console.WindowWidth);
return 0; } }
sabayon aa $ mcs test.cs -r:System.dll
sabayon aa $ mono test.exe 
239
相关文章:
  • 没有找到相关文章