Someone help me with C++ code

L00PeR
7 years ago | edited 7 years ago

0

Hi, I’m trying to read the memory of a program:
```
// Test

include

include <Windows.h>

using namespace std;
int main(){
string test = “test”;
cout <<&test<<endl;
while(1){

cout << "s="<<test<<endl;  
system("pause");  
}  

}
When you execute this code, it prints the address of "test", and then, every time you press enter, it tells you its value.

include

include <Windows.h>

using namespace std;
int main(){

char PROCNAME[50] = "C:\\Users\\Usuario\\Desktop\\test.exe";  
cout << "Press enter to start."<<endl;  

HWND hwnd = FindWindow(NULL, PROCNAME);  
cout << "[*] Searching process...";  
if (hwnd == NULL){  
    cout << " [!]"<<endl;  
    MessageBoxA(NULL, "Couldn't find window", "ERROR", MB_OK | MB_ICONERROR);  
    exit(-1);  
}  
cout << "[DONE]"<<endl;  
DWORD procId;  
GetWindowThreadProcessId(hwnd, &procId);  
cout << "[+] "<<PROCNAME << " is on: "<<procId<<endl;  
cout << "[*] Opening process...";  
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procId);  
if (process == NULL){  
    cout << " [ERROR]"<<endl;  
    MessageBoxA(NULL, "Cannot obtain process!", "Error", MB_OK | MB_ICONERROR);  
    exit(-1);  
}  
cout << " [DONE]"<<endl;  
char buffer[500];  
LPCVOID addr = (LPCVOID)0xb4fd70;  
ReadProcessMemory(process, addr, &buffer, sizeof(buffer), 0);  
cout << "Buffer: "<<buffer<<endl;  

return 0;  

}
```
With this code I’m trying to read that variable, but I can’t (Returns me strange ASCII letters).
Can someone tell me whats wrong??
Edit: I can attach to the process correctly, but when I use ReadProcessMemory() , Doesn’t return what it should

1reply
1voice
177views
L00PeR
7 years ago

0

Oh, really sorry, I founded the solution by myself.
Let me explain were was the problem.
I am working with Dev-C++, So I was executing both programs by pressing f11.
The solution was to run the programs by double-clicking them from the Desktop, not from Dev-c++.

You must be logged in to reply to this discussion. Login
1 of 2

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss