作者:你去过的地方叫远方 | 来源:互联网 | 2023-05-17 11:52
服务端:
#include
#include
#include
using namespace std;
#define MAP_PREFIX L"Local\\"
#define MAP_NAME L"SampleMap"
#define FULL_MAP_NAME MAP_PREFIX MAP_NAME
#define MAP_SIZE 65536
#define VIEW_OFFSIZE 0
#define VIEW_SIZE 1024
#define MESSAGE L"Message from the first process."
struct STUDENT
{
int age;
int sex;
int classes;
int parent;
};
typedef struct TEST
{
BOOL bOK;
int num;
char a[20]; //使用指针的时候在读取会出错
STUDENT student[2];
}*LPTEST;
int main()
{
HANDLE hMapFile = NULL;
/*PVOID pView = NULL;*/
LPTEST pTest = NULL;
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
MAP_SIZE,
FULL_MAP_NAME
);
if (hMapFile == NULL)
{
wprintf(L"CreateFileMapping failed");
goto Cleanup;
}
wprintf(L"The file mapping(%s) is created\n", FULL_MAP_NAME);
//pView = MapViewOfFile(
// hMapFile,
// FILE_MAP_ALL_ACCESS,
// 0,
// VIEW_OFFSIZE,
// VIEW_SIZE
//);
//if (pView == NULL)
//{
// wprintf(L"MapViewOfFile failed");
// goto Cleanup;
//}
pTest = (LPTEST)MapViewOfFile(
hMapFile,
FILE_MAP_ALL_ACCESS,
0,
VIEW_OFFSIZE,
/*VIEW_SIZE*/
sizeof(LPTEST)
);
if (pTest == NULL)
{
wprintf(L"MapViewOfFile failed");
goto Cleanup;
}
wprintf(L"The file view is mapped\n");
//PWSTR pszMessage = MESSAGE;
//DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage);
//memcpy_s(pView, VIEW_SIZE, pszMessage, cbMessage);
//wprintf(L"This message is written to the view:\n\"%s\"\n", pszMessage);
pTest->bOK = TRUE;
pTest->num = 3;
char* p = "abecdfa";
STUDENT* pStudent = new STUDENT[2];
pStudent[0].age = 13;
pStudent[0].classes = 3;
pStudent[0].parent = 2;
pStudent[0].sex = 0;
pStudent[1].age = 12;
pStudent[1].classes = 1;
pStudent[1].parent = 2;
pStudent[1].sex = 1;
memcpy(pTest->student, pStudent,sizeof(STUDENT)*2);
strcpy(pTest->a, p);
cout <bOK < cout <num < cout <a < cout <student[0].age <<"," <student[0].classes <<","<student[0].parent <<","<student[0].sex < cout <student[1].age <<"," <student[1].classes <<","<student[1].parent <<","<student[1].sex < wprintf(L"Press ENTER to clean up resources and quit");
getchar();
Cleanup:
if (hMapFile)
{
//if (pView)
//{
// UnmapViewOfFile(pView);
// pView = NULL;
//}
if (pTest)
{
UnmapViewOfFile(pTest);
pTest = NULL;
}
CloseHandle(hMapFile);
hMapFile = NULL;
}
return 0;
}
客户端:
#include
#include
#include
using namespace std;
#define MAP_PREFIX L"Local\\"
#define MAP_NAME L"SampleMap"
#define FULL_MAP_NAME MAP_PREFIX MAP_NAME
#define VIEW_OFFSET 0
#define VIEW_SIZE 1024
struct STUDENT
{
int age;
int sex;
int classes;
int parent;
};
typedef struct TEST
{
BOOL bOK;
int num;
char a[20];
STUDENT student[2];
}*LPTEST;
int main()
{
HANDLE hMapFile = NULL;
/*PVOID pView = NULL;*/
LPTEST pTest = NULL;
hMapFile = OpenFileMapping(
FILE_MAP_READ | FILE_MAP_WRITE,
FALSE,
FULL_MAP_NAME
);
if (hMapFile == NULL)
{
wprintf(L"OpenFileMapping failed");
goto Cleanup;
}
wprintf(L"The file mapping(%s) is opened\n", FULL_MAP_NAME);
//pView = MapViewOfFile(
// hMapFile,
// FILE_MAP_READ,
// 0,
// VIEW_OFFSET,
// VIEW_SIZE
//);
//if (pView == NULL)
//{
// wprintf(L"MapViewOfFile failed");
// goto Cleanup;
//}
pTest = (LPTEST)MapViewOfFile(
hMapFile,
FILE_MAP_READ|FILE_MAP_WRITE,
0,
VIEW_OFFSET,
/*VIEW_SIZE*/
sizeof(LPTEST)
);
if (pTest == NULL)
{
wprintf(L"MapViewOfFile failed");
goto Cleanup;
}
pTest->bOK = FALSE;
wprintf(L"The file view is mapped\n");
//wprintf(L"Read from the file mapping:\n\"%s\"\n", (PWSTR)pView);
cout <bOK < cout <num < cout <a < cout <student[0].age <<"," <student[0].classes <<"," <student[0].parent <<"," <student[0].sex < cout <student[1].age <<"," <student[1].classes <<"," <student[1].parent <<"," <student[1].sex < wprintf(L"Press ENTER to clean up resources and quit");
getchar();
Cleanup:
if (hMapFile)
{
//if (pView)
//{
// UnmapViewOfFile(pView);
// pView = NULL;
//}
if (pTest)
{
UnmapViewOfFile(pTest);
pTest = NULL;
}
CloseHandle(hMapFile);
hMapFile = NULL;
}
return 0;
}