user input help :L

Just started learning c++ and ive got a bit stuck :L
I'm making a console application that injects a file into a process.
The file name and process name are both specified at the start of the code using

1
2
    char FileToInject[] = "File_Name.xxx";
    char ProcessName[] = "Process_Name.exe";

The console works fine, however the process name and file name are now fixed once ive compiled the console, and I am unable to change the file/process later on.

What I would like to do is have the console ask for file name, and the process name, so I dont need to create multiple console applications for different files and processes. Once the user has typed the file name and process name, I would like it to replace whats in the chars, so it is compatible with the rest of the code. Any suggestions?

Sorry if im asking too much, or maybe even a stupidly easy question, and please go easy on me. :)

Matt
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
    std::string file, proc;
    std::cout << "Enter the file name/path: " << std::flush;
    std::getline(std::cin, file);
    std::cout << "Enter the process name: " << std::flush:
    std::getline(std::cin, proc);

    //...
}


By the way, char blah[] = "string literal"; is neither valid C nor valid C++, since string literals are constant.
Last edited on
By the way, char blah[] = "string literal"; is neither valid C nor valid C++, since string literals are constant.


I think it is actually OK to do that; the array gets copies of the characters in the string literal.
Thanks for the quick replys! But im still a bit confused. I refer to the ProcessName Char later on in the code

1
2
3
4
5
if(!strcmp(pe32.szExeFile, ProcessName))
				{
					processId = pe32.th32ProcessID;
					break;
				}

How do I use the std::getline(std::cin, proc); later on? How would i refer back to the user input?

Heres the rest of the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
#include <string>

using namespace std;
bool InjectDLL(DWORD ProcessID);

char FileToInject[] = "File_Name.xxx";
char ProcessName[] = "Process_Name.exe";
typedef HINSTANCE (*fpLoadLibrary)(char*);

int main()
{
	DWORD processId = NULL;
	PROCESSENTRY32 pe32 = {sizeof(PROCESSENTRY32)};
	HANDLE hProcSnap;
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    std::string file, proc;
    std::cout << "Enter the file name/path: " << std::flush;
    std::getline(std::cin, file);
    std::cout << "Enter the process name: " << std::flush;
    std::getline(std::cin, proc);

	while(!processId)
	{
		system("CLS");

		hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
		
		if(Process32First(hProcSnap, &pe32))
		{
			do
			{
				if(!strcmp(pe32.szExeFile, ProcessName))
				{
					processId = pe32.th32ProcessID;
					break;
				}
			}
			while(Process32Next(hProcSnap, &pe32));
		}
	}

	while(!InjectDLL(processId))
	{
		system("CLS");
		cout << "Failed" << endl;
		Sleep(1000);
	}

        cout << "Done" << endl;

	CloseHandle(hProcSnap);
	Sleep(10000);

	return 0;
}

bool InjectDLL(DWORD ProcessID)
{
	HANDLE hProc;
	LPVOID paramAddr;

	HINSTANCE hDll = LoadLibrary("KERNEL32");

	fpLoadLibrary LoadLibraryAddr = (fpLoadLibrary)GetProcAddress(hDll, "LoadLibraryA");

	hProc = OpenProcess (PROCESS_ALL_ACCESS, false, ProcessID);

	char dllPath[250] = "Location in documents";

	strcat(dllPath, FileToInject);

	paramAddr = VirtualAllocEx(hProc, 0, strlen(dllPath)+1, MEM_COMMIT, PAGE_READWRITE);
	bool memoryWritten = WriteProcessMemory(hProc, paramAddr, dllPath, strlen(dllPath)+1, NULL);

	CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)LoadLibraryAddr, paramAddr, 0, 0);

	CloseHandle(hProc);

	return memoryWritten;
}
Last edited on
#include <iostream>
#include <string>

int main()
{
std::string file, proc;
std::cout << "Enter the file name/path: " << std::flush;
std::getline(std::cin, file);
std::cout << "Enter the process name: " << std::flush:
std::getline(std::cin, proc);

//...
}


How would I use this with,
1
2
3
4
5
if(!strcmp(pe32.szExeFile, ProcessName))
				{
					processId = pe32.th32ProcessID;
					break;
				}

Read a tutorial on std::string, as it seems someone accidentally taught you the horrible C way to handle strings first.
Topic archived. No new replies allowed.