Page 1 of 1

[C] HTTP-Downloader

PostPosted:Sat Jan 12, 2019 11:32 am
by KarNak
Small/Clean HTTP Downloader (c)

Use this clean code in most my projects,

-Rex

Re: [C] HTTP-Downloader

PostPosted:Sun Jan 13, 2019 12:14 am
by Brock
Took a quick peek at the code, don't forget to close thread and process handles upon successful call returns. Only mentioning this because you mentioned the word "clean" twice and these are resource leaks.

Download.cpp

download_thread() --->
CloseHandle(pInfo->hThread);
CloseHandle(pInfo->hProcess);

download_DialogProc() --->
HANDLE hThread = CreateThread(..., ..);
if (hThread) {
CloseHandle(hThread);
}

For downloading to disk as a file directly (not memory) there exists the API URLDownloadToFile inside URLMon. Seems you likely have more of a *nix background so perhaps you weren't aware of it in the world of Windows. Would be an improvement if you added proxy and SSL support as well to it, these days it's almost required for anything done with remote sockets, and it's very simple to do with WinInet functionality.

Thanks for sharing, although I wonder if the more appropriate section of this forum is the Newbie area (can be more helpful to those learning there)

Re: [C] HTTP-Downloader

PostPosted:Sun Jan 13, 2019 1:25 pm
by KarNak
Thank you for the correction brock.

Re: [C] HTTP-Downloader

PostPosted:Mon Jan 14, 2019 4:46 pm
by mrfearless
A downside to using URLDownloadToFile, (that i found but perhaps i have overlooked something) is that it uses a cache, so if your downloading a file again it will just use the cached version. I found that problematic when creating an auto update library and fetching a version text file (or other files). If the file name is the same (as previously downloaded and cached), but content has changed, you will get the older version, before the cache is expired at some later time (i think).

If anyone has any advice/experience with additional net api calls to avoid this issue that will be cool. I ended up coding manually something myself that uses the wininet api calls to do something similar.

Re: [C] HTTP-Downloader

PostPosted:Tue Jan 15, 2019 1:42 pm
by Brock

Re: [C] HTTP-Downloader

PostPosted:Tue Mar 26, 2019 12:36 am
by AxtMueller
Using URLDownloadToFile is the best solution, it supports not only HTTP but also HTTPS.

Re: [C] HTTP-Downloader

PostPosted:Tue Aug 06, 2019 10:26 am
by VinayParde
Thank you for the information