Not everything that sets GetLastError() to failure is an error. It is important to first recognize the errors using the return value of the function and examine GetLastError() to get more information about the error that occurred.
For existing mappings, CreateFileMapping documented to return a valid handle and set the value of GetLastError() to ERROR_ALREADY_EXISTS . In this case, the meaning of the error is informative: it is valid for checking it, if you are interested in whether there is a comparison before opening it, but this is not an error. You encounter an error by checking the return value for NULL. Otherwise, you just continue to use the pen.
PS If you want to make sure that the section exists before opening, you can use OpenFileMapping , which will fail for non-existent sections instead of creating a new one.
Anton Kovalenko
source share