InternetCloseHandle Function

Closes an HINTERNET handle.

BOOL InternetCloseHandle( HINTERNET hInternet );

Parameters

Parameter Type Description
hInternet HINTERNET The handle to close. This handle is obtained from a previous call to InternetOpen, InternetOpenUrl, InternetConnect, or HttpOpenRequest.

Return Value

Returns TRUE if the handle was successfully closed. Returns FALSE if the handle could not be closed. To get a specific error message, call GetLastError.

Remarks

The InternetCloseHandle function closes a session, connection, request, or data transfer handle obtained by a previous call to the WinINet API. It is crucial to close all WinINet handles when they are no longer needed to free up system resources.

Important

Always call InternetCloseHandle on all returned handles, including those that are returned from a failed operation. Failure to close handles can lead to memory leaks and other resource exhaustion issues.

When a handle is closed, all associated resources are released. For example, closing an HINTERNET handle obtained from InternetOpenUrl will cancel any ongoing data transfer and free the buffer associated with that request.

Example


#include <windows.h>
#include <wininet.h>

// ...

HINTERNET hOpen = NULL;
HINTERNET hUrlFile = NULL;

// Initialize WinINet
hOpen = InternetOpen(L"MyUserAgent", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hOpen != NULL) {
    // Open a URL
    hUrlFile = InternetOpenUrl(hOpen, L"http://www.example.com", NULL, 0, 0, 0);
    if (hUrlFile == NULL) {
        // Handle error
    }
} else {
    // Handle error
}

// ... perform operations with hUrlFile ...

// Close the HINTERNET handles
if (hUrlFile != NULL) {
    InternetCloseHandle(hUrlFile); // Close the URL handle first
    hUrlFile = NULL;
}
if (hOpen != NULL) {
    InternetCloseHandle(hOpen);   // Then close the session handle
    hOpen = NULL;
}
                

Requirements

Attribute Value
Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Header wininet.h
Library Wininet.lib
DLL wininet.dll