bool DownloadFile(const char *lpszURL, const char *lpszFile)
{
bool result = false;
int kbpsCounter = 0;
int kbps;
DWORD ticks, bytesRead;
BYTE buffer[1024];
FILE *hDLFile;
HINTERNET hSession, hRequest;
/* Session öffnen */
hSession = InternetOpen("AppTitel", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hSession != NULL)
{
/*
URL öffnen */
hRequest = InternetOpenUrl(hSession, lpszURL, NULL, 0, INTERNET_FLAG_RELOAD, 0);
if (hRequest != NULL)
{
/* Download starten
Vllt checken ob Datei schon existiert? */
hDLFile = fopen(lpszFile, "wb");
if (hDLFile != NULL)
{
ticks = GetTickCount();
while (true)
{
/* Daten auslesen */
if (InternetReadFile(hRequest, buffer, 1024, &bytesRead) == false)
{
break;
}
/* kbps jede Sekunde berechnen */
kbpsCounter += bytesRead;
if (ticks <= (GetTickCount() - 1000))
{
kbps = kbpsCounter / 1024;
printf("%d kb/s\n", kbps); //kbps ausgeben
kbpsCounter = 0;
ticks = GetTickCount();
}
/* In Datei schreiben */
if (bytesRead != 0)
{
fwrite(buffer, bytesRead, 1, hDLFile);
}
else // Download beendet
{
result = true;
break;
}
}
fclose(hDLFile);
}
InternetCloseHandle(hRequest);
}
InternetCloseHandle(hSession);
}
return result;
}