为什么没有SetWindowsHookEx接受钩子程序? [英] Why doesnt SetWindowsHookEx accept the hook procedure?

查看:208
本文介绍了为什么没有SetWindowsHookEx接受钩子程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个dll,其中我可以使用来监视所有的系统事件(进程创建,销毁等)
这是我到目前为止:

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 - 我的dll的入口点

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头文件
$ b

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 p>

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);
    }
}

现在的问题是, SetWindowsHookEx 不接受 HookProcedure ,虽然我已经阅读和看到在网上的问题的函数的返回值是正确的。 br>
我得到错误:

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 :


错误C3867:'CBT :: CBTHook :: HookProcedure':function call missing argument列表;使用'& CBT :: CBTHook :: HookProcedure'创建指向成员的指针

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

要么!

我在这里失踪了什么?

What am i missing here?

推荐答案

您的hook过程必须是一个自由函数或静态类方法。

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.

编辑:

要设置一个钩子你不需要类。这是一个基本的例子。每一个其他问题都来自于你使用一个类。如果你想使用一个类,确保你知道如何做。如果你不确定,C ++不是Java。

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.

示例:

#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);
}

这篇关于为什么没有SetWindowsHookEx接受钩子程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆