CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    [RESOLVED] Console Ctrl Event handler

    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.
    What the mind can conceive it can achieve.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Console Ctrl Event handler

    Perhaps, the HandlerRoutine must be global or static class member function?
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Re: Console Ctrl Event handler

    Quote Originally Posted by VictorN View Post
    Perhaps, the HandlerRoutine must be global or static class member function?
    As soon as you replied Victor, I was on the case. I know your right, it needs to be a static class member function. I have changed ONLY the definition within the vmConsoleEvent class to static BOOL DefaultConsoleHandler(DWORD dwCtrlType); and it's public now. Then I just changed the C'tor in vmConsoleEvent.cpp to:

    Code:
     vmConsoleEvent::vmConsoleEvent(){
       RegisterEventHandler(::DefaultConsoleHandler);
    }
    I get an error on the same line stating:


    vmConsoleEvent.cpp In constructor 'vmStd::vmConsoleEvent::vmConsoleEvent()':
    error: DefaultConsoleHandler' has not been declared

    Have I not fully declared the static member function, or is this something to do with the fact that all my classes are within the namespace vmStd? i.e. Is this some sort of linkage issue?
    What the mind can conceive it can achieve.

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Console Ctrl Event handler

    The method still remains to be a class member, so compiler reasonably complains there's no global :: DefaultConsoleHandler exists.
    Code:
     vmConsoleEvent::vmConsoleEvent(){
       RegisterEventHandler(vmConsoleEvent::DefaultConsoleHandler);
    }
    Best regards,
    Igor

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Console Ctrl Event handler

    :: means the global space while your function was declared as a class static member!
    Try
    Code:
       RegisterEventHandler(vmConsoleEvent::DefaultConsoleHandler);
    or just
    Code:
       RegisterEventHandler(DefaultConsoleHandler);
    Victor Nijegorodov

  6. #6
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Re: Console Ctrl Event handler

    I tried both of those suggestions before I posted. Because I got errors, I used the :: to see if the previous errors went away. If I use RegisterEventHandler(DefaultConsoleHandler); in the c'tor I get this:

    vmConsoleEvent.cpp - In constructor 'vmStd::vmConsoleEvent::vmConsoleEvent()':
    vmConsoleEvent.cpp error: no matching function for call to 'vmStd::vmConsoleEvent::RegisterEventHandler(BOOL (&)(DWORD))' vmConsoleEvent.cpp note: candidate is:

    vmConsoleEvent.h note: BOOL vmStd::vmConsoleEvent::RegisterEventHandler(BOOL (__attribute__((__stdcall__)) **)(DWORD))
    vmConsoleEvent.h note: no known conversion for argument 1 from 'BOOL(DWORD) {aka int(long unsigned int)}' to 'BOOL (__attribute__((__stdcall__)) **)(DWORD) {aka int (__attribute__((__stdcall__)) **)(long unsigned int)}'|

    The same errors occur if I use: RegisterEventHandler(vmConsoleEvent:: DefaultConsoleHandler);

    Issue now points to RegisterEventHandler() ?
    Last edited by Gerald Bates; December 10th, 2014 at 08:13 AM.
    What the mind can conceive it can achieve.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Console Ctrl Event handler

    Try to change the signature of RegisterEventHandler from
    Code:
    BOOL RegisterEventHandler(PHANDLER_ROUTINE *pHandlerRoutine);
    to
    Code:
    BOOL RegisterEventHandler(PHANDLER_ROUTINE pHandlerRoutine);
    Victor Nijegorodov

  8. #8
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Re: Console Ctrl Event handler

    Still produces errors?

    ||=== Build: Debug in vmConsole (compiler: GNU GCC Compiler) ===|
    D:\Programming\Projects\2015\vmConsole\src\vmConsoleEvent.cpp||In constructor 'vmStd::vmConsoleEvent::vmConsoleEvent()':|
    D:\Programming\Projects\2015\vmConsole\src\vmConsoleEvent.cpp|20|error: invalid conversion from 'BOOL (*)(DWORD) {aka int (*)(long unsigned int)}' to 'PHANDLER_ROUTINE {aka int (__attribute__((__stdcall__)) *)(long unsigned int)}' [-fpermissive]|
    include\vmConsoleEvent.h|21|error: initializing argument 1 of 'BOOL vmStd::vmConsoleEvent::RegisterEventHandler(PHANDLER_ROUTINE)' [-fpermissive]|

    Will keep trying to see if I can get this to compile, but I will be only trying anything as I don't have a clue what the errors mean?
    What the mind can conceive it can achieve.

  9. #9
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Re: Console Ctrl Event handler

    Victor, I have removed the pointer from signature and also cast the call in order to get this to compile without errors. Only problem now is that the CTRL signals are not being trapped.

    The class definition now:

    Code:
    class vmConsoleEvent {
    public:
       vmConsoleEvent();
       ~vmConsoleEvent();
    
    
       BOOL RegisterEventHandler(PHANDLER_ROUTINE pHandlerRoutine);
       BOOL RemoveEventHandler(PHANDLER_ROUTINE pHandlerRoutine);
    
    
       static BOOL DefaultConsoleHandler(DWORD dwCtrlType);
    
    
    private:
    
    
    };
    Implementation:

    Code:
    vmConsoleEvent::vmConsoleEvent()
    {
       RegisterEventHandler((PHANDLER_ROUTINE) DefaultConsoleHandler);  // *** Cast ? ***
    }
    What the mind can conceive it can achieve.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Console Ctrl Event handler

    Quote Originally Posted by Gerald Bates View Post
    Victor, I have removed the pointer from signature and also cast the call in order to get this to compile without errors. Only problem now is that the CTRL signals are not being trapped.
    Well, now see the example in MSDN: http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    BTW, how are you using your class? Show the rest of your code!
    Victor Nijegorodov

  11. #11
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Re: Console Ctrl Event handler

    Looked at your link and I was missing the infinite loop. Now I get a messageBox() displaying what is trapped.

    A few seconds later I get a windows XP End program box pop up? Will put these methods back into the vmConsole class and test.

    Many thanks for all your help, appreciated.
    What the mind can conceive it can achieve.

  12. #12
    Join Date
    Jan 2009
    Location
    England
    Posts
    57

    Re: Console Ctrl Event handler

    I have had a design change and now ignore the CTRL signals within my DefaultConsoleHandler(). Instead these messages will be passed into an event queue for the application to deal with. So far all is working okay but I am missing a crucial class in order to make my program work as intended. That's a subject for my next post.

    Many thanks again to everyone, appreciated.
    What the mind can conceive it can achieve.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured