Currently, I’m spawning a message box with a OS-library function (Windows.h), which magically keeps my program alive and responding to calls to the callback function.
What alternative approach could be used to silently let the program run forever?
Trapping ‘Ctrl-c’ or SIGINT and subsequently calling RemoveHook() for a clean exit would be nice to have but is not essential.
HOOK my_hook;
CALLBACK my_callback_fn()
{
...
}
int main()
{
my_hook = SetHook(my_callback_fn);
MessageBox("Press OK to stop."); // This is bad.
RemoveHook(my_hook);
return 0;
}
You probably want to pump messages. A typical message loop looks like something like this:
You don’t seem to have an actual window to pump on, and you don’t say what
SetHookis actually doing – maybe that will be able to provide one for you?Another method is to use the
MsgWaitfunctions. Maybe you have some kind of handle that you are waiting to become signalled so you can exit?: