Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <windowsx.h>
- class outbuf : public std::streambuf {
- public:
- outbuf() {
- setp(0, 0);
- }
- virtual int_type overflow(int_type c = traits_type::eof()) {
- return fputc(c, stdout) == EOF ? traits_type::eof() : c;
- }
- };
- #include <strsafe.h>
- void ErrorExit(LPTSTR lpszFunction)
- {
- // Retrieve the system error message for the last-error code
- LPVOID lpMsgBuf;
- LPVOID lpDisplayBuf;
- DWORD dw = GetLastError();
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dw,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf,
- 0, NULL );
- // Display the error message and exit the process
- lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
- (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
- StringCchPrintf((LPTSTR)lpDisplayBuf,
- LocalSize(lpDisplayBuf) / sizeof(TCHAR),
- TEXT("%s failed with error %d: %s"),
- lpszFunction, dw, lpMsgBuf);
- MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
- LocalFree(lpMsgBuf);
- LocalFree(lpDisplayBuf);
- ExitProcess(dw);
- }
- ///////////////////////////////////////////////////////////////////////////////////
- DWORD threadid = 0;
- HHOOK msghook = 0;
- LRESULT CALLBACK MsgCallback(int code, WPARAM w, LPARAM l){
- PCWPRETSTRUCT p = (PCWPRETSTRUCT)l;
- std::cout << p->message << " -- " << WM_PAINT << '\n';
- switch (p->message){
- case WM_PAINT:
- std::cout << "yasss!" << '\n';
- break;
- }
- return CallNextHookEx(msghook, code, w, l);
- }
- #ifdef WIN32
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
- #else
- int main() {
- #endif
- if(AllocConsole()) {
- freopen("CONOUT$", "w", stdout);
- SetConsoleTitle("Debug Console");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
- }
- outbuf ob;
- std::streambuf *sb = std::cout.rdbuf(&ob);
- HWND hwnd;
- WNDCLASSEX wc;
- ZeroMemory(&wc, sizeof(WNDCLASSEX));
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WinProc;
- wc.hInstance = hInstance;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
- wc.lpszClassName = "WindowClass";
- RegisterClassEx(&wc);
- hwnd = CreateWindowEx(
- NULL,
- wc.lpszClassName, // name of the window class
- "Hooking input test", // title of the window
- WS_OVERLAPPEDWINDOW, // window style
- 300, // x-position of the window
- 300, // y-position of the window
- 500, // width of the window
- 400, // height of the window
- NULL, // we have no parent window, NULL
- NULL, // we aren't using menus, NULL
- hInstance, // application handle
- NULL
- ); // used with multiple windows, NULL
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- /// hooking
- threadid = GetCurrentThreadId();
- msghook = SetWindowsHookEx(WH_CALLWNDPROCRET, &MsgCallback, hInstance, threadid);
- if (msghook == NULL){
- ErrorExit(TEXT("mouse SetWindowsHookEx"));
- return 0;
- }
- MSG msg;
- BOOL bRet;
- while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
- {
- if (bRet == -1){
- UnhookWindowsHookEx(msghook);
- std::cout.rdbuf(sb);
- }else{
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- UnhookWindowsHookEx(msghook);
- std::cout.rdbuf(sb);
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment