In response to a StackOverflow question[^], I developed an alternative for URLDownloadToFile
function; it is based on a generic HTTP client[^] written in C++.
CString FormatErrorMessage(DWORD dwLastError)
{
const int nLength = 0x1000;
TCHAR lpszBuffer[nLength] = { 0 };
if (::FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwLastError,
0,
(LPTSTR) &lpszBuffer,
nLength, NULL) == 0)
{
_stprintf_s(lpszBuffer, nLength, _T("Unknown INET error 0x%.4X\n"), dwLastError);
}
return lpszBuffer;
}
HRESULT DownloadURLToFile(CString strURLDownload, CString strFilePath)
{
CHTTPClient httpClient;
httpClient.EnableStatusCallback(TRUE);
httpClient.InitilizePostArguments();
httpClient.SetDownloadFile(strFilePath);
BOOL bResult = httpClient.Request(
strURLDownload,
CHTTPClient::RequestGetMethod,
__DEFAULT_AGENT_NAME);
DWORD dwLastError = httpClient.GetLastError();
OutputDebugString(_T("DownloadURLToFile: ") + FormatErrorMessage(dwLastError));
LPBYTE lpszHttpResponse = httpClient.QueryHTTPResponse();
return (bResult ? S_OK : E_FAIL);
}