Advertisement
alien_fx_fiend

AdvTitleChanger (GUI-Based)

Jul 12th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.11 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.     switch (step) {
  138.     case 0:
  139.         processNames = Split(input, L';');
  140.         AppendTextToRichEdit(hRichTextBox, L"\nEnter the new window title text: \n");
  141.         step++;
  142.         break;
  143.  
  144.     case 1:
  145.         newTitle = input;
  146.         AppendTextToRichEdit(hRichTextBox, L"\n\nDo you want persistence (Y/N)? \n");
  147.         step++;
  148.         break;
  149.  
  150.     case 2:
  151.         persistence = input;
  152.         HANDLE hToken;
  153.         if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
  154.             AppendTextToRichEdit(hRichTextBox, L"OpenProcessToken error: " + std::to_wstring(GetLastError()) + L"\n");
  155.             return;
  156.         }
  157.  
  158.         if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
  159.             AppendTextToRichEdit(hRichTextBox, L"Failed to enable debug privilege.\n");
  160.             CloseHandle(hToken);
  161.             return;
  162.         }
  163.  
  164.         EnumWindows(EnumWindowsProc, (LPARAM)&processNames);
  165.  
  166.         if (targetProcesses.empty()) {
  167.             AppendTextToRichEdit(hRichTextBox, L"No windows found for the specified processes.\n");
  168.         }
  169.         else {
  170.             if (persistence == L"Y" || persistence == L"y") {
  171.                 std::thread titleModifierThread(TitleModifierThread, &newTitle);
  172.                 titleModifierThread.detach();
  173.  
  174.                 AppendTextToRichEdit(hRichTextBox, L"Window titles are being modified persistently. Press Enter to stop and exit...\n");
  175.             }
  176.             else {
  177.                 ModifyWindowTitles(newTitle);
  178.                 AppendTextToRichEdit(hRichTextBox, L"Window titles modified once. Press Enter to exit...\n");
  179.             }
  180.         }
  181.  
  182.         SetPrivilege(hToken, SE_DEBUG_NAME, FALSE);
  183.         CloseHandle(hToken);
  184.         step++;
  185.         break;
  186.  
  187.     case 3:
  188.         PostQuitMessage(0);
  189.         break;
  190.     }
  191.  
  192.     SetWindowText(hTextBox, L"");
  193. }
  194.  
  195. LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  196.     switch (uMsg) {
  197.     case WM_CREATE:
  198.         hRichTextBox = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY,
  199.             10, 10, 372, 200, hwnd, (HMENU)1, NULL, NULL);
  200.         hTextBox = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_MULTILINE,
  201.             10, 220, 280, 20, hwnd, (HMENU)2, NULL, NULL);
  202.         hButton = CreateWindowEx(0, L"BUTTON", L"Send", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  203.             300, 220, 80, 20, hwnd, (HMENU)3, NULL, NULL);
  204.         hCloseButton = CreateWindowEx(0, L"BUTTON", L"Close", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  205.             10, 250, 290, 20, hwnd, (HMENU)4, NULL, NULL);
  206.         hLabel = CreateWindowEx(0, L"STATIC", L"2024 (c) Entisoft Software. All Rights Reserved. Evans Thorpemorton.", WS_CHILD | WS_VISIBLE | SS_CENTER,
  207.             10, 280, 372, 20, hwnd, (HMENU)5, NULL, NULL);
  208.  
  209.         AppendTextToRichEdit(hRichTextBox, L"Enter the process names separated by semicolons (e.g., notepad.exe;calc.exe): \n");
  210.         SetTimer(hwnd, 1, 10, NULL);
  211.         break;
  212.  
  213.     case WM_COMMAND:
  214.         if (LOWORD(wParam) == 3) {
  215.             ProcessInput(hwnd);
  216.         }
  217.         else if (LOWORD(wParam) == 4) {
  218.             PostQuitMessage(0);
  219.         }
  220.         break;
  221.  
  222.     case WM_TIMER:
  223.         if (wParam == 1) {
  224.             if (GetFocus() == hTextBox) {
  225.                 if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
  226.                     static DWORD lastEnterTime = 0;
  227.                     DWORD currentTime = GetTickCount();
  228.                     if (currentTime - lastEnterTime > 500) {
  229.                         ProcessInput(hwnd);
  230.                         lastEnterTime = currentTime;
  231.                     }
  232.                     return 0;
  233.                 }
  234.                 if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState('A') & 0x8000)) {
  235.                     SendMessage(hTextBox, EM_SETSEL, 0, -1);
  236.                     return 0;
  237.                 }
  238.                 if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState(VK_BACK) & 0x8000)) {
  239.                     static DWORD lastCtrlBackspaceTime = 0;
  240.                     DWORD currentTime = GetTickCount();
  241.                     if (currentTime - lastCtrlBackspaceTime > 100) {
  242.                         DeleteWordToLeft(hTextBox);
  243.                         lastCtrlBackspaceTime = currentTime;
  244.                     }
  245.                     return 0;
  246.                 }
  247.             }
  248.         }
  249.         break;
  250.  
  251.     case WM_DESTROY:
  252.         running = false;
  253.         KillTimer(hwnd, 1);
  254.         PostQuitMessage(0);
  255.         break;
  256.  
  257.     case WM_SIZE:
  258.         MoveWindow(hLabel, 10, HIWORD(lParam) - 20, 372, 20, TRUE);
  259.         break;
  260.  
  261.     default:
  262.         return DefWindowProc(hwnd, uMsg, wParam, lParam);
  263.     }
  264.  
  265.     return 0;
  266. }
  267.  
  268. void DeleteWordToLeft(HWND hWnd) {
  269.     DWORD start, end;
  270.     SendMessage(hWnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
  271.     if (start == 0) return;
  272.  
  273.     std::wstring text(GetWindowTextLength(hWnd), L'\0');
  274.     GetWindowText(hWnd, &text[0], text.size() + 1);
  275.     text.resize(text.size() - 1);
  276.  
  277.     size_t pos = start - 1;
  278.     while (pos > 0 && iswspace(text[pos])) {
  279.         --pos;
  280.     }
  281.     while (pos > 0 && !iswspace(text[pos])) {
  282.         --pos;
  283.     }
  284.  
  285.     text.erase(pos, start - pos);
  286.     SetWindowText(hWnd, text.c_str());
  287.     SendMessage(hWnd, EM_SETSEL, pos, pos);
  288. }
  289.  
  290. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  291.     WNDCLASS wc = {};
  292.     wc.lpfnWndProc = WndProc;
  293.     wc.hInstance = hInstance;
  294.     wc.lpszClassName = L"WindowTitleModifier";
  295.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  296.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  297.     RegisterClass(&wc);
  298.  
  299.     DWORD style = (WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX) & ~WS_THICKFRAME;
  300.  
  301.     int windowWidth = 400;
  302.     int windowHeight = 350;
  303.  
  304.     int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  305.     int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  306.  
  307.     int x = (screenWidth - windowWidth) / 2;
  308.     int y = (screenHeight - windowHeight) / 2;
  309.  
  310.     // Initialize common controls (IMPORTANT!)
  311.     INITCOMMONCONTROLSEX icex;
  312.     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  313.     icex.dwICC = ICC_USEREX_CLASSES; // Include Rich Edit control class
  314.     InitCommonControlsEx(&icex);
  315.  
  316.     // Load the Rich Edit library (for versions before Windows Vista)
  317.     HMODULE hRichEdit = LoadLibrary(L"riched20.dll");
  318.  
  319.  
  320.     HWND hwnd = CreateWindowEx(0, wc.lpszClassName, L"Window Title Modifier", style, x, y, windowWidth, windowHeight, NULL, NULL, hInstance, NULL);
  321.  
  322.     if (hwnd == NULL) {
  323.         return 0;
  324.     }
  325.  
  326.     ShowWindow(hwnd, nCmdShow);
  327.     UpdateWindow(hwnd);
  328.  
  329.     MSG msg = {};
  330.     while (GetMessage(&msg, NULL, 0, 0)) {
  331.         TranslateMessage(&msg);
  332.         DispatchMessage(&msg);
  333.     }
  334.  
  335.     return 0;
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement