当打开的控制台窗口关闭时,如何防止我的程序关闭? [英] How can I prevent my program from closing when a open console window is closed?

查看:1427
本文介绍了当打开的控制台窗口关闭时,如何防止我的程序关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的主程序(Win32)打开控制台。我发现了一些代码,它的工作原理,但我不明白。我发现的问题是,当我在控制台上点击X,它也关闭程序。

I am trying to open the console from my main program (Win32). I found some code, and it works, but I don't understand it. The problem I'm happening is that when I click X on the console, it closes the program as well.

粗略我有这个:

int APIENTRY WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) {

  // create the main program window, classes registered, etc...
  hwnd = CreateWindowEx(0, csClassName, "theNewTimer", WS_POPUP | WS_CLIPCHILDREN, 300, 0, WINDOW_WIDTH, WINDOW_HEIGHT, HWND_DESKTOP, NULL, hThisInstance, NULL);
  ShowWindow (hwnd, nFunsterStil);

  // and now the console
  AllocConsole();
  HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
  int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
  FILE* hf_out = _fdopen(hCrt, "w");
  setvbuf(hf_out, NULL, _IONBF, 1);
  *stdout = *hf_out;

  HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
  hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
  FILE* hf_in = _fdopen(hCrt, "r");
  setvbuf(hf_in, NULL, _IONBF, 128);
  *stdin = *hf_in;


// and then the message loop concluding

I googled一些,但不知道我在读什么。

I googled some of it, but had no idea of what I was reading.

推荐答案

我不认为这是可能的。从MSDN文档中获取 <

I do not think this is possible. From the MSDN documentation for a HandlerRoutine, there's this sentence.


CTRL_CLOSE_EVENT

The CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT signals give the process an opportunity to clean up before termination.

我读这个说法, CTRL_CLOSE_EVENT 是建议,并且该过程将要退出。我的猜测是,当系统发送 CTRL_CLOSE_EVENT 时,它会启动一个计时器。

I read this as saying that CTRL_CLOSE_EVENT is advisory, and that the process is going to exit regardless. My guess is that when the system sends CTRL_CLOSE_EVENT, it starts a timer. The process is allowed to keep running for a little bit of time, but eventually, the OS will just kill the process unilaterally.

这是我注册的处理程序

BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType) {
   switch (dwCtrlType) {
       case CTRL_C_EVENT:
       case CTRL_CLOSE_EVENT:
           return TRUE; // breakpoint set here

       default:
           return FALSE;
   }
}

这是我如何注册处理程序, AllocConsole

Here's how I registered the handler, after my call to AllocConsole:

BOOL result = SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE /* Add */);
assert(result);

我在标记为 //的断点设置断点。然后,我在Visual Studio调试器下运行该进程。当控制台窗口聚焦时,我按Ctrl + C。我的断点被击中,我能够通过我的处理程序,并回到 KernelBase.dll!CtrlRoutine 等等。当我让进程恢复正常执行时,进程继续运行。

I set a breakpoint on the line marked //breakpoint set here. Then, I ran the process under the Visual Studio debugger. When the console window was focused, I pressed Ctrl+C. My breakpoint got hit, and I was able to step through my handler and back into KernelBase.dll!CtrlRoutine and so on. The process kept running when I let the process resume normal execution.

但是,当我关闭控制台窗口时,我的处理程序被调用,但是我无法跟踪执行非常远。我能够单步执行几次,但然后过程就退出了。 Visual Studio报告程序'[10644] Win32Project.exe'已退出与代码-1073741510(0xc000013a)。

However, when I closed the console window, my handler did get called, but I was unable to trace its execution very far. I was able to single step execution a few times, but then the process simply exited. Visual Studio reported "The program '[10644] Win32Project.exe' has exited with code -1073741510 (0xc000013a)."

0xc000013a是STATUS_CONTROL_C_EXIT:

0xc000013a is STATUS_CONTROL_C_EXIT:

c:\Tools\ERR>Err.exe 0xc000013a
# for hex 0xc000013a / decimal -1073741510 :
  STATUS_CONTROL_C_EXIT                                         ntstatus.h
# {Application Exit by CTRL+C}
# The application terminated as a result of a CTRL+C.
# 1 matches found for "0xc000013a"

这篇关于当打开的控制台窗口关闭时,如何防止我的程序关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆