Named Shared Memory Between Two Processes

I am trying to create a client / server in C using file association, it is still developing very early, but I have some problems understanding how file association works.

I create a file mapping structure on my server and put some data on it, and then my client opens the file mapping and reads the data in order. Then my client writes the data to be read to the server, but the server cannot read the client data, and I cannot understand why, since the file association must be synchronized in both processes. I still do not use the event at this stage, but I do not think that they are necessary for this to work (are they?)

I have her code.

Server:

struct _HBACKUPSERVICE{
    DWORD           dwServerProcessID;
    TCHAR           szWork[MAX_PATH];
    HANDLE          hMapFile;
};

PHBACKUPSERVICE pBuf = NULL;
HANDLE hMapFile;

hMapFile = CreateFileMapping(
    INVALID_HANDLE_VALUE,    // use paging file
    NULL,                    // default security
    PAGE_READWRITE,          // read/write access
    0,                       // maximum object size (high-order DWORD)
    sizeof(HBACKUPSERVICE),  // maximum object size (low-order DWORD)
    _T("MyService"));        // name of mapping object

if (/*phBackupService->*/hMapFile == NULL){
    _tprintf(TEXT("Could not create file mapping object (%d).\n"),
        GetLastError());
    return *pBuf;
}

pBuf = (PHBACKUPSERVICE)MapViewOfFile(
    hMapFile,               // handle to map object
    FILE_MAP_ALL_ACCESS,    // read/write permission
    0,
    0,
    sizeof(HBACKUPSERVICE));

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


// Populate backup service structure
pBuf->hMapFile = hMapFile;
pBuf->dwServerProcessID = GetCurrentProcessId();

// Wait for client
do{
    _tprintf(_T("\nServer: Waiting for work."));
    pBuf = (PHBACKUPSERVICE)MapViewOfFile(
        _BackupService.hMapFile,    // handle to map object
        FILE_MAP_ALL_ACCESS,        // read/write permission
        0,
        0,
        sizeof(HBACKUPSERVICE));
    if (StringCbLength(pBuf->szWork, 1 * sizeof(TCHAR), NULL) == S_OK){ Sleep(500); }
} while (StringCbLength(pBuf->szWork, 1 * sizeof(TCHAR), NULL) == S_OK); // ERROR: pBuf->szWork is always empty...

_tprintf(_T("Work from client: %s"), pBuf->szWork);

Customer:

HBACKUPSERVICE _BackupService;
HANDLE hMapFile;

hMapFile = OpenFileMapping(
    FILE_MAP_ALL_ACCESS,   // read/write access
    FALSE,                 // do not inherit the name
    _T("MyService"));          // name of mapping object

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

BackupService= (PHBACKUPSERVICE)MapViewOfFile(
    hMapFile,               // handle to map object
    FILE_MAP_ALL_ACCESS,    // read/write permission
    0,
    0,
    sizeof(HBACKUPSERVICE));

_tprintf(_T("Server process id: %d"), _BackupService.dwServerProcessID);
_tprintf(_T("send work to server"));
StringCchCopy(_BackupService.szWork, STRSAFE_MAX_CCH, _T("Do work for me!!!!!!!!!!")); //ERROR: the server never sees this

Thank!

+4
1

MapViewOfFile() , , . , . . pBuf, MapViewOfFile(), . .

, HBACKUPSERVICE . .

:

:

typedef struct _HBACKUPSERVICE {
    DWORD           dwServerProcessID;
    TCHAR           szWork[MAX_PATH];
    HANDLE          hMapFile;
} HBACKUPSERVICE, *PHBACKUPSERVICE;

:

PHBACKUPSERVICE pBuf = NULL;
HANDLE hMapFile;

hMapFile = CreateFileMapping(
    INVALID_HANDLE_VALUE,    // use paging file
    NULL,                    // default security
    PAGE_READWRITE,          // read/write access
    0,                       // maximum object size (high-order DWORD)
    sizeof(HBACKUPSERVICE),  // maximum object size (low-order DWORD)
    _T("MyService"));        // name of mapping object

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

pBuf = (PHBACKUPSERVICE)MapViewOfFile(
    hMapFile,               // handle to map object
    FILE_MAP_ALL_ACCESS,    // read/write permission
    0,
    0,
    sizeof(HBACKUPSERVICE));

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

// Populate backup service structure
pBuf->hMapFile = hMapFile;
pBuf->dwServerProcessID = GetCurrentProcessId();
ZeroMemory(pBuf->szWork, sizeof(pBuf->szWork));

// Wait for client
_tprintf(_T("\nServer: Waiting for work."));
while (pBuf->szWork[0] == 0){ Sleep(500); }

_tprintf(_T("Work from client: %s"), pBuf->szWork);

:

PHBACKUPSERVICE BackupService = NULL;
HANDLE hMapFile;

hMapFile = OpenFileMapping(
    FILE_MAP_ALL_ACCESS,   // read/write access
    FALSE,                 // do not inherit the name
    _T("MyService"));          // name of mapping object

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

BackupService = (PHBACKUPSERVICE)MapViewOfFile(
    hMapFile,               // handle to map object
    FILE_MAP_ALL_ACCESS,    // read/write permission
    0,
    0,
    sizeof(HBACKUPSERVICE));

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

_tprintf(_T("Server process id: %d"), BackupService->dwServerProcessID);
_tprintf(_T("send work to server"));
StringCchCopy(BackupService->szWork, MAX_PATH, _T("Do work for me!!!!!!!!!!"));

, TCHAR . , , ANSI UNICODE. szWork , , HBACKUPSERVICE. TCHAR CHAR WCHAR , , .

+2

Source: https://habr.com/ru/post/1548178/


All Articles