Search code examples
c++windowswinhttp

Does handles from WinHTTP Async, needs to be closed?


Do you need to close the handles from WinHttpOpen(), WinHttpConnect() and WinHttpOpenRequest(), after you use them with async mode WinHttpOpen().

I suppose they need to be, but how is that done because everything is asynchronously done, I've seen that the last called event from the callback was WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE, but im not sure if this is always the case (called last).

So do I need to close the handles, and if yes how?


Solution

  • Yes, WinHTTP handles need to be closed. WinHttOpen() documentation:

    After the calling application has finished using the HINTERNET handle returned by WinHttpOpen, it must be closed using the WinHttpCloseHandle function.

    For asynchronous mode this can be done on the class destructor. In this case the handle can be a member variable so it's not lost. If you are using the main function, simply close the handle at the end.