InternetCloseHandleFunction

Closes a WinINet Internet handle opened by InternetOpen, InternetConnect, InternetOpenUrl, or similar functions.

Syntax

BOOL InternetCloseHandle(
    HINTERNET hInternet
);

Parameters

ParameterTypeDescription
hInternet HINTERNET Handle to an Internet session, request, or FTP file.

Return value

Returns TRUE if the function succeeds; otherwise returns FALSE. Use GetLastError for extended error information.

Remarks

Note: For asynchronous operations, ensure that any pending callbacks are completed before closing the handle to avoid access violations.

Examples

#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet.lib")

int main() {
    HINTERNET hSession = InternetOpenA("MyApp", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if (!hSession) {
        printf("InternetOpen failed. Error: %lu\\n", GetLastError());
        return 1;
    }

    HINTERNET hConnect = InternetConnectA(hSession, "example.com",
                                          INTERNET_DEFAULT_HTTP_PORT,
                                          NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    if (!hConnect) {
        printf("InternetConnect failed. Error: %lu\\n", GetLastError());
        InternetCloseHandle(hSession);
        return 1;
    }

    // ... perform request ...

    // Close the child handle first
    InternetCloseHandle(hConnect);
    // Then close the session handle
    InternetCloseHandle(hSession);
    return 0;
}

Related