I'm trying to read data from another program (that i made myself i'll display the code of the programs) with the memoryadress everything works fine I can read int char and string datas but when I try to read the value of a string i get an error showing on visual studio(Yesterday this wasn't happening) I still get the right output but yet I can't rerun the program with a loop : it says "Violating reading access _Pnext has been 0xDE74C"
-The code of the program i'm trying to read datas from:
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main()
{
const int sizeArrChar = 128;
int varInt = 123456;
string varString = "DefaultString";
char arrChar[sizeArrChar] = "Long char array right there ->";
int *ptr2int = &varInt;
int **ptr2ptr = &ptr2int;
int ***ptr2ptr2 = &ptr2ptr;
for (;;)
{
cout << "Process ID: " << GetCurrentProcessId() << "\n"<< endl;
cout << "varInt (0x" << &varInt << ") = " << varInt << endl;
cout << "varString (0x" << &varString << ") = " << varString << endl;
cout << "arrChar (0x" << &arrChar << ") = " << arrChar << endl;
cout << "ptr2int (0x" << &ptr2int << ") = " << ptr2int << endl;
cout << "ptr2ptr (0x" << &ptr2ptr << ") = " << ptr2ptr << endl;
cout << "ptr2ptr2 (0x" << &ptr2ptr2 << ") = " << ptr2ptr2 << "\n" << endl;
cout << "Press enter to cout again!" << "\n" << "\n" << endl;
cout << "---------------------------------------------------------" << endl;
system("pause");
}
system("pause");
return 0;
}
-The code of the program to read the datas of the program above : (This is only to read string not int or char)
#include <iostream>
#include <Windows.h>
#include <string>
int main()
{
using namespace std;
int processNumber;
cout << "Enter the process number" << endl;
cin >> processNumber;
for (;;)
{
uintptr_t memoryAdress = 0x0;
cout << "Enter memoryadress" << endl;
cin >> hex >> memoryAdress;
cout << hex << memoryAdress << endl;
HANDLE hProcess = OpenProcess(PROCESS_VM_READ, FALSE, processNumber);
if (hProcess == NULL) { // Failed to get a handle
cout << "OpenProcess failed. GetLastError = " << dec << GetLastError() << endl;
system("pause");
return EXIT_FAILURE;
}
string intRead;
ReadProcessMemory(hProcess, (LPCVOID)memoryAdress, &intRead, sizeof(string), NULL);
cout << "intRead = " << intRead << endl;
BOOL WINAPI CloseHandle(
_In_ HANDLE hObject
);
system("pause");
}
return 0;
}
Everything works i get the right output but i can only read the data once because of the error occuring so I can't read the data several times in a row and that's the main issue.
Here is the output:
Enter the process number
14788
Enter memoryadress
0x009DFC2C
9dfc2c
intRead = DefaultString
Appuyez sur une touche pour continuer...