Search code examples
c++windowsprocessmsdnfile-mapping

Shared data Filemapping


Hello I want to share an object from a class from a P1 process to a P2.exe process using the filemapping from microsoft: https://msdn.microsoft.com/fr-fr/library/windows/desktop/aa366537(v=vs.85).aspx.

P1 write the datas and P2 read them. I want to share all the datas but it doesn't work.

P1:

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
#include <string>
using namespace std;
TCHAR szName[] = TEXT("Global\\MyFileMappingObject");
class Object
{
public:
    string nameWindow;
    string nameLogFile;
    HWND hwndWindow;
    HANDLE histFile;
    WIN32_FIND_DATA dataFile;
};// members to read
int _tmain()
{
    Object add;

    HANDLE hMapFile;
    add.nameLogFile= "Logfil";
    add.nameWindow = "Windowstest";
    Table *bufData;
    hMapFile = CreateFileMapping(
        INVALID_HANDLE_VALUE,    // use paging file
        NULL,                    // default security
        PAGE_READWRITE,          // read/write access
        0,                       // maximum object size (high-order DWORD)
        sizeof(Table),                // maximum object size (low-order DWORD)
        szName);                 // name of mapping object

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).\n"),
            GetLastError());
        return false;
    }
    bufData = (Object*)MapViewOfFile(hMapFile,   // handle to map object
        FILE_MAP_ALL_ACCESS, // read/write permission
        0,
        0,
        sizeof(Table));
    if (bufData == NULL)
    {
        _tprintf(TEXT("Could not map view of file (%d).\n"),
            GetLastError());

        CloseHandle(hMapFile);

        return false;
    }
    CopyMemory((PVOID)bufData, &add, sizeof(Table));
        cout << "Mise en memoire " << endl;

    _getch();

    UnmapViewOfFile(bufData);

    CloseHandle(hMapFile);

}

P2:

#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <string>
using namespace std;
#pragma comment(lib, "user32.lib")
class Object
{
public:
    string nameWindow;
    string nameLogFile;
    HWND hwndWindow;
    HANDLE histFile;
    WIN32_FIND_DATA dataFile;
};// members to write
TCHAR szName[] = TEXT("Global\\MyFileMappingObject");

int _tmain()
{
    HANDLE hMapFile;
    LPCTSTR pBuf;
    Table *bufData=new Table;
    hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS,   // read/write access
        FALSE,                 // do not inherit the name
        szName);               // name of mapping object

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not open file mapping object (%d).\n"),
            GetLastError());
        return 1;
    }

    bufData = (Object*)MapViewOfFile(hMapFile, // handle to map object
        FILE_MAP_ALL_ACCESS,  // read/write permission
        0,
        0,
        sizeof(Object));

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

        CloseHandle(hMapFile);

        return 1;
    }
    cout << "DATA" << endl;
    cout << "Log:" << bufData->nameLogFile<<endl;
    cout << "Window:" << bufData->nameWindow << endl;


    UnmapViewOfFile(bufData);

    CloseHandle(hMapFile);
    system("pause");
    return 0;
}

So I try with only 2 strings for the tests:add.nameLogFile= "Logfil" and add.nameWindow = "Windowstest"; size of both. It works the P2 reads well the two strings.

But when I had one character (add.nameWindow = "Windowstest+" size of 28 both) it doesnt'work anymore. It may be a size or memory error with this:

TCHAR szName[] = TEXT("Global\MyFileMappingObject");

So my questions are:

How increase the data size of the file or buffer? How can I pass all members of my object class in the filemapping (the two strings and HWND hwndWindow; HANDLE histFile; WIN32_FIND_DATA dataFile) Is there an other method two share datas from an object between two process.

PS: I read many forum and I don't find my answer or no understand.

Thanks for all.


Solution

  • This doesn't really work.

    When a std::string stores a short string, the characters might be stored inside the std::string object. However, when the string grows larger, additional space will be allocated on the heap - outside of the string object.

    This heap space is not written to the file and then it doesn't work anymore.

    Unfortunately you will have to rethink this from the beginning.