Handles created and used by the WinInet functions are called HINTERNETs. These HINTERNET handles returned by the WinInet functions in Windows CE control only Internet functions. They are not native system handles. Therefore, only an Internet function works with its corresponding Internet handle. HINTERNET handles cannot be used with functions such as ReadFile or CloseHandle. Similarly, native system handles cannot be used with the WinInet functions. For example, a handle returned by CreateFile cannot be passed to InternetReadFile.
HINTERNET handles are maintained in a tree hierarchy in which a higher-level function must be called before a dependent function is called. The handle returned by the InternetOpen function is the root node. Handles returned by the InternetConnect function occupy the next level. Handles returned by the FtpOpenFile, FtpFindFirstFile, and HttpOpenRequest functions are the leaf nodes.
The following illustration shows the hierarchy of the HINTERNET handles. Each box represents a WinInet function that returns an HINTERNET handle.
At the top level is InternetOpen, which creates the root HINTERNET. The next level contains InternetOpenUrl and InternetConnect. The functions that use the HINTERNET handle returned by InternetConnect make up the last level.
All HINTERNET handles can be closed by using InternetCloseHandle. Client applications must close all HINTERNET handles derived from the HINTERNET handle to be closed before calling InternetCloseHandle.
The following code example shows the handle hierarchy for the WinInet functions.
HINTERNET hRootHandle, hOpenUrlHandle;
hRootHandle = InternetOpen(
TEXT("Example"),
INTERNET_OPEN_TYPE_DIRECT,
NULL,
NULL,
0);
hOpenUrlHandle = InternetOpenUrl(
hRootHandle,
TEXT("http://www.server.com/default.htm"),
NULL,
0,
INTERNET_FLAG_RAW_DATA,
0);
// Close the handle created by InternetOpenUrl, so that the
// InternetOpen handle can be closed.
InternetCloseHandle(hOpenUrlHandle);
// Close the handle created by InternetOpen.
InternetCloseHandle(hRootHandle);
Note Handle values are recycled quickly. Therefore, if a handle is closed and a new handle is generated immediately, the new handle can have the same value as the handle just closed.