TheNosiriN

Untitled

Dec 12th, 2021
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <windows.h>
  5. #include <windowsx.h>
  6.  
  7.  
  8.  
  9.  
  10. class outbuf : public std::streambuf {
  11. public:
  12.     outbuf() {
  13.         setp(0, 0);
  14.     }
  15.  
  16.     virtual int_type overflow(int_type c = traits_type::eof()) {
  17.         return fputc(c, stdout) == EOF ? traits_type::eof() : c;
  18.     }
  19. };
  20.  
  21.  
  22.  
  23.  
  24. #include <strsafe.h>
  25. void ErrorExit(LPTSTR lpszFunction)
  26. {
  27.     // Retrieve the system error message for the last-error code
  28.     LPVOID lpMsgBuf;
  29.     LPVOID lpDisplayBuf;
  30.     DWORD dw = GetLastError();
  31.  
  32.     FormatMessage(
  33.         FORMAT_MESSAGE_ALLOCATE_BUFFER |
  34.         FORMAT_MESSAGE_FROM_SYSTEM |
  35.         FORMAT_MESSAGE_IGNORE_INSERTS,
  36.         NULL,
  37.         dw,
  38.         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  39.         (LPTSTR) &lpMsgBuf,
  40.         0, NULL );
  41.  
  42.     // Display the error message and exit the process
  43.  
  44.     lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
  45.         (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
  46.     StringCchPrintf((LPTSTR)lpDisplayBuf,
  47.         LocalSize(lpDisplayBuf) / sizeof(TCHAR),
  48.         TEXT("%s failed with error %d: %s"),
  49.         lpszFunction, dw, lpMsgBuf);
  50.     MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
  51.  
  52.     LocalFree(lpMsgBuf);
  53.     LocalFree(lpDisplayBuf);
  54.     ExitProcess(dw);
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. ///////////////////////////////////////////////////////////////////////////////////
  63.  
  64. DWORD threadid = 0;
  65. HHOOK msghook = 0;
  66.  
  67.  
  68. LRESULT CALLBACK MsgCallback(int code, WPARAM w, LPARAM l){
  69.  
  70.     PCWPRETSTRUCT p = (PCWPRETSTRUCT)l;
  71.     std::cout << p->message << " -- " << WM_PAINT << '\n';
  72.  
  73.     switch (p->message){
  74.         case WM_PAINT:
  75.             std::cout << "yasss!" << '\n';
  76.         break;
  77.     }
  78.  
  79.  
  80.     return CallNextHookEx(msghook, code, w, l);
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. #ifdef WIN32
  89. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
  90. #else
  91. int main() {
  92. #endif
  93.  
  94.     if(AllocConsole()) {
  95.         freopen("CONOUT$", "w", stdout);
  96.         SetConsoleTitle("Debug Console");
  97.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  98.     }
  99.  
  100.  
  101.     outbuf ob;
  102.     std::streambuf *sb = std::cout.rdbuf(&ob);
  103.  
  104.  
  105.  
  106.  
  107.     HWND hwnd;
  108.     WNDCLASSEX wc;
  109.  
  110.    
  111.     ZeroMemory(&wc, sizeof(WNDCLASSEX));
  112.  
  113.     wc.cbSize = sizeof(WNDCLASSEX);
  114.     wc.style = CS_HREDRAW | CS_VREDRAW;
  115.     wc.lpfnWndProc = WinProc;
  116.     wc.hInstance = hInstance;
  117.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  118.     wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  119.     wc.lpszClassName = "WindowClass";
  120.     RegisterClassEx(&wc);
  121.  
  122.     hwnd = CreateWindowEx(
  123.         NULL,
  124.         wc.lpszClassName,    // name of the window class
  125.         "Hooking input test",    // title of the window
  126.         WS_OVERLAPPEDWINDOW,    // window style
  127.         300,    // x-position of the window
  128.         300,    // y-position of the window
  129.         500,    // width of the window
  130.         400,    // height of the window
  131.         NULL,    // we have no parent window, NULL
  132.         NULL,    // we aren't using menus, NULL
  133.         hInstance,    // application handle
  134.         NULL
  135.     );    // used with multiple windows, NULL
  136.  
  137.     ShowWindow(hwnd, nCmdShow);
  138.     UpdateWindow(hwnd);
  139.  
  140.  
  141.  
  142.     /// hooking
  143.     threadid = GetCurrentThreadId();
  144.  
  145.     msghook = SetWindowsHookEx(WH_CALLWNDPROCRET, &MsgCallback, hInstance, threadid);
  146.     if (msghook == NULL){
  147.         ErrorExit(TEXT("mouse SetWindowsHookEx"));
  148.         return 0;
  149.     }
  150.  
  151.  
  152.  
  153.  
  154.  
  155.     MSG msg;
  156.     BOOL bRet;
  157.     while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
  158.     {
  159.         if (bRet == -1){
  160.             UnhookWindowsHookEx(msghook);
  161.             std::cout.rdbuf(sb);
  162.         }else{
  163.             TranslateMessage(&msg);
  164.             DispatchMessage(&msg);
  165.         }
  166.     }
  167.  
  168.  
  169.     UnhookWindowsHookEx(msghook);
  170.     std::cout.rdbuf(sb);
  171.  
  172.     return msg.wParam;
  173. }
  174.  
  175.  
Advertisement
Add Comment
Please, Sign In to add comment