A child process can inherit several properties and resources from its parent process. You can also prevent a child process from inheriting properties from its parent process. The following can be inherited:
- Open handles returned by the CreateFile function. This includes handles to files, console input buffers, console screen buffers, named pipes, serial communication devices, and mailslots.
- Open handles to process, thread, mutex, event, semaphore, named-pipe, anonymous-pipe, and file-mapping objects. These are returned by the CreateProcess, CreateThread, CreateMutex, CreateEvent, CreateSemaphore, CreateNamedPipe, CreatePipe, and CreateFileMapping functions, respectively.
- Environment variables.
- The current directory.
- The console, unless the process is detached or a new console is created. A child console process can also inherits the parent's standard handles, as well as access to the input buffer and the active screen buffer.
- The error mode, as set by the SetErrorMode function.
- The process affinity mask.
- The association with a job.
The child process does not inherit the following:
- Priority class.
- Handles returned by LocalAlloc, GlobalAlloc, HeapCreate, and HeapAlloc.
- Pseudo handles, as in the handles returned by the GetCurrentProcess or GetCurrentThread function. These handles are valid only for the calling process.
- DLL module handles returned by the LoadLibrary function.
- GDI or USER handles, such as HBITMAP or HMENU.
A child process can inherit some of its parent's handles, but not inherit others. To cause a handle to be inherited, you must do two things:
- Specify that the handle is to be inherited when you create, open, or duplicate the handle. Creation functions typically use the bInheritHandle member of a SECURITY_ATTRIBUTES structure for this purpose. DuplicateHandle uses the bInheritHandles parameter.
- Specify that inheritable handles are to be inherited by setting the bInheritHandles parameter to TRUE when calling the CreateProcess function. Additionally, to inherit the standard input, standard output, and standard error handles, the dwFlags member of the STARTUPINFO structure must include STARTF_USESTDHANDLES.
An inherited handle refers to the same object in the child process as it does in the parent process. It also has the same value and access privileges. Therefore, when one process changes the state of the object, the change affects both processes. To use a handle, the child process must retrieve the handle value and "know" the object to which it refers. Usually, the parent process communicates this information to the child process through its command line, environment block, or some form of interprocess communication.
The DuplicateHandle function is useful if a process has an inheritable open handle that you do not want to be inherited by the child process. In this case, use DuplicateHandle to open a duplicate of the handle that cannot be inherited, then use the CloseHandle function to close the inheritable handle. You can also use the DuplicateHandle function to open an inheritable duplicate of a handle that cannot be inherited.
A child process inherits the environment variables of its parent process by default. However, CreateProcess enables the parent process to specify a different block of environment variables. For more information, see Environment Variables.
The GetCurrentDirectory function retrieves the current directory of the calling process. A child process inherits the current directory of its parent process by default. However, CreateProcess enables the parent process to specify a different current directory for the child process. To change the current directory of the calling process, use the SetCurrentDirectory function.