在两个进程之间命名共享内存

时间:2014-07-12 12:37:55

标签: c winapi file-mapping

我正在尝试使用文件映射在C中构建客户端/服务器,它仍处于开发阶段,但我在理解文件映射的工作原理方面遇到了一些麻烦。

我在我的服务器上创建一个struct的文件映射并在其上放置一些数据,然后我的客户端打开文件映射a读取数据ok。然后我的客户端写入数据供服务器读取,但服务器无法读取客户端数据,我无法理解为什么因为文件映射应该跨两个进程同步。我现在还没有使用事件,但我不认为这是必须的(是吗?)

她是我的代码。

服务器:

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);

客户端:

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

谢谢!

1 个答案:

答案 0 :(得分:2)

您的服务器在其读取循环中调用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替换为CHARWCHAR,而不是根据您的需要,并在两端与之保持一致。