Salve ha tutti, mi servirebbe una mamo per finire questo programma, quello che deve fare é inizializzare un buffer i memoria ram, scrivere l'indirizzo della memoria in un file txt, avviare un secondo programma figlio e aspetta che il primo byte di memoria si inizializza ha 1, il secondo programma legge il valore nel file txt, e cambia il primo byte nella ram ha 1.
Questo è quanto ho scritto finora:
Lib.h
#include <iostream>
#include <WS2tcpip.h>
#include <fstream>
#include <string.h>
#include <string>
#include <thread>
#include <chrono>
#include <filesystem>
#include <direct.h>
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <memory>
#include <sys/stat.h>
#include <cctype>
#include <tchar.h>
#include <strsafe.h>
#include <bitset>
#include <cstring>
#include <Sensapi.h>
#include <boost/process.hpp>
#include <nlohmann/json.hpp>
#include <thread>
#include <vector>
#include <execution>
#pragma comment(lib, "Sensapi.lib")
#pragma comment (lib, "ws2_32.lib")
#pragma comment (lib, "User32.lib")
using json = nlohmann::json;
using namespace std;
using namespace std::chrono;
namespace bp = boost::process;
CTR.h
#ifndef CTR_H
#define CTR_H
namespace CTR {
class CTR {
public:
int* Ind_S = new int;
int* Ind_C = new int;
int Pdr_S = 0;
int Num_Elm = 20;
int Num_Gr_Elm = sizeof(int);
int Num_Byte = Num_Elm * Num_Gr_Elm;
void CTR_S_Start(char* Pos_File);
void CTR_S_Connect();
//void CTR_S_Message(const char* Message, int Pos_Mes);
//void CTR_S_Close();
void CTR_C_Connect(char* Pos_File);
//void CTR_C_Message(const char* Message, int Pos_Mes);
//void CTR_C_Disconect();
//void CTR_();
};
}
//#include "CTR.cpp"
#endif
CTR.cpp
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <shellapi.h>
#include <bitset>
#include <string>
#include <cstdio>
#include <stdio.h>
#include <process.h>
#include <strsafe.h>
using namespace std;
#include "CTR.h"
#pragma warning (disable : 4996)
#ifndef CTR_CPP
#define CTR_CPP
int* Byte(int N_Byte) {
int* ptr = (int*)malloc(N_Byte);
return ptr;
}
char* Sost_Per(char* Pos_File, const char* Nom_File) {
char* POS_FILE = new char[strlen(Pos_File) + 1];
strcpy(POS_FILE, Pos_File);
char* File = strrchr(POS_FILE, '\\');
if (File) {
*File = '\0';
}
char* p = strcat(POS_FILE, (const char*)Nom_File);
return p;
}
void Null_Byte(int* Byte, int Gra_Byte) {
for (int i = 0; i <= Gra_Byte; i++) {
Byte[i] = 0;
}
}
void Message(int* Byte, int Pos_Mes, int Mes) {
Byte[Pos_Mes] = Mes;
}
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
void Convert_Char_Int_Ram(int* Ind, char* Data) {
char Ind_Char[20] = "";
int len = std::strlen(Data);
for (int i = 0; i < len; i++) {
Data[i] = std::tolower(Data[i]);
}
strcat(Ind_Char, "0x");
strcat(Ind_Char, Data);
long long n = (long long)strtoll(Ind_Char, NULL, 16);
long long* ptr = (long long*)n;
Ind = reinterpret_cast<int*>(ptr);
}
void CTR::CTR::CTR_S_Start(char* Pos_File) {
int* Pos_Byte = Byte(CTR::Num_Byte);
CTR::Ind_S = Pos_Byte;
CTR::Pdr_S = _getpid();
char* asdfg = new char;
std::sprintf(asdfg, "%d", CTR::Pdr_S);
char* ptrova = new char;
int Error_Sscanf = sscanf(reinterpret_cast<char*>(&Pos_Byte), "%p", &Pos_Byte);
sprintf(ptrova, "%p", Pos_Byte);
if (Pos_Byte != NULL) {
Null_Byte(Pos_Byte, CTR::Num_Elm);
FILE* File = fopen(Pos_File, "w");
if (File != NULL) {
fprintf(File, ptrova);
fprintf(File, "\n");
fprintf(File, asdfg);
fclose(File);
}
}
else
{
cout << "Si è verificato un errore nella allocazione di memoria - " << Pos_Byte << " - " << endl;
system("pause");
}
}
void CTR::CTR::CTR_S_Connect() {
while (CTR::Ind_S[0] != 1) {}
printf("ciao");
}
void CTR::CTR::CTR_C_Connect(char* Pos_File) {
FILE* File = fopen(Pos_File, "r");
char RAM[20];
char PDR_C[20];
int PDR = 0;
if (File != NULL) {
fgets(RAM, 20, File);
fgets(PDR_C, 20, File);
fclose(File);
}
Convert_Char_Int_Ram(CTR::Ind_C, RAM);
CTR::Pdr_S = atoi(PDR_C);
HANDLE hProcess = OpenProcess(PROCESS_VM_READ, FALSE, CTR::Pdr_S);
if (hProcess == NULL) {
std::cout << "Errore nell'apertura del processo" << std::endl;
}
DWORD value;
SIZE_T bytesRead;
LPVOID zxc = (LPVOID)reinterpret_cast<LPVOID>(CTR::Ind_C);
if (!ReadProcessMemory(hProcess, zxc, &value, sizeof(value), NULL)) {
ErrorExit((LPTSTR)TEXT("ReadProcessMemory"));
std::cout << "Errore nella lettura della memoria" << std::endl;
}
cout << value;
/*
printf("Valori degli elementi\n");
for (int i = 0; i < 20; i++)
{
printf("%6d%c", CTR::Ind_C[i], (i % 10 == 9) ? '\n' : ' ');
}
Message(CTR::Ind_C, 0, 1);
*/
}
#endif
Server_Programma_Principale.cpp : int main
#include "Debug/Client_Programma_Secondario/Lib.h"
#include "Debug/Client_Programma_Secondario/CTR.h"
int main()
{
CTR::CTR q;
q.CTR_S_Start((char*)"*\\Server_Programma_Principale\Debug\\Client_Programma_Secondario\\Debug\\Data.txt");
char* ptrova = new char;
int Error_Sscanf = sscanf(reinterpret_cast<char*>(&q.Ind_S), "%p", &q.Ind_S);
sprintf(ptrova, "%p", q.Ind_S);
cout << ptrova << endl;
bp::child c("*\\Server_Programma_Principale\\Debug\\Client_Programma_Secondario\\Debug\\Client_Programma_Secondario.exe");
}
Client_Programma_Secondario.cpp
#include "Lib.h"
#include "CTR.h"
int main()
{
CTR::CTR q;
q.CTR_C_Connect((char*)"Data.txt");
char* ptrova = new char;
int Error_Sscanf = sscanf(reinterpret_cast<char*>(&q.Ind_C), "%p", &q.Ind_C);
sprintf(ptrova, "%p", q.Ind_C);
cout << ptrova << endl;
}