2009-08-23 12 views
0

Je veux construire un logiciel d'automatisation de test logiciel et je joue avec Windows Hooks pour cela.Besoin d'aide avec Windows Journal Record Hook

J'ai donc construit le code C suivant. Quelqu'un peut-il me dire comment le corriger?

#include "windows.h" 

// the call back function 
LRESULT CALLBACK JournalRecordProc(int code, WPARAM wParam, LPARAM lParam) 
{ 

    HHOOK hhk = 0; 

    if (code > 0) 
    { 
     // save Data in File 
    } 

    if (code < 0) 
    { 
     // work done: now pass on to the next one that does hooking 
     CallNextHookEx(hhk, code, wParam, lParam); 
    } 

    /* 
    if (code ==) 
    { 
     // ESC button pressed -> finished recording 
     UnhookWindowsHookEx(hhk); 
    } 
    */ 

} 

int main() 

{ 
    int iRet = 0; 

    HHOOK hHook = 0; 

    HINSTANCE hMod = 0; 

    HOOKPROC (*hHookProc)(int, WPARAM, LPARAM); 

     hHookProc = &JournalRecordProc; 

    // type of hook, callback function handle, hinstance [dll ?], 0 for systemwide 
    hHook = SetWindowsHookEx(WH_JOURNALRECORD, hHookProc, hMod, 0); 

    return iRet; 
} 

Quand je compile ce que j'obtiens les erreurs du compilateur:

error C2440: '=': 'LRESULT (__stdcall 
*)(int,WPARAM,LPARAM)' kann nicht in 'HOOKPROC (__cdecl 
*)(int,WPARAM,LPARAM)' konvertiert werden (could not be converted) 

error C2440: 'Funktion': 'HOOKPROC (__cdecl *)(int,WPARAM,LPARAM)' kann nicht in 'HOOKPROC' konvertiert werden (could not be converted) 

warning C4024: 'SetWindowsHookExA': Unterschiedliche Typen für formalen und übergebenen Parameter 2 

Répondre

2

Il n'y a pas besoin de déclarer une variable hHookProc séparée - juste passer votre procédure SetWindowsHookEx directement:

hHook = SetWindowsHookEx(WH_JOURNALRECORD, JournalRecordProc, hMod, 0); 

Vous allons également avoir besoin d'un handle de module valide:

HINSTANCE hMod = GetModuleHandle(NULL); 

Après avoir fait ces modifications, et fait votre JournalRecordProc retourner une valeur, tout compile maintenant et fonctionne pour moi (en ce que SetWindowsHookEx réussit, de toute façon).

+0

Merci beaucoup! Je ne savais pas que je peux mettre le nom de la fonction (JournalRecordProc - pour la poignée de fonction) directement dans l'appel de fonction de SetWindowsHookEx() :-) –