Why doesnt SetWindowsHookEx accept the hook procedure?

筅森魡賤 提交于 2020-01-03 00:52:30

问题


I am trying to create a dll where i can use to monitor all of system events (process creation, destruction , etc) This is what i have come up so far :

Dll main- The entry point of my dll

// dllmain.cpp : Defines the entry point for the DLL application.

#include "stdafx.h"
#include "CBTHook.h"
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        CBT::CBTHook::SetHandle(hModule);
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

DLL Header file

//Dll header file - function signatures

#ifdef CBTHOOKDLL_EXPORTS
#define CBTHOOKDLL_API __declspec(dllexport) 
#else
#define CBTHOOKDLL_API __declspec(dllimport) 
#endif

namespace CBT
{
    class CBTHook
    {
    public:
        CBTHook();
        static void SetHandle(HINSTANCE handle);
        void InstallHook();
        void UnistallHook();
        LRESULT CALLBACK HookProcedure(int nCode, WPARAM wparam, LPARAM lparam);
        ~CBTHook();

    private:

        static HINSTANCE currentProcessHandle;
        HOOKPROC hkprcSysMsg;
        static HHOOK hookID;
    };

}

CBTHook.cpp

// CBTHook.cpp : Defines the exported functions for the DLL application.
//
#pragma once
#include "stdafx.h"
#include "CBTHook.h"
#include <Windows.h>
#include <iostream>

using namespace std;

namespace CBT
{
    CBTHook::CBTHook()
    {

    }

    void CBTHook::SetHandle(HINSTANCE handle)
    {
        currentProcessHandle = handle;
    }

    void CBTHook::InstallHook()
    {
        hookID = SetWindowsHookEx(WH_CBT,HookProcedure, currentProcessHandle, 0);
    }

    void CBTHook::UnistallHook()
    {
        UnhookWindowsHookEx(hookID);
    }

    LRESULT CALLBACK CBTHook::HookProcedure(int nCode, WPARAM wparam, LPARAM lparam)
    {
        if (nCode >= 0)
        {
            switch (nCode)
            {
            case HCBT_CREATEWND:
                cout << "Created!~" << endl;
                break;
            case HCBT_DESTROYWND:
                cout << "Destroied!~" << endl;
                break;
            default:
                cout << "sth else" << endl;
                break;
            }
        }
        else
            return CallNextHookEx(hookID, nCode, wparam, lparam);
    }
}

now the problem is that, SetWindowsHookEx wont accept the HookProcedure while as far as i have read and seen on the net the return value of the function in question is correct.
I get the error :

error C3867: 'CBT::CBTHook::HookProcedure': function call missing argument list; use '&CBT::CBTHook::HookProcedure' to create a pointer to member

doing as suggested by above doesnt solve the problem either!

What am i missing here?


回答1:


Your hook procedure must be a free function or a static class method. If you want to call a class instance method, you need to wrap that call in one of the above.

Edit:

To set a hook you need no classes. This is a basic example. Every other problem stems from your use of a class. If you want to use a class, make sure you know how to do it. If you are unsure, C++ is not Java. You don't need to use a class if it works just perfectly without.

Example:

#include "stdafx.h"

HHOOK hHook;

BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
    switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
        hHook = SetWindowsHookEx(WH_CBT, HookProcedure, hModule, 0);
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

LRESULT CALLBACK HookProcedure(int nCode, WPARAM wparam, LPARAM lparam)
{
    if (nCode >= 0)
    {
        switch (nCode)
        {
        case HCBT_CREATEWND:
            cout << "Created!~" << endl;
            break;
        case HCBT_DESTROYWND:
            cout << "Destroied!~" << endl;
            break;
        default:
            cout << "sth else" << endl;
            break;
        }
    }
    else
        return CallNextHookEx(hHook, nCode, wparam, lparam);
}


来源:https://stackoverflow.com/questions/20565826/why-doesnt-setwindowshookex-accept-the-hook-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!