Advertisement
alien_fx_fiend

AdvTitleChangerv3 (GUI-Based /w Scrollbar)

Jul 12th, 2024
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.57 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,
  203.             WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_VSCROLL,
  204.             10, 10, 372, 200, hwnd, (HMENU)1, NULL, NULL); // Add WS_VSCROLL and WS_HSCROLL
  205.         hTextBox = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_MULTILINE,
  206.             10, 220, 280, 20, hwnd, (HMENU)2, NULL, NULL);
  207.         hButton = CreateWindowEx(0, L"BUTTON", L"Send", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  208.             300, 220, 80, 20, hwnd, (HMENU)3, NULL, NULL);
  209.         hCloseButton = CreateWindowEx(0, L"BUTTON", L"Close", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  210.             10, 250, 290, 20, hwnd, (HMENU)4, NULL, NULL);
  211.         hLabel = CreateWindowEx(0, L"STATIC", L"2024 (c) Entisoft Software. All Rights Reserved. Evans Thorpemorton.", WS_CHILD | WS_VISIBLE | SS_CENTER,
  212.             10, 280, 372, 20, hwnd, (HMENU)5, NULL, NULL);
  213.  
  214.         AppendTextToRichEdit(hRichTextBox, L"Enter the process names separated by semicolons (e.g., notepad.exe;calc.exe): \n");
  215.         SetTimer(hwnd, 1, 10, NULL);
  216.  
  217.         // Initialize the SCROLLINFO structure inside the case
  218.         SCROLLINFO si;
  219.         si.cbSize = sizeof(si);
  220.         si.fMask = SIF_ALL;
  221.         si.nMin = 0;
  222.         si.nMax = 100;
  223.         si.nPage = 10;
  224.         si.nPos = 0;
  225.  
  226.         SetScrollInfo(hRichTextBox, SB_VERT, &si, TRUE);
  227.         break;
  228.  
  229.     case WM_COMMAND:
  230.         if (LOWORD(wParam) == 3) {
  231.             ProcessInput(hwnd);
  232.         }
  233.         else if (LOWORD(wParam) == 4) {
  234.             PostQuitMessage(0);
  235.         }
  236.         break;
  237.  
  238.     case WM_TIMER:
  239.         if (wParam == 1) {
  240.             if (GetFocus() == hTextBox) {
  241.                 if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
  242.                     static DWORD lastEnterTime = 0;
  243.                     DWORD currentTime = GetTickCount();
  244.                     if (currentTime - lastEnterTime > 500) {
  245.                         ProcessInput(hwnd);
  246.                         lastEnterTime = currentTime;
  247.                     }
  248.                     return 0;
  249.                 }
  250.                 if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState('A') & 0x8000)) {
  251.                     SendMessage(hTextBox, EM_SETSEL, 0, -1);
  252.                     return 0;
  253.                 }
  254.                 if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState(VK_BACK) & 0x8000)) {
  255.                     static DWORD lastCtrlBackspaceTime = 0;
  256.                     DWORD currentTime = GetTickCount();
  257.                     if (currentTime - lastCtrlBackspaceTime > 100) {
  258.                         DeleteWordToLeft(hTextBox);
  259.                         lastCtrlBackspaceTime = currentTime;
  260.                     }
  261.                     return 0;
  262.                 }
  263.             }
  264.         }
  265.         break;
  266.  
  267.     case WM_DESTROY:
  268.         running = false;
  269.         KillTimer(hwnd, 1);
  270.         PostQuitMessage(0);
  271.         break;
  272.  
  273.     case WM_SIZE:
  274.         MoveWindow(hLabel, 10, HIWORD(lParam) - 20, 372, 20, TRUE);
  275.         break;
  276.  
  277.     default:
  278.         return DefWindowProc(hwnd, uMsg, wParam, lParam);
  279.     }
  280.  
  281.     return 0;
  282. }
  283.  
  284. void DeleteWordToLeft(HWND hWnd) {
  285.     DWORD start, end;
  286.     SendMessage(hWnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
  287.     if (start == 0) return;
  288.  
  289.     std::wstring text(GetWindowTextLength(hWnd), L'\0');
  290.     GetWindowText(hWnd, &text[0], text.size() + 1);
  291.     text.resize(text.size() - 1);
  292.  
  293.     size_t pos = start - 1;
  294.     while (pos > 0 && iswspace(text[pos])) {
  295.         --pos;
  296.     }
  297.     while (pos > 0 && !iswspace(text[pos])) {
  298.         --pos;
  299.     }
  300.  
  301.     text.erase(pos, start - pos);
  302.     SetWindowText(hWnd, text.c_str());
  303.     SendMessage(hWnd, EM_SETSEL, pos, pos);
  304. }
  305.  
  306. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  307.     WNDCLASS wc = {};
  308.     wc.lpfnWndProc = WndProc;
  309.     wc.hInstance = hInstance;
  310.     wc.lpszClassName = L"WindowTitleModifier";
  311.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  312.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  313.     RegisterClass(&wc);
  314.  
  315.     DWORD style = (WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX) & ~WS_THICKFRAME;
  316.  
  317.     int windowWidth = 400;
  318.     int windowHeight = 350;
  319.  
  320.     int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  321.     int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  322.  
  323.     int x = (screenWidth - windowWidth) / 2;
  324.     int y = (screenHeight - windowHeight) / 2;
  325.  
  326.     // Initialize common controls (IMPORTANT!)
  327.     INITCOMMONCONTROLSEX icex;
  328.     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  329.     icex.dwICC = ICC_USEREX_CLASSES; // Include Rich Edit control class
  330.     InitCommonControlsEx(&icex);
  331.  
  332.     // Load the Rich Edit library (for versions before Windows Vista)
  333.     HMODULE hRichEdit = LoadLibrary(L"riched20.dll");
  334.  
  335.  
  336.     HWND hwnd = CreateWindowEx(0, wc.lpszClassName, L"Window Title Modifier", style, x, y, windowWidth, windowHeight, NULL, NULL, hInstance, NULL);
  337.  
  338.     if (hwnd == NULL) {
  339.         return 0;
  340.     }
  341.  
  342.     ShowWindow(hwnd, nCmdShow);
  343.     UpdateWindow(hwnd);
  344.  
  345.     MSG msg = {};
  346.     while (GetMessage(&msg, NULL, 0, 0)) {
  347.         TranslateMessage(&msg);
  348.         DispatchMessage(&msg);
  349.     }
  350.  
  351.     return 0;
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement