I'm on windows using c++ and making a simple method to communicate between two processes
the first process creates the mapped memory , writes the first message in it and duplicate the handle for the other process (the mapped memory is unnamed) the code is like this :
hMapped = CreateFileMappingA(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 1000, "my_shared_memory");
if (!hMapped)
{
cout << "[!] failed to create the shared mapped memory with error : " << GetLastError() << endl;
getchar();
}
char *shared_buffer = (char*)MapViewOfFile(hMapped, FILE_MAP_ALL_ACCESS, 0, 0, mapped_memory_size);
then the other process gets the handle , opens a view and retrieve the first buffer written
it then loops and checks every 6 seconds for the new writing operations and deal with it
I write from the first process as this :
std::lock_guard<std::mutex> lock(mtx);
RtlSecureZeroMemory(shared_buffer, mapped_memory_size);
shared_buffer[0] = 'n'; // it's a hint for the other process
memcpy(shared_buffer + 1, this->stuff.get_data().c_str(), this->stuff.get_data().size() + 1);
but the buffer isn't updated for the second process it is the first buffer
this is the code in the second process :
HANDLE shared_memory = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, "my_shared_memory");
char *shared_buffer = (char*)MapViewOfFile(shared_memory, FILE_MAP_ALL_ACCESS, 0, 0, 1000);
utils::command_line_parser cmd_parser;
cmd_parser.parse(std::string((char*)shared_buffer + 1));
if (!cmd_parser.valid()) { // I get that they are valid and I verify that
printf("failed to parse the arguments !");
return TRUE;
}
while(true)
{
Sleep(6000);
// CloseHandle(shared_memory);
// shared_memory = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, "my_shared_memory");
// shared_buffer = (char*)MapViewOfFile(shared_memory, FILE_MAP_ALL_ACCESS, 0, 0, 1000);
MessageBoxA(0, (char*)shared_buffer, "message", 0);
char res = shared_buffer[0];
switch (res)
{
case 'q' :
// do some stuff
case 'n' :
// do some stuff
break;
default:
break;
}