Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
How to put wstring title in the LPCWSTR findwindow? Or how do I convert wstring title?
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
#include <tlhelp32.h>
#include <sstream>
#include <stdlib.h>
#include <atlstr.h>
#include <locale>
#include <codecvt>

using namespace std;
DWORD ProcessId = 30850.352;
LPCWSTR pid;
LPWSTR widestr;
HWND hwnd;
bool isNotepad(const PROCESSENTRY32W& entry) {
return std::wstring(entry.szExeFile) == L"simplewrapper.exe";
}

BOOL CALLBACK enumWindowsProc(HWND hwnd, LPARAM lParam) {
const auto& pids = *reinterpret_cast<std::vector<dword>*>(lParam);


std::string winId2;
DWORD winId;
GetWindowThreadProcessId(hwnd, &winId);

//Converting DWORD to string
std::string DwordToString(std::string winId2, std::string cur_str, DWORD winId);
{
std::string winId2 = std::to_string(long long(winId));

}
for (DWORD pid : pids) {
if (winId == pid) {
std::wstring title(GetWindowTextLength(hwnd) + 1, L'\0'); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
GetWindowTextW(hwnd, &title[0], title.size()); //note: C++11 only

std::cout << "Found window:\n";
std::cout << "Process ID: " << pid << '\n';
std::wcout << "Title: " << title << "\n\n";

return 0;
}
}
return TRUE;
}

int main(int argc, char** argv) {

std::vector<dword> pids;

HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

PROCESSENTRY32W entry;
entry.dwSize = sizeof entry;

if (!Process32FirstW(snap, &entry)) {
return 0;
}

do {
if (isNotepad(entry)) {
pids.emplace_back(entry.th32ProcessID);
}
} while (Process32NextW(snap, &entry));

EnumWindows(enumWindowsProc, reinterpret_cast<lparam>(&pids));
int lpBuffer = 30000.000;

hwnd = FindWindow(0, /*TEXT("MSCTFIME UI"*/ title /*)*/);<<<<<<<<<<<<<<<<<<<<<
if (hwnd == NULL) {

cout << "Cannot find window." << endl;
}
else {
cout << "Found window." << endl;
GetWindowThreadProcessId(hwnd, &ProcessId);
//HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);
}
if (HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId)) {
cout << "Opened Process." << endl;
int MemoryHack = WriteProcessMemory(hProcess, (LPVOID)0x70EB2190, &lpBuffer, sizeof(lpBuffer), NULL);
int MemoryHack2 = WriteProcessMemory(hProcess, (LPVOID)0x70EB2190, &lpBuffer, sizeof(lpBuffer), NULL);
if (!MemoryHack) {
cout << "Unable to write MemoryHack to memory" << endl;
}
else {
cout << "Failed to OpenProcess." << endl;
}
CloseHandle(hProcess);
}
else {

}
return 0;
}

What I have tried:

<pre>#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
#include <tlhelp32.h>
#include <sstream>
#include <stdlib.h>
#include <atlstr.h>
#include <locale>
#include <codecvt>

using namespace std;
DWORD ProcessId = 30850.352;
LPCWSTR pid;
LPWSTR widestr;
HWND hwnd;
bool isNotepad(const PROCESSENTRY32W& entry) {
	return std::wstring(entry.szExeFile) == L"simplewrapper.exe";
}

BOOL CALLBACK enumWindowsProc(HWND hwnd, LPARAM lParam) {
	const auto& pids = *reinterpret_cast<std::vector<dword>*>(lParam);

	
	std::string winId2;
	DWORD winId;
	GetWindowThreadProcessId(hwnd, &winId);

	//Converting DWORD to string
	std::string DwordToString(std::string winId2, std::string cur_str, DWORD winId);
	{
		std::string winId2 = std::to_string(long long(winId));
	
	}
	        for (DWORD pid : pids) { 
	        if (winId == pid) {
				std::wstring title(GetWindowTextLength(hwnd) + 1, L'\0');    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			GetWindowTextW(hwnd, &title[0], title.size()); //note: C++11 only

			std::cout << "Found window:\n";
			std::cout << "Process ID: " << pid << '\n';
			std::wcout << "Title: " << title << "\n\n";

			return 0;
	} 
	 }
	return TRUE;
}

int main(int argc, char** argv) {

	std::vector<dword> pids;

	HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

	PROCESSENTRY32W entry;
	entry.dwSize = sizeof entry;

	if (!Process32FirstW(snap, &entry)) {
		return 0;
	}

	do {
		if (isNotepad(entry)) {
			pids.emplace_back(entry.th32ProcessID);
		}
	} while (Process32NextW(snap, &entry));

	EnumWindows(enumWindowsProc, reinterpret_cast<lparam>(&pids));
	int lpBuffer = 30000.000;

	hwnd = FindWindow(0, /*TEXT("MSCTFIME UI"*/ title /*)*/);<<<<<<<<<<<<<<<<<<<<<
	if (hwnd == NULL) {

		cout << "Cannot find window." << endl;
	}
	else {
		cout << "Found window." << endl;
		GetWindowThreadProcessId(hwnd, &ProcessId);
		//HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);
	}
	if (HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId)) {
		cout << "Opened Process." << endl;
		int MemoryHack = WriteProcessMemory(hProcess, (LPVOID)0x70EB2190, &lpBuffer, sizeof(lpBuffer), NULL);
		int MemoryHack2 = WriteProcessMemory(hProcess, (LPVOID)0x70EB2190, &lpBuffer, sizeof(lpBuffer), NULL);
		if (!MemoryHack) {
			cout << "Unable to write MemoryHack to memory" << endl;
		}
		else {
			cout << "Failed to OpenProcess." << endl;
		}
		CloseHandle(hProcess);
	}
	else {

	}
	return 0;
}
Posted
Updated 1-Jan-21 23:43pm
Comments
Rick York 1-Jan-21 18:04pm    
ain't even going to bother.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900