Differences Between Task Handles and Instance Handles (76676)



The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) 3.0
  • Microsoft Windows Software Development Kit (SDK) 3.1

This article was previously published under Q76676

SUMMARY

Windows creates two handles associated with each task running in the system. One handle is the instance handle, hInstance, which is passed to the WinMain function when the program starts execution. The other handle is the task handle, hTask, which is returned by GetCurrentTask().

MORE INFORMATION

The numerical values of hInstance and hTask are different. In some routines, hTask can be used in place of hInstance without any problems. In other routines, using hTask in place of hInstance causes incorrect behavior and may result in unrecoverable application errors (UAEs). Using hTask in place of hInstance is considered bad form and will probably cause problems when the application is run under future versions of Windows.

To retrieve the instance handle for the application currently running, use the following code fragment:
   hInstance = GetWindowWord(GetActiveWindow(), GWW_HINSTANCE);
				
To retrieve the task handle for the application currently running, use the following code fragment:
   hTask = GetCurrentTask();
				
The instance handle, hInstance, is used to identify the data associated with a particular instance of an application. The task handle, hTask, is the handle to a structure, called the task database (TDB), which contains information about the task (for example, its queue, module handle, and so forth). The instance handle and the PDB (program database), also known as the PSP (program segment prefix), are also stored in the task database. The GetCurrentPDB() function returns a handle to the current PDB.

Each instance of an application has both an instance handle and a task handle. Dynamic-link libraries (DLLs) are not tasks; therefore, they have only an instance handle and do not have a task handle.

Modification Type:MajorLast Reviewed:10/30/2003
Keywords:kb16bitonly KB76676