CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005
    Posts
    266

    SetWindowsHookEx - Without Creating DLL

    Well i am sorry but i am trying to add a hook using SetWindowsHookEx
    so here is what i have
    Myproc:
    Code:
    LRESULT CALLBACK ChookDlg::MyPrc(int nCode,WPARAM wParam,LPARAM lParam){//...}
    and my function
    Code:
    g_hHook=SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)&ChookDlg::MyPrc,NULL,dwID);
    when i try to compile it gives me an error
    error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'HOOKPROC'
    None of the functions with this name in scope match the target type
    I don't know what to do
    Hmm i saw somewhere that it needs to be static but i need to use some variables of this ChookDlg and i can't (
    Last edited by kolkoo; October 6th, 2005 at 02:39 AM.

  2. #2
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: Maybe stupid question but...

    Your problem is probably that you haven't made MyPrc a static member function. You can't use a non-static member function for your hook procedure because its argument list won't match what's required; a non-static member function receives an additional parameter implicitly, the this pointer. To illustrate:
    Code:
    class C
    {
    public:
        LRESULT CALLBACK HookProc1(int nCode, WPARAM wParam, LPARAM lParam);
        static LRESULT CALLBACK HookProc2(int nCode, WPARAM wParam, LPARAM lParam);
    
        // ... rest of your class here...
    };
    
    // ... more code here...
    
    SetWindowsHookEx(WH_GETMESSAGE, C::HookProc1, NULL, dwID);  // ERROR: function is not of the correct type
    SetWindowsHookEx(WH_GETMESSAGE, C::HookProc2, NULL, dwID);  // OK: static member functions work fine

  3. #3
    Join Date
    Jul 2005
    Posts
    266

    Re: Maybe stupid question but...

    yeah my problem was that i couldn't use my member variables but i found a way to fix that 10x anyway

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