The Windows Processes and Threads 12








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:


  1. 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.
  2. 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.
  3. Environment variables.
  4. The current directory.
  5. 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.
  6. The error mode, as set by the SetErrorMode() function.
  7. The process affinity mask.
  8. The association with a job.


The child process does not inherit the following:


  1. Priority class.
  2. Handles returned by LocalAlloc(), GlobalAlloc(), HeapCreate(), and HeapAlloc().
  3. Pseudo handles, as in the handles returned by the GetCurrentProcess() or GetCurrentThread() function. These handles are valid only for the calling process.
  4. DLL module handles returned by the LoadLibrary() function.
  5. GDI or USER handles, such as HBITMAP or HMENU.


Inheriting Handles


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:


  1. 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.
  2. 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.


Inheriting Environment Variables


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.


Inheriting the Current Directory


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.




< Processes & Threads 11 | Win32 Process & Thread Programming | Win32 Programming | Processes & Threads 13 >