Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I compare a DWORD with an LPCSTR? Or can you please tell me how to convert them both.

What I have tried:

How do I compare a DWORD with an LPCSTR? Or can you please tell me how to convert them both.
C++
#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"*/ pid /*)*/);<<<<<<<<<<<<<<<<<<<<<
	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 11:39am
v2
Comments
Stefan_Lang 1-Jan-21 6:51am    
What is 'dword' in your pid vector definition? If you mean DWORD, you must write DWORD. If the type dword is defined elsewhere, chances are that it doesn't match DWORD! This is all the more likely because the definition changes depending on whether you build for x86 or x64.

Quote:
How do I compare a DWORD with an LPCSTR?
Both types contain simple binary values so you can do a direct compare with a cast:
C++
DWORD ProcessId = 30850.352; // this value does not look correct
LPCWSTR pid;

if (ProcessId == (DWORD)pid)
{
// both values are the same
}
 
Share this answer
 
v2
Comments
Stefan_Lang 1-Jan-21 6:19am    
Is it april 1st already?
Richard MacCutchan 1-Jan-21 6:25am    
Er, no.
Stefan_Lang 1-Jan-21 8:06am    
Then we seem to have a different understanding - I wouldn't consider it wise to cast a pointer to an actual string into a DWORD and compare it to another DWORD that is clearly not a pointer and expect that to make any sense. Note that this pid is passed to FindWindow() which only expects string parameters.
Richard MacCutchan 1-Jan-21 8:12am    
I know it is wrong, and something I would never do. But just look at that DWORD initialisation. I have tried to get this questioner to go and learn some proper C/C++ etc for quite a long time. But all he does is copy some code that he really does not understand and then ask us what it means. So actually trying to diagnose it correctly and add all the necessary corrections is largely a waste of time. And looking at the actual code in the question, I doubt it will ever work.
Stefan_Lang 1-Jan-21 8:21am    
I see. I haven't been on this site for some months so I'm not up to date to the who's who ;-)

And, yes, I also have some doubt as to the mechanics used in this program. There are way too many things I've spotted on a glance that seem dubious to say the least. That said, I always try to avoid this kind of low level Windows stuff, there's way too many traps for the unwary, side effects, odd (and type-unsafe) ways of passing information, unclear responsibilities, and generally outdated concepts (and when I say outdated, I mean concepts developed for Windows 3.x)

Anyway, have a happy new year!
You're using the wrong function. FindWindow expects actual text strings that it wishes to compare with the window class name and the window title. The process ID does not contain any such text info - it is just a number! You have to come up with the actual window title instead. E. g. if you're looking for the Snipping Tool window, you should search for
C++
_T("Snipping Tool")
.Of course, the problem is that window titles are volatile: the application may choose to change it any time. If there is a function that retrieves the (current) window title based on the process ID, I'm not aware of it. More likely there may be a function that retrieves window handle(s) for a given process ID. A quick search gives me this[^]
 
Share this answer
 
Please delete this question. I posted it wrong.
 
Share this answer
 

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