I'm writing windows program with C and Visual Studio. I have to map a file than access it from it's 750th byte. I tried
pFile = (char *) MapViewOfFile(hMMap,FILE_MAP_ALL_ACCESS,0,(DWORD) 750,0)
open file with this but it returns error 1132.
ERROR_MAPPED_ALIGNMENT
1132 (0x46C)
The base address or the file offset specified does not have the proper alignment.
How can resolve this?
The documentation of MapViewOfFile is pretty clear that the offset has to be a multiple of the allocation granularity (which is normally 64KB I believe, but call GetSystemInfo
to get true actual value as the documentation states).
So since 750 is smaller than the allocation granularity you'll have to map the file from 0. If you really need your pointer to the 750th byte, then just increment the pointer
pFile = (char *) MapViewOfFile(hMMap,FILE_MAP_ALL_ACCESS,0,(DWORD) 0,0);
char* pBuffer = pFile + 750;
You'll need a second buffer because you will have to pass pFile to UnmapViewOfFile
I presume that you have to access the file from its 750th byte, not that you have to create the map from this offset. As you've discovered, you can't do this because the offset that you pass to MapViewOfFile
must be a multiple of the system's "allocation granularity" which you can get from GetSystemInfo
, but tends to be at least 4kbytes, IIRC.
You can create a view of the file which starts at byte 0 and just read from 750 bytes beyond the returned address.
The offset in the file has to be a multiple of 4k (so 750 doesn't work)
You'll probably be better off mapping the file from 0 and just offsetting 750 from the mapped address.