I am having problems compiling code that simply allows me to install a default HandlerRoutine(). I want the user to be able to remove this handler or install their own. I have condensed the issue into a separate class to demonstrate.
vmConsoleEvent.h
Code:
#ifndef __VMCONSOLE_EVENT_H__
#define __VMCONSOLE_EVENT_H__
#include <windows.h>
namespace vmStd {
class vmConsoleEvent {
public:
vmConsoleEvent();
~vmConsoleEvent();
BOOL RegisterEventHandler(PHANDLER_ROUTINE *pHandlerRoutine);
BOOL RemoveEventHandler(PHANDLER_ROUTINE *pHandlerRoutine);
protected:
BOOL DefaultConsoleHandler(DWORD dwCtrlType);
private:
};
} // namespace vmStd
#endif // __VMCONSOLE_EVENT_H__
vmConsoleEvent.cpp
Code:
#include "vmConsoleEvent.h"
using namespace vmStd;
vmConsoleEvent::vmConsoleEvent()
{
// Error always here
RegisterEventHandler((PHANDLER_ROUTINE) DefaultConsoleHandler);
}
vmConsoleEvent::~vmConsoleEvent()
{
// Empty
}
BOOL vmConsoleEvent::RegisterEventHandler(PHANDLER_ROUTINE *pHandlerRoutine)
{
if(::SetConsoleCtrlHandler((PHANDLER_ROUTINE) pHandlerRoutine, TRUE) == false) {
MessageBox(NULL, TEXT("Error registering event handler."),
TEXT("vmAPI ERROR - vmConsoleEvent::RegisterEventHandler()"),
MB_ICONERROR | MB_OK | MB_TASKMODAL);
return FALSE;
}
return TRUE;
}
BOOL vmConsoleEvent::RemoveEventHandler(PHANDLER_ROUTINE *pHandlerRoutine)
{
if(::SetConsoleCtrlHandler((PHANDLER_ROUTINE) pHandlerRoutine, FALSE) == false) {
MessageBox(NULL, TEXT("Error removing event handler."),
TEXT("vmAPI ERROR - vmConsoleEvent::RemoveEventHandler()"),
MB_ICONERROR | MB_OK | MB_TASKMODAL);
return FALSE;
}
return TRUE;
}
BOOL vmConsoleEvent::DefaultConsoleHandler(DWORD dwCtrlType)
{
switch (dwCtrlType) {
case CTRL_C_EVENT:
MessageBox(NULL, TEXT("Ctrl_C event trapped."),
TEXT("vmAPI Event - vmConsoleEvent::ConsoleHandler()"),
MB_ICONINFORMATION | MB_OK | MB_TASKMODAL);
// Signal is handled - don't pass it on to the next handler
return TRUE;
default:
// Pass signal on to the next handler
return FALSE;
}
}
As this code stands, I receive a compiler error: Invalid use of member function (did you forget the '()' ?) within the C'tor.
I wanted to just call RegisterEventHandler(DefaultConsoleHandler) in the C'tor but if I do that then I get error: no matching function for call to 'vmStd::vmConsoleEvent::RegisterEventHandler(<unresolved overloaded function type>)'candidate is: note: BOOL vmStd::vmConsoleEvent::RegisterEventHandler(BOOL (__attribute__((__stdcall__)) **)(DWORD))| note: no known conversion for argument 1 from '<unresolved overloaded function type>' to 'BOOL (__attribute__((__stdcall__)) **)(DWORD) {aka int (__attribute__((__stdcall__)) **)(long unsigned int)}'
One thing that really baffles me is where is DefaultConsoleHandler(DWORD dwCtrlType) actually getting the CtrlType from in order for the switch/case to work? I know that the (PHANDLER_ROUTINE) is actually typedef BOOL(CALLBACK*PHANDLER_ROUTINE) (DWORD). This means absolutely nothing to me.