A forum for reverse engineering, OS internals and malware analysis 

Ask your beginner questions here.
 #32433  by KarNak
 Sat Jan 12, 2019 11:32 am
Small/Clean HTTP Downloader (c)

Use this clean code in most my projects,

-Rex
Attachments
(3.94 KiB) Downloaded 25 times
 #32439  by Brock
 Sun Jan 13, 2019 12:14 am
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)
 #32441  by KarNak
 Sun Jan 13, 2019 1:25 pm
Thank you for the correction brock.
 #32448  by mrfearless
 Mon Jan 14, 2019 4:46 pm
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.