Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <CommCtrl.h>
- #include <string>
- #include <TlHelp32.h>
- #include <tchar.h>
- #include <Psapi.h>
- #include <iostream>
- #include <vector>
- #include <sstream>
- #include <thread>
- #include <WinUser.h>
- #include <richedit.h>
- #pragma comment(lib, "Psapi.lib")
- #pragma comment(lib, "Comctl32.lib")
- void DeleteWordToLeft(HWND hEdit);
- struct ProcessInfo {
- std::wstring processName;
- HWND hwnd;
- };
- std::vector<ProcessInfo> targetProcesses;
- std::vector<std::wstring> processNames;
- bool running = true;
- HWND hRichTextBox, hTextBox, hButton, hCloseButton, hGroupBox, hLabel;
- void DeleteWordToLeft(HWND hEdit);
- void AppendTextToRichEdit(HWND hRichText, const std::wstring& text) {
- int len = GetWindowTextLength(hRichText);
- SendMessage(hRichText, EM_SETSEL, (WPARAM)len, (LPARAM)len);
- SendMessage(hRichText, EM_REPLACESEL, 0, (LPARAM)text.c_str());
- }
- BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege) {
- TOKEN_PRIVILEGES tp;
- LUID luid;
- if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid)) {
- AppendTextToRichEdit(hRichTextBox, L"LookupPrivilegeValue error: " + std::to_wstring(GetLastError()) + L"\n");
- return FALSE;
- }
- tp.PrivilegeCount = 1;
- tp.Privileges[0].Luid = luid;
- tp.Privileges[0].Attributes = (bEnablePrivilege) ? SE_PRIVILEGE_ENABLED : 0;
- if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
- AppendTextToRichEdit(hRichTextBox, L"AdjustTokenPrivileges error: " + std::to_wstring(GetLastError()) + L"\n");
- return FALSE;
- }
- if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
- AppendTextToRichEdit(hRichTextBox, L"The token does not have the specified privilege.\n");
- return FALSE;
- }
- return TRUE;
- }
- BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
- DWORD processId;
- GetWindowThreadProcessId(hwnd, &processId);
- HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
- if (hProcess != NULL) {
- TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
- if (GetModuleFileNameEx(hProcess, NULL, szProcessName, MAX_PATH)) {
- std::wstring processName(szProcessName);
- size_t pos = processName.find_last_of(L"\\");
- if (pos != std::wstring::npos) {
- processName = processName.substr(pos + 1);
- }
- CloseHandle(hProcess);
- TCHAR windowTitle[MAX_PATH];
- GetWindowText(hwnd, windowTitle, MAX_PATH);
- for (const auto& targetName : *(std::vector<std::wstring>*)lParam) {
- if (processName == targetName) {
- targetProcesses.push_back({ processName, hwnd });
- }
- }
- }
- else {
- AppendTextToRichEdit(hRichTextBox, L"GetModuleFileNameEx error: " + std::to_wstring(GetLastError()) + L"\n");
- }
- }
- return TRUE;
- }
- bool ModifyWindowTitles(const std::wstring& newTitle) {
- bool anyModified = false;
- for (const auto& process : targetProcesses) {
- if (SetWindowText(process.hwnd, newTitle.c_str())) {
- anyModified = true;
- AppendTextToRichEdit(hRichTextBox, L"\nModified window title of " + process.processName + L"\n");
- }
- else {
- AppendTextToRichEdit(hRichTextBox, L"\nFailed to modify window title of " + process.processName + L"\n");
- }
- }
- return anyModified;
- }
- DWORD WINAPI TitleModifierThread(LPVOID lpParam) {
- std::wstring newTitle = *(std::wstring*)lpParam;
- while (running) {
- ModifyWindowTitles(newTitle);
- Sleep(1000);
- }
- return 0;
- }
- std::vector<std::wstring> Split(const std::wstring& s, wchar_t delimiter) {
- std::vector<std::wstring> tokens;
- std::wstring token;
- std::wistringstream tokenStream(s);
- while (std::getline(tokenStream, token, delimiter)) {
- tokens.push_back(token);
- }
- return tokens;
- }
- void ProcessInput(HWND hwnd) {
- static int step = 0;
- static std::wstring newTitle;
- static std::wstring persistence;
- wchar_t buffer[256];
- GetWindowText(hTextBox, buffer, 256);
- std::wstring input(buffer);
- // Echo the input to the RichTextBox
- AppendTextToRichEdit(hRichTextBox, L"> " + input + L"\n");
- switch (step) {
- case 0:
- processNames = Split(input, L';');
- AppendTextToRichEdit(hRichTextBox, L"\nEnter the new window title text: \n");
- step++;
- break;
- case 1:
- newTitle = input;
- AppendTextToRichEdit(hRichTextBox, L"\n\nDo you want persistence (Y/N)? \n");
- step++;
- break;
- case 2:
- persistence = input;
- HANDLE hToken;
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
- AppendTextToRichEdit(hRichTextBox, L"OpenProcessToken error: " + std::to_wstring(GetLastError()) + L"\n");
- return;
- }
- if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
- AppendTextToRichEdit(hRichTextBox, L"Failed to enable debug privilege.\n");
- CloseHandle(hToken);
- return;
- }
- EnumWindows(EnumWindowsProc, (LPARAM)&processNames);
- if (targetProcesses.empty()) {
- AppendTextToRichEdit(hRichTextBox, L"No windows found for the specified processes.\n");
- }
- else {
- if (persistence == L"Y" || persistence == L"y") {
- std::thread titleModifierThread(TitleModifierThread, &newTitle);
- titleModifierThread.detach();
- AppendTextToRichEdit(hRichTextBox, L"Window titles are being modified persistently. Press Enter to stop and exit...\n");
- }
- else {
- ModifyWindowTitles(newTitle);
- AppendTextToRichEdit(hRichTextBox, L"Window titles modified once. Press Enter to exit...\n");
- }
- }
- SetPrivilege(hToken, SE_DEBUG_NAME, FALSE);
- CloseHandle(hToken);
- step++;
- break;
- case 3:
- PostQuitMessage(0);
- break;
- }
- SetWindowText(hTextBox, L"");
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
- switch (uMsg) {
- case WM_CREATE:
- hRichTextBox = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY,
- 10, 10, 372, 200, hwnd, (HMENU)1, NULL, NULL);
- hTextBox = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_MULTILINE,
- 10, 220, 280, 20, hwnd, (HMENU)2, NULL, NULL);
- hButton = CreateWindowEx(0, L"BUTTON", L"Send", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
- 300, 220, 80, 20, hwnd, (HMENU)3, NULL, NULL);
- hCloseButton = CreateWindowEx(0, L"BUTTON", L"Close", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
- 10, 250, 290, 20, hwnd, (HMENU)4, NULL, NULL);
- hLabel = CreateWindowEx(0, L"STATIC", L"2024 (c) Entisoft Software. All Rights Reserved. Evans Thorpemorton.", WS_CHILD | WS_VISIBLE | SS_CENTER,
- 10, 280, 372, 20, hwnd, (HMENU)5, NULL, NULL);
- AppendTextToRichEdit(hRichTextBox, L"Enter the process names separated by semicolons (e.g., notepad.exe;calc.exe): \n");
- SetTimer(hwnd, 1, 10, NULL);
- break;
- case WM_COMMAND:
- if (LOWORD(wParam) == 3) {
- ProcessInput(hwnd);
- }
- else if (LOWORD(wParam) == 4) {
- PostQuitMessage(0);
- }
- break;
- case WM_TIMER:
- if (wParam == 1) {
- if (GetFocus() == hTextBox) {
- if (GetAsyncKeyState(VK_RETURN) & 0x8000) {
- static DWORD lastEnterTime = 0;
- DWORD currentTime = GetTickCount();
- if (currentTime - lastEnterTime > 500) {
- ProcessInput(hwnd);
- lastEnterTime = currentTime;
- }
- return 0;
- }
- if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState('A') & 0x8000)) {
- SendMessage(hTextBox, EM_SETSEL, 0, -1);
- return 0;
- }
- if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState(VK_BACK) & 0x8000)) {
- static DWORD lastCtrlBackspaceTime = 0;
- DWORD currentTime = GetTickCount();
- if (currentTime - lastCtrlBackspaceTime > 100) {
- DeleteWordToLeft(hTextBox);
- lastCtrlBackspaceTime = currentTime;
- }
- return 0;
- }
- }
- }
- break;
- case WM_DESTROY:
- running = false;
- KillTimer(hwnd, 1);
- PostQuitMessage(0);
- break;
- case WM_SIZE:
- MoveWindow(hLabel, 10, HIWORD(lParam) - 20, 372, 20, TRUE);
- break;
- default:
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- return 0;
- }
- void DeleteWordToLeft(HWND hWnd) {
- DWORD start, end;
- SendMessage(hWnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
- if (start == 0) return;
- std::wstring text(GetWindowTextLength(hWnd), L'\0');
- GetWindowText(hWnd, &text[0], text.size() + 1);
- text.resize(text.size() - 1);
- size_t pos = start - 1;
- while (pos > 0 && iswspace(text[pos])) {
- --pos;
- }
- while (pos > 0 && !iswspace(text[pos])) {
- --pos;
- }
- text.erase(pos, start - pos);
- SetWindowText(hWnd, text.c_str());
- SendMessage(hWnd, EM_SETSEL, pos, pos);
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
- WNDCLASS wc = {};
- wc.lpfnWndProc = WndProc;
- wc.hInstance = hInstance;
- wc.lpszClassName = L"WindowTitleModifier";
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- RegisterClass(&wc);
- DWORD style = (WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX) & ~WS_THICKFRAME;
- int windowWidth = 400;
- int windowHeight = 350;
- int screenWidth = GetSystemMetrics(SM_CXSCREEN);
- int screenHeight = GetSystemMetrics(SM_CYSCREEN);
- int x = (screenWidth - windowWidth) / 2;
- int y = (screenHeight - windowHeight) / 2;
- // Initialize common controls (IMPORTANT!)
- INITCOMMONCONTROLSEX icex;
- icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
- icex.dwICC = ICC_USEREX_CLASSES; // Include Rich Edit control class
- InitCommonControlsEx(&icex);
- // Load the Rich Edit library (for versions before Windows Vista)
- HMODULE hRichEdit = LoadLibrary(L"riched20.dll");
- HWND hwnd = CreateWindowEx(0, wc.lpszClassName, L"Window Title Modifier", style, x, y, windowWidth, windowHeight, NULL, NULL, hInstance, NULL);
- if (hwnd == NULL) {
- return 0;
- }
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- MSG msg = {};
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement