WinHttpSetStatusCallback function (winhttp.h)
The WinHttpSetStatusCallback function sets up a callback function that WinHTTP can call as progress is made during an operation.
Syntax
WINHTTPAPI WINHTTP_STATUS_CALLBACK WinHttpSetStatusCallback(
[in] HINTERNET hInternet,
[in] WINHTTP_STATUS_CALLBACK lpfnInternetCallback,
[in] DWORD dwNotificationFlags,
[in] DWORD_PTR dwReserved
);
Parameters
[in] hInternet
HINTERNET handle for which the callback is to be set.
[in] lpfnInternetCallback
Pointer to the callback function to call when progress is made. Set this to NULL to remove the existing callback function. For more information about the callback function, see WINHTTP_STATUS_CALLBACK.
[in] dwNotificationFlags
Unsigned long integer value that specifies flags to indicate which events activate the callback function.
The possible values are as follows.
Value | Meaning |
---|---|
|
Activates upon any completion notification. This flag specifies that all notifications required for read or write operations are used. See WINHTTP_STATUS_CALLBACK for a list of completions. |
|
Activates upon any status change notification including completions. See WINHTTP_STATUS_CALLBACK for a list of notifications. |
|
Activates upon beginning and completing name resolution. |
|
Activates upon beginning and completing connection to the server. |
|
Activates when detecting the proxy server. |
|
Activates when completing a query for data. |
|
Activates when the response headers are available for retrieval. |
|
Activates upon completion of a data-read operation. |
|
Activates when an asynchronous error occurs. |
|
Activates upon beginning and completing the sending of a request header with WinHttpSendRequest. |
|
Activates when a request header has been sent with WinHttpSendRequest. |
|
Activates upon completion of a data-post operation. |
|
Activates upon beginning and completing the receipt of a resource from the HTTP server. |
|
Activates when beginning and completing the closing of an HTTP connection. |
|
Activates when an HINTERNET handle is created or closed. |
|
Activates when the request is redirected. |
|
Activates when receiving an intermediate (100 level) status code message from the server. |
|
Activates upon a secure connection failure. |
[in] dwReserved
This parameter is reserved and must be NULL.
Return value
If successful, returns a pointer to the previously defined status callback function or NULL if there was no previously defined status callback function. Returns WINHTTP_INVALID_STATUS_CALLBACK if the callback function could not be installed. For extended error information, call GetLastError. Among the error codes returned are the following.
Error Code | Description |
---|---|
|
The type of handle supplied is incorrect for this operation. |
|
An internal error has occurred. |
|
Not enough memory was available to complete the requested operation. (Windows error code) |
Remarks
If you set the callback on the session handle before creating the request handle, the request handle inherits the callback function pointer from its parent session.
Even when WinHTTP is used in asynchronous mode (that is, when WINHTTP_FLAG_ASYNC has been set in WinHttpOpen), this function operates synchronously. The return value indicates success or failure. To get extended error information, call GetLastError.
Both synchronous and asynchronous functions use the callback function to indicate the progress of the request, such as resolving a name, connecting to a server, and so on. The callback function is required for an asynchronous operation.
A callback function can be set on any handle and is inherited by derived handles. A callback function can be changed using WinHttpSetStatusCallback, provided there are no pending requests that need to use the previous callback value. However, changing the callback function on a handle does not change the callbacks on derived handles, such as that returned by WinHttpConnect. You must change the callback function at each level.
Many WinHTTP functions perform several operations on the network. Each operation can take time to complete and each can fail.
After initiating the WinHttpSetStatusCallback function, the callback function can be accessed from within WinHTTP for monitoring time-intensive network operations.
At the end of asynchronous processing, the application may set the callback function to NULL. This prevents the client application from receiving additional notifications.
The following code snippet shows the recommended method for setting the callback function to NULL.
WinHttpSetStatusCallback( hOpen,
NULL,
WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
NULL );
Note, however, that WinHTTP does not synchronize WinHttpSetStatusCallback with worker threads. If a callback originating in another thread is in progress when an application calls WinHttpSetStatusCallback, the application still receives a callback notification even after WinHttpSetStatusCallback successfully sets the callback function to NULL and returns.
Examples
The following example shows how to install a callback function for asynchronous WinHTTP functions. The example assumes that a WINHTTP_STATUS_CALLBACK function named "AsyncCallback( )" has been previously implemented:
// Use WinHttpOpen to obtain an HINTERNET handle.
HINTERNET hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession)
{
// Install the status callback function.
WINHTTP_STATUS_CALLBACK isCallback = WinHttpSetStatusCallback( hSession,
(WINHTTP_STATUS_CALLBACK)AsyncCallback,
WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
NULL);
// Place additional code here.
// When finished, release the HINTERNET handle.
WinHttpCloseHandle(hSession);
}
else
{
printf("Error %u in WinHttpOpen.\n", GetLastError());
}
Requirements
Requirement | Value |
---|---|
Minimum supported client | Windows XP, Windows 2000 Professional with SP3 [desktop apps only] |
Minimum supported server | Windows Server 2003, Windows 2000 Server with SP3 [desktop apps only] |
Target Platform | Windows |
Header | winhttp.h |
Library | Winhttp.lib |
DLL | Winhttp.dll |
Redistributable | WinHTTP 5.0 and Internet Explorer 5.01 or later on Windows XP and Windows 2000. |