Link to home
Start Free TrialLog in
Avatar of bolludobarbaro
bolludobarbaro

asked on

problems with hooks in windows 98

Hi, I've trying to make a keyboard hook work and I thought I had succeded (because it worked fine in Win 2000) but when I tested the program in Windows 98 I realized I hadn't.

The executable just calls the DLL with LoadLibrary and the DLL should do all the job. The problem in Windows 98 is that when i run the program and I press any key (so the callback function of the SetWindowsHookEx is run by Windows) i get a Windows serious message about a memory problem, and then I have to reboot the system because everything gets slow (even thought I have closed the program!!).
I did the same thing not using a DLL, just putting all the code in an executable and it works fine but it doesn't get all the keystrokes, that's why I need to do it with a DLL.
I hope you can help me, give a clue or something.
Here it is the skeleton. With only this, the program crashs when I touch a key:



#include <windows.h>
#include <fstream.h>
#include <string.h>
LRESULT CALLBACK callb(int nCode, WORD wParam, DWORD lParam );

//Variables
HHOOK hook;
static HINSTANCE hDLLInstance;


int juega()
{

       hook = SetWindowsHookEx(WH_KEYBOARD,
                   (HOOKPROC)callb,
                   hDLLInstance,
                   0);
       if (hook == NULL) MessageBox(NULL,"Hook = NULL","Error",MB_OK);


return 0;
}

LRESULT CALLBACK callb(int nCode, WORD wParam, DWORD lParam )
{


     LRESULT NextHook = CallNextHookEx( hook, nCode, wParam, lParam );


Sleep(10);

return NextHook;


}



BOOL APIENTRY DllMain( HINSTANCE hInstance,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                          )
{



    switch (ul_reason_for_call)
     {
          case DLL_PROCESS_ATTACH:
        hDLLInstance = hInstance;
        juega();


          break;
          case DLL_THREAD_ATTACH:
          case DLL_THREAD_DETACH:
          case DLL_PROCESS_DETACH:
          break;
    }
return TRUE;
}

ASKER CERTIFIED SOLUTION
Avatar of PlanetCpp
PlanetCpp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bolludobarbaro
bolludobarbaro

ASKER

Thank you very much for your comments, PlanetCpp. I fixed the code with your suggestions, but it still doesn't work.
But may be the problem is in the first thing you say:
that i am calling the StartHook from the DLL, and you from your application. In the MSDN library they advise to do it in your way, but since I am a newbie in programming I don't know how to do it.
I tried it in this way but it didn't work:

In the DLL I added:

_declspec(dllexport)

just before LRESULT CALLBACK callb(int nCode, WPARAM wParam, LPARAM lParam )

In the excutable I wrote this:

HHOOK hook;
HOOKPROC getproc;
BOOL pio=0;

HINSTANCE hinstDLL = LoadLibrary((LPCTSTR) "c:\\mydll.dll");

getproc = (HOOKPROC)GetProcAddress(hinstDLL, "callb");
                   
       hook = SetWindowsHookEx(WH_KEYBOARD,
                   (HOOKPROC)callb,
                   hinstDLL,
                   0);
pio = FreeLibrary(hinstDLL);

But it will never work, because the GetProcAddress retrieves NULL.
Where is the mistake?
Thanks

oh ok, to export i would make a header for neatness sake, and include this header in your app, or write up top.. doesn't matter
what you can do is something like
#define EXPORT _declspec(dllexport)
EXPORT int myfunction(int,long);
EXPORT void function2();

like that, this will make the compiler create a lib file
along with the dll. now when i said i called it from outside i didnt mean the SetWindowsHookEx was outside, i've done that but more often i keep a function in the dll called StartHook() and one called EndHook()
those have the hook code and those are called from the exe.
but once you export a function you can import it into your exe and use it as normal.
so lets say you exported the int myfunction(int,long);
make a header in your exe apps project, in it you import
#define IMPORT _declspec(dllimport)
IMPORT int myfunction(int,long);
now include that into your exe and under it link to the lib
---in exe cpp
...
....
#include "import.h" //the one you made right above
#pragma comment(lib,"mydll.lib")
the mydll.lib is the lib file the dll project makes (look in debug folder, or release folder whichever is current setting)
take that copy and paste it into the applications folder (not the debug/release but where the cpp files are)
take the dll that was created and copy it into the debug/release folder of your app
now you can use myfunction as if it werent in the dll at all. do not loadlibrary or getprocaddress, just use it as normal.
so you can export the callback, or do what i usually do is export two function to start and stop the hook.
also make sure that each time you build the exe you have to copy and overwrite the old one that you linked to in the debug folder of the exe project. the lib file can stay the same as long as you didn't change any export code.
if you forget you'll be using the old dll thinking that the new code is still causing an error
Thank you!!