C钩/回叫系统

时间:2012-03-03 16:44:02

标签: c pointers callback hook

我一直在试图弄清楚如何在C中实现一个钩子系统。任何人都可以给我一些指示或示例吗?

3 个答案:

答案 0 :(得分:3)

使用函数指针:

struct callbacks {
    void (*somethingHappened)(void *context);
    bool (*shouldIDoSomething)(void *context);
};

void doSomethingAwesome(void *operationData, struct callbacks callbacks)
{
     // context is the data you pass to the function
     if (callbacks.shouldIDoSomething(context))
     {
          // do something
          callbacks.somethingHappened(context);
     }
}

或者,您可以创建一个调度服务:

struct dictionary
{
    char **keys;
    void **values;        

    int count;
};

struct list
{
    void **values;

    int count;
};

// functions to add, remove, etc;

struct dictionary *callbacks = NULL;
void registerForDispatch(char *key, void (*callback)(void *))
{
    if (!callbacks)
    {
        callbacks = dictionary_create(10); // dictionary with 10 key / value pairs
    }

    if (!dictionary_containsKey(callbacks, key))
    {
        struct list *callBacksForKey = list_create(10); // default 10 callbacks, array should be auto-expanding

        dictionary_setValue(callbacks, key, arr);
    }

    struct list *callBacksForKey = dictionary_getValue(callbacks, key);

    list_addObject(callBacksForKey, callback);
}

void unregisterFromDispatch(char *key, void (*callback)(void *))
{
    struct list *callBacksForKey = dictionary_getValue(callbacks, key);

    list_removeObject(callback);
}

void sendNotification(char *key, void *context)
{
    struct list *callBacksForKey = dictionary_getValue(callbacks, key);

    for (int i = 0; list->count; i++)
    {
         void (*callback)(void *) = list->values[i];
         callback(context);
    }
}

使用第二种解决方案,您可以在一次回调中使用多个侦听器。

答案 1 :(得分:0)

你可以看到qsort示例: http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/

/* qsort example */
#include <stdio.h>
#include <stdlib.h>

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main ()
{
  int n;
  qsort (values, 6, sizeof(int), compare);
  for (n=0; n<6; n++)
     printf ("%d ",values[n]);
  return 0;
}

qsort需要一个函数指针,它是排序的回调。

答案 2 :(得分:0)

我不确定你到底在寻找什么,但我一直在使用QT / C ++中的钩子开发应用程序,所以我可以给你一些例子。

首先,声明函数原型,像这样

LRESULT CALLBACK    LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam);

然后,您需要在main函数中设置钩子,如下所示:

HHOOK mouseHook;
HINSTANCE appInstance = GetModuleHandle(NULL);
mouseHook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, appInstance, 0); //set global mouse hook

然后你需要定义回调函数,我可以给你我的代码&gt;

//Mouse hook callback function.

LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam){


    if (nCode == HC_ACTION) {

    switch(wParam){
    case WM_LBUTTONUP:
    case WM_RBUTTONUP:
    case WM_MOUSEWHEEL:
#ifdef DEBUG
        w->printText("Scrolling or click");
#endif
        w->save_key("MOUSE");
        i++;
        break;

    default: break;
    }
     }
    return CallNextHookEx(mouseHook, nCode, wParam, lParam);

}