SERVER
#include <windows.h>
#include <string.h>
#include <iostream.h>
class student
{
public:
long ID;
char name[20];
};
void main()
{
HANDLE hMemShare;
LPCTSTR TestMemShare = "share";
student stu;
int stu_num = 30;
student *lpstu;
stu.ID = 01;
char* SecBug = "wangliwei";
strcpy(stu.name,SecBug);
hMemShare = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,sizeof(student),TestMemShare);
if(hMemShare == NULL)
{
cout<<"failed"<<endl;
}
lpstu = (student *)MapViewOfFile(hMemShare, FILE_MAP_WRITE,0,0,sizeof(student));
if(lpstu == NULL)
{
cout<<"failed"<<endl;
}
*lpstu = stu;
while(1){}
UnmapViewOfFile(lpstu);
}
CLIENT
#include <windows.h>
#include <iostream.h>
class student
{
public:
long ID;
char name[20];
};
void main()
{
HANDLE hMemShare;
LPCTSTR TestMemShare = "share";
student stu;
student *lpstu;
///char *lpTch;
stu.ID = 0;
char* tst = "小张";
strcpy(stu.name ,tst);
cout<<stu.name<<endl;
hMemShare = OpenFileMapping(FILE_MAP_READ,FALSE,TestMemShare);
if(hMemShare == NULL)
{
cout<<"failed"<<endl;
}
lpstu = (student *)MapViewOfFile(hMemShare, FILE_MAP_READ,0,0,sizeof(student));
if(lpstu == NULL)
{
cout<<"failed"<<endl;
}
stu = *lpstu;
cout<<stu.ID<<endl;
cout<<stu.name<<endl;
cout<<"successful"<<endl;
UnmapViewOfFile(lpstu);
}