OpenFileMapping

  HANDLE OpenFileMapping(dwDesiredAccess, bInheritHandle, lpName)    
  DWORD dwDesiredAccess; /* desired access mode */
  BOOL bInheritHandle; /* inherit flag */
  LPTSTR lpName; /* name of file mapping object */

The OpenFileMapping function opens a named file mapping object.

Parameters

dwDesiredAccess

Specifies the desired access to the file mapping object. For Win32/NT, this access is checked against any security descriptor on the target file mapping object.

bInheritHandle

Specifies whether or not the returned handle is to be inherited by a new process during process creation. A value of TRUE indicates that the new process will inherit the handle.

lpName

Points to a string that names the file mapping object to be opened. If there is an open handle to a file mapping object by this name, and the security descriptor on the mapping object does not disallow the desired access requested via dwDesiredAccess, the open operation will succeed.

Return Value

If the function is successful, the return value is an open handle to the specified file mapping object.

If the function fails, the return value is NULL. Use the GetLastError function to obtain extended error information.

Comments

The handle that OpenFileMapping returns may be used with any function that requires a handle to a file mapping object.

If the function is successful, the handle is granted access to the file mapping object only to the extent that it requested access through the dwDesiredAccess parameter.

Example

The following code fragment uses named file mapping objects as named shared memory:

//

// In creating process

//

hFileMapping = CreateFileMapping(

hFile,

NULL,

PAGE_READWRITE,

0,

0,

0,

"NameOfFileMappingObject"

);

assert(hFileMapping);

base = MapViewOfFile(

hFileMapping,

FILE_MAP_WRITE,

0,

0,

0

);

//

// base points to mapped view of file

//

assert(base);

//

// In sharing process

//

hFileMapping = OpenFileMapping(

FILE_MAP_READ,

FALSE,

"NameOfFileMappingObject"

);

assert(hFileMapping);

base = MapViewOfFile(

hFileMapping,

FILE_MAP_READ,

0,

0,

0

);

//

// base points to mapped view of file.

// Note that the value of base

// is not necessarily the same in both

// processes sharing the file

// mapping object.

//

assert(base);

See Also

CreateFileMapping