Created
March 7, 2018 10:40
-
-
Save vk2gpu/48f9aa7050655cd2fd2a3f85dfab7664 to your computer and use it in GitHub Desktop.
Win32 snippet for reading a file from the interwebs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const char* url = "https://downloads.sourceforge.net/project/ispcmirror/v1.9.1/ispc-v1.9.1-windows-vs2015.zip"; | |
HINTERNET hInternet = ::InternetOpenA("httpRequest", INTERNET_OPEN_TYPE_PRECONFIG, nullptr, nullptr, 0); | |
if(::InternetGoOnlineA(url, nullptr, 0)) | |
{ | |
HINTERNET hFileHandle = ::InternetOpenUrlA(hInternet, url, nullptr, 0, 0, 0); | |
DWORD requestStatusSize = sizeof(requestStatusSize); | |
DWORD requestStatus = 0; | |
::HttpQueryInfoA(hFileHandle, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &requestStatus, &requestStatusSize, nullptr); | |
if(requestStatus == HTTP_STATUS_OK) | |
{ | |
DWORD requestLengthSize = sizeof(requestStatusSize); | |
DWORD requestLength = 0; | |
::HttpQueryInfoA(hFileHandle, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &requestLength, &requestLengthSize, nullptr); | |
DWORD chunkSize = 32 * 1024; | |
Core::Vector<u8> internetData(requestLength); | |
DWORD totalRead = 0; | |
DWORD thisRead = 0; | |
while(::InternetReadFile(hFileHandle, &internetData[totalRead], chunkSize, &thisRead) && thisRead > 0 && totalRead < requestLength) | |
{ | |
totalRead += thisRead; | |
if((i32)totalRead > (internetData.size() / 2)) | |
{ | |
internetData.resize(internetData.size() * 2); | |
} | |
Core::Log("Total downloaded... %ukb (%.1f%%)\n", (totalRead / 1024), ((f32)totalRead / (f32)requestLength) * 100.0f); | |
} | |
} | |
::InternetCloseHandle(hFileHandle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment