Advertisement
Axeer

Hooks

Nov 28th, 2022
2,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "stdio.h"
  4. #include "Windows.h"
  5. #include "psapi.h"
  6. #include "winuser.h"
  7. #include "strsafe.h"
  8.  
  9. #define STRX 2
  10. #define STRY 10
  11. #define LINEHEIGHT 20
  12.  
  13. static const HWND gh_hwndMain;
  14. static const HOOK h;
  15.  
  16. #define DRAW(__x,__line_index,__format,...)\
  17.     CHAR __szBuf[512];\
  18.     StringCchPrintfA(__szBuf, 512,__format, __VA_ARGS__);\
  19.     draw(__szBuf, STRX, STRY + (LINEHEIGHT * __line_index));
  20.  
  21. typedef struct Hook
  22. {
  23.     int nType;
  24.     HOOKPROC hkprc;
  25.     HHOOK hhook;
  26. } HOOK;
  27.  
  28.  
  29.  
  30. void WINAPI draw(CHAR* string, int x, int y);
  31. LRESULT WINAPI MouseCallback(int nCode, WPARAM wParam, LPARAM lParam);
  32. LRESULT WINAPI KeyboardCallback(int nCode, WPARAM wParam, LPARAM lParam);
  33. void WINAPI sethooks();
  34. LPSTR* GetDrives();
  35. LPCSTR DefineDrive(LPCSTR driveName);
  36.  
  37. void WINAPI draw(CHAR* string, int x, int y) {
  38.     HDC hdc = GetDC(gh_hwndMain);
  39.     size_t cch;
  40.  
  41.     HRESULT hResult = StringCchLengthA(string, 255 / sizeof(CHAR), &cch);
  42.     if (FAILED(hResult))
  43.     {
  44.  
  45.     }
  46.     TextOutA(hdc, x, y, LPCSTR(string), cch);
  47.     ReleaseDC(gh_hwndMain, hdc);
  48.  
  49. }
  50.  
  51. LRESULT WINAPI MouseCallback(int nCode, WPARAM wParam, LPARAM lParam) {
  52.     MSLLHOOKSTRUCT* pMouseStruct = (MSLLHOOKSTRUCT*)lParam; // WH_MOUSE_LL struct
  53.  
  54.     if (nCode == 0) {
  55.         if (pMouseStruct != NULL) {
  56.             printf_s("Mouse Coordinates: x = %i | y = %i \n", pMouseStruct->pt.x, pMouseStruct->pt.y);
  57.         }
  58.  
  59.         CHAR szBuf[255];
  60.         CHAR szMsg[255] = "mouse click";
  61.         HDC hdc = GetDC(gh_hwndMain);
  62.  
  63.         HRESULT hResult = StringCchPrintfA(szBuf, 255,
  64.             "MOUSE - nCode: %d, msg: %s, x: %d, y: %d   ",
  65.             nCode, szMsg, pMouseStruct->pt.x, pMouseStruct->pt.y);
  66.  
  67.         size_t cch;
  68.  
  69.         DRAW(2, 0, "MOUSE - nCode: %d, msg: %s, x: %d, y: %d   ",
  70.             nCode, szMsg, pMouseStruct->pt.x, pMouseStruct->pt.y)
  71.             //draw(szBuf, 2, strPos);
  72.  
  73.             switch (wParam) {
  74.  
  75.             case WM_LBUTTONUP: {
  76.                 printf_s("LEFT CLICK UP\n");
  77.             }break;
  78.             case WM_LBUTTONDOWN: {
  79.                 printf_s("LEFT CLICK DOWN\n");
  80.             }break;
  81.  
  82.             }
  83.  
  84.     }
  85.  
  86.     return CallNextHookEx(h.hhook, nCode, wParam, lParam);
  87. }
  88.  
  89. LRESULT WINAPI KeyboardCallback(int nCode, WPARAM wParam, LPARAM lParam) {
  90.     if (nCode >= 0)
  91.     {
  92.         if (wParam == WM_KEYDOWN)
  93.         {
  94.             KBDLLHOOKSTRUCT kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
  95.             switch (kbdStruct.vkCode)
  96.             {
  97.             default:
  98.  
  99.                 DRAW(2, 1, "KEYBOARD - nCode: %d, keycode: %d, scancode: %d, time in minutes since system start: %d", nCode, kbdStruct.vkCode, kbdStruct.scanCode, (kbdStruct.time / 1000) / 60)
  100.                     break;
  101.             }
  102.         }
  103.     }
  104.  
  105.     return CallNextHookEx(h.hhook, nCode, wParam, lParam);
  106. }
  107.  
  108. static HOOK mousehook = { WH_MOUSE_LL, MouseCallback };
  109. static HOOK keyboardhook = { WH_KEYBOARD_LL, KeyboardCallback };
  110.  
  111. void WINAPI sethooks() {
  112.     mousehook.hhook = SetWindowsHookExW(mousehook.nType, mousehook.hkprc, NULL, 0);
  113.     keyboardhook.hhook = SetWindowsHookExW(keyboardhook.nType, keyboardhook.hkprc, NULL, 0);
  114. }
  115.  
  116. void slice(const char* str, char* result, size_t start, size_t end)
  117. {
  118.     strncpy(result, str + start, end - start);
  119. }
  120.  
  121. LPSTR* GetDrives() {
  122.     CHAR drives[1024];
  123.     DWORD len = GetLogicalDriveStringsA(1024, drives);
  124.  
  125.     LPSTR drives_list[128];
  126.     int drives_list_index = 0;
  127.  
  128.     for (int drive_sym = 0, last = 0; drive_sym < len; drive_sym++) {
  129.         if (drives[drive_sym] == NULL) {
  130.             slice(drives, drives_list[drives_list_index++], last, drive_sym);
  131.             last = drive_sym + 1;
  132.         }
  133.     }
  134.    
  135.     return drives_list;
  136. }
  137.  
  138. LPCSTR DefineDrive(LPCSTR driveName) {
  139.     DWORD types[8] = {
  140.         DRIVE_UNKNOWN,
  141.         DRIVE_NO_ROOT_DIR,
  142.         DRIVE_REMOVABLE,
  143.         DRIVE_FIXED,
  144.         DRIVE_REMOTE,
  145.         DRIVE_CDROM,
  146.         DRIVE_RAMDISK,
  147.         NULL
  148.     };
  149.     const char* sTypes[8] = {
  150.         "DRIVE_UNKNOWN",
  151.         "DRIVE_NO_ROOT_DIR",
  152.         "DRIVE_REMOVABLE",
  153.         "DRIVE_FIXED",
  154.         "DRIVE_REMOTE",
  155.         "DRIVE_CDROM",
  156.         "DRIVE_RAMDISK",
  157.         NULL
  158.     };
  159.     return (LPCSTR)sTypes[GetDriveTypeA(driveName)];
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement