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,
NULL,
PAGE_READWRITE,
0,
sizeof(HBACKUPSERVICE),
_T("MyService"));
if (hMapFile == NULL){
_tprintf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
return *pBuf;
}
pBuf = (PHBACKUPSERVICE)MapViewOfFile(
hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
sizeof(HBACKUPSERVICE));
if (pBuf == NULL){
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
return *pBuf;
}
pBuf->hMapFile = hMapFile;
pBuf->dwServerProcessID = GetCurrentProcessId();
do{
_tprintf(_T("\nServer: Waiting for work."));
pBuf = (PHBACKUPSERVICE)MapViewOfFile(
_BackupService.hMapFile,
FILE_MAP_ALL_ACCESS,
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);
_tprintf(_T("Work from client: %s"), pBuf->szWork);
Customer:
HBACKUPSERVICE _BackupService;
HANDLE hMapFile;
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
FALSE,
_T("MyService"));
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not open file mapping object (%d).\n"),
GetLastError());
}
BackupService= (PHBACKUPSERVICE)MapViewOfFile(
hMapFile,
FILE_MAP_ALL_ACCESS,
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!!!!!!!!!!"));
Thank!