Advertisement
DJD320

Console To Notepad (using handles)

Oct 19th, 2022 (edited)
1,355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ (WinAPI) 3.55 KB | Software | 0 0
  1.     #pragma region Includes
  2.     #include <iostream> // output to console
  3.     #include <string>
  4.     #include <thread>
  5.     #include <windows.h>
  6.     #pragma endregion
  7.      
  8.     #pragma region pragma / defs
  9.     #pragma warning(disable: 4996)
  10.     #define __SLEEP_TIME_MS 1000
  11.     #pragma endregion
  12.      
  13.     #pragma region Functions
  14.     void PassToNotepad(const char* str, HWND notepad, ...) { // sends string to notepad
  15.         va_list ap;
  16.         HWND edit;
  17.         char buf[256];
  18.      
  19.         va_start(ap, str); // get the first argument
  20.         vsprintf(buf, str, ap); // format the string
  21.         va_end(ap); // clean up
  22.         strcat(buf, ""); // add null terminator
  23.      
  24.         edit = FindWindowEx(notepad, NULL, ("EDIT"), NULL); // get edit control handle
  25.         SendMessageA(edit, EM_REPLACESEL, 0, (LPARAM)buf); // send string to edit control
  26.     }
  27.      
  28.     void PassToNotepad(const char* str, HWND notepad, bool newline, ...) { // sends string to notepad
  29.         va_list ap;
  30.         HWND edit;
  31.         char buf[256];
  32.      
  33.         va_start(ap, str); // get the first argument
  34.         vsprintf(buf, str, ap); // format the string
  35.         va_end(ap); // clean up
  36.         strcat(buf, ""); // add null terminator
  37.      
  38.         edit = FindWindowEx(notepad, NULL, ("EDIT"), NULL); // get edit control handle
  39.         SendMessageA(edit, EM_REPLACESEL, 0, (LPARAM)buf); // send string to edit control
  40.         if (newline)
  41.             SendMessageA(edit, EM_REPLACESEL, 0, (LPARAM)"\r"); // send string to edit control
  42.      
  43.     }
  44.      
  45.     bool isNotepadReady(HWND* notepad) { // checks if notepad is ready
  46.         HWND res;
  47.         res = FindWindow(NULL, ("Untitled - Notepad")); // get notepad handle
  48.         if (!res)
  49.             res = FindWindow(NULL, ("*Untitled - Notepad")); // get notepad handle
  50.      
  51.         *notepad = res; // set notepad handle
  52.         return res != NULL; // return true if notepad is ready
  53.     }
  54.      
  55.     void WriteOnCurrentLine(int X = 1, int Y = 1) {
  56.         CONSOLE_SCREEN_BUFFER_INFO csbi;
  57.         GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); // get console info
  58.         for (int i = 0; i < X; i++)
  59.             csbi.dwCursorPosition.X++;
  60.         for (int i = 0; i < Y; i++)
  61.             csbi.dwCursorPosition.Y--;
  62.         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), csbi.dwCursorPosition); // set cursor position
  63.     }
  64.      
  65.     #pragma endregion
  66.      
  67.     HWND global;
  68.      
  69.     int main() {
  70.        
  71.         while (true) {
  72.             if (!isNotepadReady(&global)) { // if notepad is not ready
  73.                 std::cout << "Notepad is not ready... Waiting..." << std::endl;
  74.                 std::this_thread::sleep_for(std::chrono::milliseconds(__SLEEP_TIME_MS)); // wait
  75.             }
  76.             else { // if notepad is ready
  77.                 std::string user_input;
  78.                 std::cout << ">";
  79.                 std::getline(std::cin, user_input); // get user input
  80.                 if (user_input.empty()) { // if user input is empty (means its a new line (user pressed enter))
  81.                     PassToNotepad("", global, true);
  82.                     WriteOnCurrentLine(); // write on current line
  83.                     std::cout << " (Sent newline to notepad)" << std::endl;
  84.                 }
  85.                 else {
  86.                     PassToNotepad(user_input.c_str(), global);
  87.                     WriteOnCurrentLine(user_input.length() + 1); // write on current line but add the length of the user input + 1 (for the ">")
  88.                     std::cout << " - (Sent to notepad)" << std::endl;
  89.                 }
  90.             }
  91.         }
  92.        
  93.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement