tags:

views:

172

answers:

1

I want to build a software test automation software and I'm playing around with Windows Hooks for that.

So I built the following C code. Can anyone tell me how to correct it ?

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

When I compile this I get the compiler errors:

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
+2  A: 

There's no need to declare a separate hHookProc variable - just pass your procedure to SetWindowsHookEx directly:

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

You're also going to need a valid module handle:

HINSTANCE hMod = GetModuleHandle(NULL);

Having made those edits, and made your JournalRecordProc return a value, it all now compiles and works for me (in that SetWindowsHookEx succeeds, anyway).

RichieHindle
Thanks a lot ! I did not know that I can put the name of the function (JournalRecordProc - for the function handle) directly into the functioncall of SetWindowsHookEx() :-)
FunnyBoy