Using Handles to Kernel Resources
Many of the Windows API functions return handles. As can be seen from the earlier discussion of type casting, these are really just unsigned integers. However, they have a par-ticular purpose. Windows API calls that return handles have actually caused a resource to be created within the kernel space. The handle is just an index for that resource. When the application has finished with the resource, the call to CloseHandle() enables the kernel to free the associated kernel space resources.
Resources with handles can be shared between processes. Once a resource exists, other processes can open a handle to that resource or duplicate an existing handle to the resource. It is important to realize that the handle of a kernel resource makes sense only within the context of the process that has access to the resource. Passing the value of the handle to another process does not enable the other process to get access to the resource; the kernel needs to enable access to the resource and provide a new handle for the exist-ing resource in the new process.
Some functions do not return a handle. For these functions, there is no associated kernel resource; hence, it is not necessary to call CloseHandle() once the resource is no longer needed.