Advertisement
alien_fx_fiend

AdvTitleChangerv2 (GUI-Based /w Echo-piped Text)

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