15,471
社区成员
发帖
与我相关
我的任务
分享
int Test()
{
int iErr = 0;
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
SYSTEM_INFO sysInfo;
GetNativeSystemInfo(&sysInfo);
int BUF_SIZE = 102400 * sysInfo.dwPageSize;
HANDLE hMapFile = NULL;
LPVOID pBuf = NULL;
do
{
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // max. object size
BUF_SIZE, // buffer size
szName); // name of mapping object
if (hMapFile != NULL)
{
printf("CreateFileMapping %d (bytes) ok\n", BUF_SIZE);
}
else
{
iErr = GetLastError();
printf("Could not create file mapping object (%d).\n", iErr);
break;
}
pBuf = MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0, 0, 0);
if (pBuf == NULL)
{
iErr = GetLastError();
printf("Could not map view of file (%d).\n", iErr);
break;
}
for(LONG i=0; i<BUF_SIZE; i++)
{
((BYTE*)pBuf)[i] = (BYTE)i;
}
printf("Write done\n");
_getch();
}while(0);
if(pBuf != NULL)
{
UnmapViewOfFile(pBuf);
pBuf = NULL;
}
if(hMapFile != NULL)
{
CloseHandle(hMapFile);
hMapFile = NULL;
}
return iErr;
}