Question

My file(sample.txt) has 10 chars. I opened the file in write mode, have done a createfilemapping("mymapping"). Then I Hold the process by getchar. Then opened another process which does the below

openfilemapping("mymapping"),
char*k =  (char*)mapviewoffile 

Now I can access the value of sample.txt and change it via k. However, how to insert/append a strip of another 10 characters into the file.

Was it helpful?

Solution

Shared-memory mappings are fixed in size upon creation. You will need to call CreateFileMapping again with a larger mapping size. This will automatically extend the file, at which point you can MapViewOfFile again, and write in whatever data you want. Note that you will need to change the name of the file mapping, unless you first close all handles and mappings to it so the file mapping is destroyed.

As an aside, it's unusual (but not illegal, of course) to use named file mappings backed by disk files. Generally, if you're mapping a real file, you'd pass NULL to the lpName parameter of CreateFileMapping. You'd pass in a name when you want to create a shared memory mapping with no backing file - that is, hFile would be NULL and lpName be the name of the shared memory segment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top