Advertisement
alien_fx_fiend

AdvTitleChanger [*FINAL*] (GUI-Based (All fixed Copy/Paste from RichTextBox to Text Box))

Jul 13th, 2024 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.99 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <CommCtrl.h>
  3. #include <string>
  4. #include <TlHelp32.h>
  5. #include <tchar.h>
  6. #include <Psapi.h>
  7. #include <iostream>
  8. #include <vector>
  9. #include <sstream>
  10. #include <thread>
  11. #include <WinUser.h>
  12. #include <richedit.h>
  13.  
  14. #pragma comment(lib, "Psapi.lib")
  15. #pragma comment(lib, "Comctl32.lib")
  16.  
  17. bool isCopying = false;
  18. bool isPasting = false;
  19. void DeleteWordToLeft(HWND hEdit);
  20.  
  21. struct ProcessInfo {
  22.     std::wstring processName;
  23.     HWND hwnd;
  24. };
  25.  
  26. std::vector<ProcessInfo> targetProcesses;
  27. std::vector<std::wstring> processNames;
  28. bool running = true;
  29.  
  30. HWND hRichTextBox, hTextBox, hButton, hCloseButton, hGroupBox, hLabel;
  31.  
  32. void DeleteWordToLeft(HWND hEdit);
  33.  
  34. void AppendTextToRichEdit(HWND hRichText, const std::wstring& text) {
  35.     int len = GetWindowTextLength(hRichText);
  36.     SendMessage(hRichText, EM_SETSEL, (WPARAM)len, (LPARAM)len);
  37.     SendMessage(hRichText, EM_REPLACESEL, 0, (LPARAM)text.c_str());
  38. }
  39.  
  40. BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege) {
  41.     TOKEN_PRIVILEGES tp;
  42.     LUID luid;
  43.  
  44.     if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid)) {
  45.         AppendTextToRichEdit(hRichTextBox, L"LookupPrivilegeValue error: " + std::to_wstring(GetLastError()) + L"\n");
  46.         return FALSE;
  47.     }
  48.  
  49.     tp.PrivilegeCount = 1;
  50.     tp.Privileges[0].Luid = luid;
  51.     tp.Privileges[0].Attributes = (bEnablePrivilege) ? SE_PRIVILEGE_ENABLED : 0;
  52.  
  53.     if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
  54.         AppendTextToRichEdit(hRichTextBox, L"AdjustTokenPrivileges error: " + std::to_wstring(GetLastError()) + L"\n");
  55.         return FALSE;
  56.     }
  57.  
  58.     if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
  59.         AppendTextToRichEdit(hRichTextBox, L"The token does not have the specified privilege.\n");
  60.         return FALSE;
  61.     }
  62.  
  63.     return TRUE;
  64. }
  65.  
  66. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
  67.     DWORD processId;
  68.     GetWindowThreadProcessId(hwnd, &processId);
  69.  
  70.     HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
  71.     if (hProcess != NULL) {
  72.         TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
  73.         if (GetModuleFileNameEx(hProcess, NULL, szProcessName, MAX_PATH)) {
  74.             std::wstring processName(szProcessName);
  75.             size_t pos = processName.find_last_of(L"\\");
  76.             if (pos != std::wstring::npos) {
  77.                 processName = processName.substr(pos + 1);
  78.             }
  79.             CloseHandle(hProcess);
  80.  
  81.             TCHAR windowTitle[MAX_PATH];
  82.             GetWindowText(hwnd, windowTitle, MAX_PATH);
  83.  
  84.             for (const auto& targetName : *(std::vector<std::wstring>*)lParam) {
  85.                 if (processName == targetName) {
  86.                     targetProcesses.push_back({ processName, hwnd });
  87.                 }
  88.             }
  89.         }
  90.         else {
  91.             AppendTextToRichEdit(hRichTextBox, L"GetModuleFileNameEx error: " + std::to_wstring(GetLastError()) + L"\n");
  92.         }
  93.     }
  94.     return TRUE;
  95. }
  96.  
  97. bool ModifyWindowTitles(const std::wstring& newTitle) {
  98.     bool anyModified = false;
  99.     for (const auto& process : targetProcesses) {
  100.         if (SetWindowText(process.hwnd, newTitle.c_str())) {
  101.             anyModified = true;
  102.             AppendTextToRichEdit(hRichTextBox, L"\nModified window title of " + process.processName + L"\n");
  103.         }
  104.         else {
  105.             AppendTextToRichEdit(hRichTextBox, L"\nFailed to modify window title of " + process.processName + L"\n");
  106.         }
  107.     }
  108.     return anyModified;
  109. }
  110.  
  111. DWORD WINAPI TitleModifierThread(LPVOID lpParam) {
  112.     std::wstring newTitle = *(std::wstring*)lpParam;
  113.     while (running) {
  114.         ModifyWindowTitles(newTitle);
  115.         Sleep(1000);
  116.     }
  117.     return 0;
  118. }
  119.  
  120. std::vector<std::wstring> Split(const std::wstring& s, wchar_t delimiter) {
  121.     std::vector<std::wstring> tokens;
  122.     std::wstring token;
  123.     std::wistringstream tokenStream(s);
  124.     while (std::getline(tokenStream, token, delimiter)) {
  125.         tokens.push_back(token);
  126.     }
  127.     return tokens;
  128. }
  129.  
  130. void ProcessInput(HWND hwnd) {
  131.     static int step = 0;
  132.     static std::wstring newTitle;
  133.     static std::wstring persistence;
  134.  
  135.     wchar_t buffer[256];
  136.     GetWindowText(hTextBox, buffer, 256);
  137.     std::wstring input(buffer);
  138.  
  139.     // Echo the input to the RichTextBox
  140.     AppendTextToRichEdit(hRichTextBox, L"> " + input + L"\n");
  141.  
  142.     switch (step) {
  143.     case 0:
  144.         processNames = Split(input, L';');
  145.         AppendTextToRichEdit(hRichTextBox, L"\nEnter the new window title text: \n");
  146.         step++;
  147.         break;
  148.  
  149.     case 1:
  150.         newTitle = input;
  151.         AppendTextToRichEdit(hRichTextBox, L"\n\nDo you want persistence (Y/N)? \n");
  152.         step++;
  153.         break;
  154.  
  155.     case 2:
  156.         persistence = input;
  157.         HANDLE hToken;
  158.         if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
  159.             AppendTextToRichEdit(hRichTextBox, L"OpenProcessToken error: " + std::to_wstring(GetLastError()) + L"\n");
  160.             return;
  161.         }
  162.  
  163.         if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
  164.             AppendTextToRichEdit(hRichTextBox, L"Failed to enable debug privilege.\n");
  165.             CloseHandle(hToken);
  166.             return;
  167.         }
  168.  
  169.         EnumWindows(EnumWindowsProc, (LPARAM)&processNames);
  170.  
  171.         if (targetProcesses.empty()) {
  172.             AppendTextToRichEdit(hRichTextBox, L"No windows found for the specified processes.\n");
  173.         }
  174.         else {
  175.             if (persistence == L"Y" || persistence == L"y") {
  176.                 std::thread titleModifierThread(TitleModifierThread, &newTitle);
  177.                 titleModifierThread.detach();
  178.  
  179.                 AppendTextToRichEdit(hRichTextBox, L"Window titles are being modified persistently. Press Enter to stop and exit...\n");
  180.             }
  181.             else {
  182.                 ModifyWindowTitles(newTitle);
  183.                 AppendTextToRichEdit(hRichTextBox, L"Window titles modified once. Press Enter to exit...\n");
  184.             }
  185.         }
  186.  
  187.         SetPrivilege(hToken, SE_DEBUG_NAME, FALSE);
  188.         CloseHandle(hToken);
  189.         step++;
  190.         break;
  191.  
  192.     case 3:
  193.         PostQuitMessage(0);
  194.         break;
  195.     }
  196.  
  197.     SetWindowText(hTextBox, L"");
  198. }
  199.  
  200.  
  201. LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  202.     switch (uMsg) {
  203.     case WM_CREATE:
  204.         hRichTextBox = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL,
  205.             WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_VSCROLL,
  206.             10, 10, 372, 200, hwnd, (HMENU)1, NULL, NULL); // Add WS_VSCROLL and WS_HSCROLL
  207.         hTextBox = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_MULTILINE,
  208.             10, 220, 280, 20, hwnd, (HMENU)2, NULL, NULL);
  209.         hButton = CreateWindowEx(0, L"BUTTON", L"Send", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  210.             300, 220, 80, 20, hwnd, (HMENU)3, NULL, NULL);
  211.         hCloseButton = CreateWindowEx(0, L"BUTTON", L"Close", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  212.             10, 250, 290, 20, hwnd, (HMENU)4, NULL, NULL);
  213.         hLabel = CreateWindowEx(0, L"STATIC", L"2024 (c) Entisoft Software. All Rights Reserved. Evans Thorpemorton.", WS_CHILD | WS_VISIBLE | SS_CENTER,
  214.             10, 280, 372, 20, hwnd, (HMENU)5, NULL, NULL);
  215.  
  216.         AppendTextToRichEdit(hRichTextBox, L"Enter the process names separated by semicolons (e.g., notepad.exe;calc.exe): \n");
  217.         SetTimer(hwnd, 1, 10, NULL);
  218.  
  219.         // Initialize the SCROLLINFO structure inside the case
  220.         SCROLLINFO si;
  221.         si.cbSize = sizeof(si);
  222.         si.fMask = SIF_ALL;
  223.         si.nMin = 0;
  224.         si.nMax = 100;
  225.         si.nPage = 10;
  226.         si.nPos = 0;
  227.  
  228.         SetScrollInfo(hRichTextBox, SB_VERT, &si, TRUE);
  229.         break;
  230.  
  231.     case WM_COMMAND:
  232.         if (LOWORD(wParam) == 3) {
  233.             ProcessInput(hwnd);
  234.         }
  235.         else if (LOWORD(wParam) == 4) {
  236.             PostQuitMessage(0);
  237.         }
  238.         break;
  239.  
  240.     case WM_TIMER:
  241.         if (wParam == 1) {
  242.             if (GetFocus() == hTextBox) {
  243.                 if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
  244.                     static DWORD lastEnterTime = 0;
  245.                     DWORD currentTime = GetTickCount();
  246.                     if (currentTime - lastEnterTime > 500) {
  247.                         ProcessInput(hwnd);
  248.                         lastEnterTime = currentTime;
  249.                     }
  250.                     return 0;
  251.                 }
  252.                 if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState('A') & 0x8000)) {
  253.                     SendMessage(hTextBox, EM_SETSEL, 0, -1);
  254.                     return 0;
  255.                 }
  256.                 if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState(VK_BACK) & 0x8000)) {
  257.                     static DWORD lastCtrlBackspaceTime = 0;
  258.                     DWORD currentTime = GetTickCount();
  259.                     if (currentTime - lastCtrlBackspaceTime > 100) {
  260.                         DeleteWordToLeft(hTextBox);
  261.                         lastCtrlBackspaceTime = currentTime;
  262.                     }
  263.                     return 0;
  264.                 }
  265.                 if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState('C') & 0x8000)) {
  266.                     static DWORD lastCtrlCTime = 0;
  267.                     DWORD currentTime = GetTickCount();
  268.                     if (currentTime - lastCtrlCTime > 500) {
  269.                         SendMessage(hTextBox, WM_COPY, 0, 0);
  270.                         lastCtrlCTime = currentTime;
  271.                     }
  272.                     return 0;
  273.                 }
  274.             }            
  275.         }
  276.         if (wParam == 2) {
  277.             KillTimer(hwnd, 2);
  278.             isPasting = false;
  279.         }
  280.         else if (wParam == 3) {
  281.             KillTimer(hwnd, 3);
  282.             isCopying = false;
  283.         }
  284.         break;
  285.  
  286.     case WM_KEYDOWN:
  287.         if (GetFocus() == hTextBox) {
  288.             if (wParam == 'V' && (GetKeyState(VK_CONTROL) & 0x8000)) {
  289.                 if (!isPasting) {
  290.                     isPasting = true;
  291.  
  292.                     // Store current text
  293.                     int textLength = GetWindowTextLength(hTextBox);
  294.                     std::wstring currentText(textLength + 1, L'\0');
  295.                     GetWindowText(hTextBox, &currentText[0], textLength + 1);
  296.  
  297.                     // Perform paste
  298.                     SendMessage(hTextBox, WM_PASTE, 0, 0);
  299.  
  300.                     // Get new text
  301.                     int newTextLength = GetWindowTextLength(hTextBox);
  302.                     std::wstring newText(newTextLength + 1, L'\0');
  303.                     GetWindowText(hTextBox, &newText[0], newTextLength + 1);
  304.  
  305.                     // If text didn't change, revert
  306.                     if (newText == currentText) {
  307.                         SetWindowText(hTextBox, currentText.c_str());
  308.                     }
  309.  
  310.                     // Set a timer to reset isPasting
  311.                     SetTimer(hwnd, 2, 100, NULL);
  312.                 }
  313.                 return 0;
  314.             }
  315.             else if (wParam == 'C' && (GetKeyState(VK_CONTROL) & 0x8000)) {
  316.                 if (!isCopying) {
  317.                     isCopying = true;
  318.                     SendMessage(hTextBox, WM_COPY, 0, 0);
  319.                     SetTimer(hwnd, 3, 100, NULL);
  320.                 }
  321.                 return 0;
  322.             }
  323.             break;
  324.         }
  325.  
  326.     case WM_KEYUP:
  327.         if (wParam == 'V' || wParam == 'C' || wParam == VK_CONTROL) {
  328.             isPasting = false;
  329.             isCopying = false;
  330.         }
  331.         break;
  332.  
  333.     case WM_DESTROY:
  334.         running = false;
  335.         KillTimer(hwnd, 1);
  336.         PostQuitMessage(0);
  337.         break;
  338.  
  339.     case WM_SIZE:
  340.         MoveWindow(hLabel, 10, HIWORD(lParam) - 20, 372, 20, TRUE);
  341.         break;
  342.  
  343.     default:
  344.         return DefWindowProc(hwnd, uMsg, wParam, lParam);
  345.     }
  346.  
  347.     return 0;
  348. }
  349.  
  350. void DeleteWordToLeft(HWND hWnd) {
  351.     DWORD start, end;
  352.     SendMessage(hWnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
  353.     if (start == 0) return;
  354.  
  355.     std::wstring text(GetWindowTextLength(hWnd), L'\0');
  356.     GetWindowText(hWnd, &text[0], text.size() + 1);
  357.     text.resize(text.size() - 1);
  358.  
  359.     size_t pos = start - 1;
  360.     while (pos > 0 && iswspace(text[pos])) {
  361.         --pos;
  362.     }
  363.     while (pos > 0 && !iswspace(text[pos])) {
  364.         --pos;
  365.     }
  366.  
  367.     text.erase(pos, start - pos);
  368.     SetWindowText(hWnd, text.c_str());
  369.     SendMessage(hWnd, EM_SETSEL, pos, pos);
  370. }
  371.  
  372. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  373.     WNDCLASS wc = {};
  374.     wc.lpfnWndProc = WndProc;
  375.     wc.hInstance = hInstance;
  376.     wc.lpszClassName = L"WindowTitleModifier";
  377.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  378.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  379.     RegisterClass(&wc);
  380.  
  381.     DWORD style = (WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX) & ~WS_THICKFRAME;
  382.  
  383.     int windowWidth = 400;
  384.     int windowHeight = 350;
  385.  
  386.     int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  387.     int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  388.  
  389.     int x = (screenWidth - windowWidth) / 2;
  390.     int y = (screenHeight - windowHeight) / 2;
  391.  
  392.     // Initialize common controls (IMPORTANT!)
  393.     INITCOMMONCONTROLSEX icex;
  394.     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  395.     icex.dwICC = ICC_USEREX_CLASSES; // Include Rich Edit control class
  396.     InitCommonControlsEx(&icex);
  397.  
  398.     // Load the Rich Edit library (for versions before Windows Vista)
  399.     HMODULE hRichEdit = LoadLibrary(L"riched20.dll");
  400.  
  401.  
  402.     HWND hwnd = CreateWindowEx(0, wc.lpszClassName, L"Window Title Modifier", style, x, y, windowWidth, windowHeight, NULL, NULL, hInstance, NULL);
  403.  
  404.     if (hwnd == NULL) {
  405.         return 0;
  406.     }
  407.  
  408.     ShowWindow(hwnd, nCmdShow);
  409.     UpdateWindow(hwnd);
  410.  
  411.     MSG msg = {};
  412.     while (GetMessage(&msg, NULL, 0, 0)) {
  413.         TranslateMessage(&msg);
  414.         DispatchMessage(&msg);
  415.     }
  416.  
  417.     return 0;
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement