在控制台程序终止时调用特定的函数

时间:2022-06-22 20:30:33

I'm on windows and I want to call a specific function when the program terminates.

我在windows上,我想在程序终止时调用一个特定的函数。

For example:

例如:

void close_program()
{
    /*do stuff*/
    printf("Goodbye.\n");
}

I tried atexit() but it only worked on casual and regular exits such as a call to the function exit() or when the main returns a value to the OS.

我尝试了atexit(),但它只适用于临时的和常规的出口,比如对函数exit()的调用,或者当main返回一个值给操作系统时。

I found out that HandlerRoutine() seems like a solution as windows sends a CTRL_CLOSE_EVENT value signal to the process closed by the user (either just close button or end task through task manager). The problem is I tried a really basic piece of code and it said 'undefined reference to HandlerRoutine' and that it returned 1.

我发现HandlerRoutine()似乎是一个解决方案,因为windows向用户关闭的进程发送了一个CTRL_CLOSE_EVENT值信号(只需关闭按钮或通过task manager结束任务)。问题是我尝试了一段非常基本的代码,它说“对HandlerRoutine的未定义引用”,它返回1。

The piece of code:

段代码:

#include <stdio.h>
#include <windows.h>

int main()
{
    while(1)
    {
         if(HandlerRoutine(CTRL_CLOSE_EVENT))
         {
              printf("Program is being terminated...\n");
         }
     }
     return 0;
}

I use MinGW.

我使用MinGW。

Any idea what is the problem might be ?

你知道问题出在哪里吗?

According to MSDN there is no need for linkage.

根据MSDN,不需要链接。

3 个解决方案

#1


0  

From the MSDN page you linked

从你链接的MSDN页面

HandlerRoutine is a placeholder for the application-defined function name.

HandlerRoutine是应用程序定义的函数名的占位符。

What you need to do is create a callback (of PHANDLER_ROUTINE type) and then use SetConsoleCtrlHandler to register this callback.

您需要做的是创建一个回调(PHANDLER_ROUTINE类型),然后使用setcomfort ectrlhandler来注册这个回调。

#2


3  

HandlerRoutine is the callback function that will be invoked when console will be terminated. It's not the function you have to call but the signature (defined as HANDLER_ROUTINE) of your function (that will be invoked by Windows itself):

HandlerRoutine是回调函数,当控制台终止时将调用它。它不是必须调用的函数,而是函数的签名(定义为HANDLER_ROUTINE) (Windows本身将调用它):

BOOL WINAPI HandlerRoutine(DWORD dwCtrlType);

You'll inspect dwCtrlType to check for CTRL_CLOSE_EVENT returning (usually) TRUE. To attach your function and make it called you have to use SetConsoleCtrlHandler() API function, like this:

您将检查dwctrl_close_event返回是否为TRUE。要附加你的函数并调用它,你必须使用setcomfort ectrlhandler () API函数,如下所示:

BOOL YourHandler(DWORD dwCtrlType)
{
    if (CTRL_CLOSE_EVENT == dwCtrlType)
    {
    }

    return TRUE;
}

Now you have your function but you instruct Windows to call it:

现在你有了你的功能,但你命令Windows调用它:

int main()
{
    SetConsoleCtrlHandler((PHANDLER_ROUTINE)YourHandler, TRUE);

    // Do your stuff here

    return 0;
}

Please note that you can register more than one handler, they'll be called in chain up to the one that returns TRUE. For a complete example just consult MSDN.

请注意,您可以注册多个处理程序,它们将被以链接的形式调用,直到返回TRUE。对于完整的示例,请参考MSDN。

#3


0  

Try including wincon.h explicitly. I know there are a number of child header files that are automatically included with windows.h but many of these files cannot simply be included by themselves (they are not self-contained), because of dependencies. wincon.h is one such child header file used for console services.

试包括wincon。显式h。我知道有许多子头文件自动包含在windows中。h但是由于依赖关系,这些文件中的许多不能简单地由它们自己包含(它们不是自包含的)。wincon。h是一种用于控制台服务的子头文件。

#1


0  

From the MSDN page you linked

从你链接的MSDN页面

HandlerRoutine is a placeholder for the application-defined function name.

HandlerRoutine是应用程序定义的函数名的占位符。

What you need to do is create a callback (of PHANDLER_ROUTINE type) and then use SetConsoleCtrlHandler to register this callback.

您需要做的是创建一个回调(PHANDLER_ROUTINE类型),然后使用setcomfort ectrlhandler来注册这个回调。

#2


3  

HandlerRoutine is the callback function that will be invoked when console will be terminated. It's not the function you have to call but the signature (defined as HANDLER_ROUTINE) of your function (that will be invoked by Windows itself):

HandlerRoutine是回调函数,当控制台终止时将调用它。它不是必须调用的函数,而是函数的签名(定义为HANDLER_ROUTINE) (Windows本身将调用它):

BOOL WINAPI HandlerRoutine(DWORD dwCtrlType);

You'll inspect dwCtrlType to check for CTRL_CLOSE_EVENT returning (usually) TRUE. To attach your function and make it called you have to use SetConsoleCtrlHandler() API function, like this:

您将检查dwctrl_close_event返回是否为TRUE。要附加你的函数并调用它,你必须使用setcomfort ectrlhandler () API函数,如下所示:

BOOL YourHandler(DWORD dwCtrlType)
{
    if (CTRL_CLOSE_EVENT == dwCtrlType)
    {
    }

    return TRUE;
}

Now you have your function but you instruct Windows to call it:

现在你有了你的功能,但你命令Windows调用它:

int main()
{
    SetConsoleCtrlHandler((PHANDLER_ROUTINE)YourHandler, TRUE);

    // Do your stuff here

    return 0;
}

Please note that you can register more than one handler, they'll be called in chain up to the one that returns TRUE. For a complete example just consult MSDN.

请注意,您可以注册多个处理程序,它们将被以链接的形式调用,直到返回TRUE。对于完整的示例,请参考MSDN。

#3


0  

Try including wincon.h explicitly. I know there are a number of child header files that are automatically included with windows.h but many of these files cannot simply be included by themselves (they are not self-contained), because of dependencies. wincon.h is one such child header file used for console services.

试包括wincon。显式h。我知道有许多子头文件自动包含在windows中。h但是由于依赖关系,这些文件中的许多不能简单地由它们自己包含(它们不是自包含的)。wincon。h是一种用于控制台服务的子头文件。