PDA

Click to See Complete Forum and Search --> : Fhooking hooks!


o0Jon0o
Feb 14th, 2002, 02:09 PM
Hi all,
I'm just trying to make a program that hooks all keystrokes and displays them in a console window. I only
learnt how to use DLLs today so I not very good yet.

Hears my code, I didn't expect it to work and it didn't, any ideas why?:

The DLL:

#include <iostream.h>
#include "stdafx.h"

LRESULT CALLBACK FilterFunc(int nCode, WPARAM wparam,LPARAM lParam)
{
MessageBox (NULL, "IT WORKS!" , "WORKS!", 0 + MB_ICONQUESTION);
return CallNextHookEx(NULL, nCode, wparam , lParam);
}

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

The APP:

//#pragma hdrstop
//#include <condefs.h>
#include <iostream.h>
#include <conio.h>


//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
HINSTANCE dllres;
HOOKPROC adss;
HHOOK setres;
cout << GetLastError() << endl;
dllres = LoadLibrary("hook.dll");
cout << GetLastError();
if(dllres != NULL)
{
adss = (HOOKPROC)GetProcAddress(dllres, "FilterFunc");
setres = SetWindowsHookEx(WH_KEYBOARD, adss, dllres, 0);
if(setres == NULL )
cout << "Yeah, it is shagged :(";
}
else
cout << "Error loading hook.dll";

getch();
return 0;
}

P.S I dose compile :D


Thanks in advance

-Jon

CornedBee
Feb 14th, 2002, 02:57 PM
Check if GetProcAddress returns a valid address. If not, you might need to add a .def file to the dll (search the forum)

o0Jon0o
Feb 14th, 2002, 04:56 PM
Wow, thanks mate
I added a .def file and set that up for my callback function and everything worked perfectly!

I don't surpose anyone knows a good way of relaying the hook codes back to my app do you? :rolleyes:

Thanks again

jOn

CornedBee
Feb 15th, 2002, 11:43 AM
You could create an InitDll function where you give the dll a hwnd where it should post notification messages. But I don't know if the global hook is executed in the processes address space, so you must watch out where you store the hwnd.