在C#中使用控制台窗口时出现异常(Windows窗体应用程序)

时间:2017-03-05 00:40:25

标签: c# console

我有一个程序WFA也有和命令窗口。我用AllocConsole()打开窗口;当我关闭控制台窗口时,我使用FreeConsole();但是当我用AllocConsole()再次打开它时;我想写它并从中读取它会引发一个例外。

代码:

    namespace WindowsFormsApplication2
{

class classx
{

    [DllImport("kernel32.dll")]
    public static extern Int32 AllocConsole();
    [DllImport("kernel32.dll")]
    public static extern bool FreeConsole();
    [DllImport("kernel32")]
    public static extern bool AttachConsole();
    [DllImport("kernel32")]
    public static extern bool GetConsoleWindow();
    public static bool z = false;
    [DllImport("kernel32")]
    public static extern bool SetConsoleCtrlHandler(HandlerRoutine HandlerRoutine, bool Add);
    public delegate bool HandlerRoutine(uint dwControlType);
}



public partial class Form1 : Form
{
    NotifyIcon icontask;
    Icon iconone_active;
    Icon iconone_inactive;
    /*Icon icontwo;
    Icon iconthree;
    Icon iconfour;
    Icon iconfive;*/
    Thread Threadworkermy;

    public Form1()
    {
        InitializeComponent();
        this.WindowState = FormWindowState.Minimized;
        this.ShowInTaskbar = false;
        iconone_active = new Icon(".../iconone_active.ico");
        iconone_inactive = new Icon(".../iconone_inactive.ico");
        icontask = new NotifyIcon();
        icontask.Icon = iconone_active;
        icontask.Visible = true;
        Threadworkermy = new Thread(new ThreadStart(checkActivityThread));
        Threadworkermy.Start();

        MenuItem Nameapp = new MenuItem("xr");
        MenuItem quitappitem = new MenuItem("quit program");
        MenuItem OpenGUI = new MenuItem("Open GUI");
        MenuItem Advancedmodewindow = new MenuItem("x");
        ContextMenu contextmenu = new ContextMenu();

        quitappitem.Click += quitappitem_click;
        OpenGUI.Click += OpenGUI_click;
        Advancedmodewindow.Click += Advancedmodewindow_click;
        contextmenu.MenuItems.Add(Nameapp);
        contextmenu.MenuItems[0].Enabled = false;
        contextmenu.MenuItems.Add("-");
        contextmenu.MenuItems.Add(OpenGUI);
        contextmenu.MenuItems.Add(Advancedmodewindow);
        contextmenu.MenuItems.Add("-");
        contextmenu.MenuItems.Add(quitappitem);
        icontask.ContextMenu = contextmenu;

        icontask.Icon = iconone_active;
        icontask.Visible = true;
    }

    private void Advancedmodewindow_click(object sender, EventArgs e)
    {
        classx.AllocConsole();
        Console.WriteLine("X");
        classx.FreeConsole();
    }

    private void OpenGUI_click(object sender, EventArgs e)
    {
        this.ShowInTaskbar = true;
        this.WindowState = FormWindowState.Normal;  
    }

    private void quitappitem_click(object sender, EventArgs e)
    {
        Threadworkermy.Abort();
        icontask.Dispose();
        this.Close();
    }

    public void checkActivityThread()
    {
        try
        {
            while(true)
            {
                Thread.Sleep(100);   
            }
        } catch(ThreadAbortException tbe)
        {

        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.WindowState = FormWindowState.Minimized;
        this.ShowInTaskbar = false;
    }


}

}

它抛出的异常' System.IO.IOException'在mscorlib.dll中 附加信息:句柄无效。

对于那些要改变类型的人,我不能。 (需要WFA申请)

1 个答案:

答案 0 :(得分:1)

似乎存在破坏consolewindow的问题,所以你可以隐藏它。

要隐藏窗口,您需要从user32.dll添加一个额外的DllImport,并将GetConsoleWindow的返回值更改为IntPtr:

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();

现在检查控制台句柄是否已存在。如果它确实显示控制台,否则创建控制台窗口:

private void Advancedmodewindow_click(object sender, EventArgs e)
{
    IntPtr handle = classx.GetConsoleWindow();
    if (handle == IntPtr.Zero)
    {
        classx.AllocConsole();
        handle = classx.GetConsoleWindow();
    }
    else
    {
        //shows the window with the given handle
        classx.ShowWindow(handle, 8);
    }
    Console.WriteLine("X");
    //hides the window with the given handle
    classx.ShowWindow(handle, 0);
}

原始解决方案可以在这里找到:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/cdee5d88-3325-47ce-9f6b-83aa4447f8ca/console-exception-on-windows-8?forum=clr