Ok I thought the following would hook the keyboard input for only the one process but its getting it for all. Is there a way I can limit it to just one process?
File: dll.h
Code:
#ifndef _DLL_H_
#define _DLL_H_
#include <windows.h>
#define EXPORT __declspec (dllexport)
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
EXPORT LRESULT CALLBACK KbProc(int, WPARAM, LPARAM);
EXPORT BOOL WINAPI InstallHook(HWND);
EXPORT void WINAPI RemoveHook(void);
#endif /* _DLL_H_ */
File: dllmain.c
Code:
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#pragma data_seg("shared_data")
// Superglobals
HHOOK hookdata = 0;
#pragma data_seg()
#pragma comment(linker, "/SECTION:shared_data,S")
HMODULE hmodule = 0;
HWND hwindow = 0;
BOOL recycle = FALSE;
BOOL running = FALSE;
UINT timerid = 0;
void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);
EXPORT LRESULT CALLBACK KbProc(int code, WPARAM w, LPARAM l)
{
if ( code < 0 || code != HC_ACTION )
return CallNextHookEx(hookdata,code,w,l);
char buff[100];
sprintf(buff,"Values are: %d %u %u\n", code, w, l);
MessageBox(0, buff, "ok", 0);
switch(w)
{
default: return CallNextHookEx(hookdata,code,w,l);
}
return TRUE;
}
BOOL APIENTRY DllMain (HMODULE hmod,
DWORD reason,
LPVOID na)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
hmodule = hmod;
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}
EXPORT BOOL WINAPI InstallHook(HWND hwnd)
{
if ( hookdata != 0 )
return FALSE;
hwindow = hwnd;
hookdata = SetWindowsHookEx(WH_KEYBOARD, KbProc, hmodule, 0);
if ( !hookdata )
MessageBox(0, TEXT("Hook failed."), TEXT("Error"), MB_ICONEXCLAMATION);
return hookdata != 0;
}
EXPORT void WINAPI RemoveHook(void)
{
if ( hookdata != 0 )
{
UnhookWindowsHookEx(hookdata);
}
}
void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
}
File: main.c (its in one directory deeper then the other two)
Code:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "..\dll.h"
typedef BOOL (WINAPI *INST)(HWND);
typedef void (WINAPI *UNINST)(void);
int main(int argc, char *argv[])
{
HWND hwindow = FindWindow(NULL,"Untitled - Notepad");
if ( !hwindow )
{
MessageBox(0, TEXT("Could not locate notepad"), TEXT("Error"), MB_ICONEXCLAMATION);
system("pause");
return EXIT_FAILURE;
}
HMODULE hmod = LoadLibrary(TEXT("..\\Hooks.dll"));
if ( hmod == 0 )
{
MessageBox(0, TEXT("Could not load library"), TEXT("Error"), MB_ICONEXCLAMATION);
system("pause");
return EXIT_FAILURE;
}
INST InHook = (void*)GetProcAddress(hmod, TEXT("InstallHook"));
UNINST UnHook = (void*)GetProcAddress(hmod, TEXT("RemoveHook"));
if ( InHook && UnHook )
{
InHook(hwindow);
printf("Please press enter to terminate: ");
fflush(stdout);
getchar();
UnHook();
}
FreeLibrary(hmod);
system("pause");
return 0;
};
References: http://cboard.cprogramming.com/showthread.php?t=60642
http://cboard.cprogramming.com/showt...t=windows+hook
As I said it is getting info from all processes which is ok if I can filter them out and send them on their way.
Also is there a way to get the HWND using the process name and not the the window name?