0

So I have a first process (process1.cpp) which takes the list of the current processes in the system and should write them in the shared memory alongside their PID and PPID:

addr = (LPTSTR)MapViewOfFile(hMapFile,
    FILE_MAP_ALL_ACCESS,
    0,
    0,
    256);

And I make the writing like this:

int j = 0;
do
    {
        if (pe32.cntThreads < 3)
        {
            char currentProcessExeName = (char)pe32.szExeFile;
            char currentProcessID = (char)pe32.th32ProcessID;
            char currentProcessParentID = (char)pe32.th32ParentProcessID;

            addr = (LPCTSTR)currentProcessExeName;
            addr = (LPCTSTR)currentProcessID;
            addr = (LPCTSTR)currentProcessParentID;

            j = j + 3;
        }
    } while (Process32Next(hProcessSnap, &pe32));

Then, in the second process (process2.cpp) I want to read that array. The problem is that I get either 'can't read memory' error, or null values.

enter image description here

Here's how I make the read in the second process:

    addr = (LPTSTR)MapViewOfFile(hMapFile,
            FILE_MAP_ALL_ACCESS,
            0,
            0,
            BUF_SIZE);

if (addr == NULL)
{
    _tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError());
    CloseHandle(hMapFile);

    return 1;
}


for (int i = 0; i < sizeof(addr); i = i + 3)
{
    printf("\nProcess: %S [%d] [%d] \n", addr[i], addr[i + 1], addr[i + 2]);
}

Any fix for this ?

P.S.: I've posted as less code as possible not to load the page too much. If needed, I will post more.

miTzuliK
  • 93
  • 1
  • 1
  • 11
  • You are memory-mapping a file, not creating shared memory. – Some programmer dude Oct 23 '16 at 11:26
  • Yeah, you're right, I'm sorry. Forgot to edit it. I will do it now. – miTzuliK Oct 23 '16 at 11:27
  • Also, your way to get the process information is flawed. If `char` is signed you will only get seemingly weird values in the `-128` to `127` range. You are also *reassigning* the variable `addr` three times in the loop instead of writing to the memory mapping of the file. – Some programmer dude Oct 23 '16 at 11:31
  • Thank you for this as well. But how do I correctly write an array to the memory mapping of a file ? – miTzuliK Oct 23 '16 at 11:34
  • Create a structure to contain the information you need to share, with the correct data-types for the member fields. Then look at the shared memory as an array of this structure. – Some programmer dude Oct 23 '16 at 11:35
  • Thank you for your advices. It worked, but I still have one small issue. If you can review my post below, I'd appreciate. – miTzuliK Oct 23 '16 at 13:15

0 Answers0