#include #include #include #pragma comment(lib, "wininet.lib") #define BUFFER_SIZE 1024 void ResumeDownload(const char* url, const char* filePath, DWORD startPosition); int main() { const char* url = "https://www5e.biglobe.ne.jp/~develop/img/hero-img.png"; const char* filePath = "downloaded_file.png"; DWORD startPosition = 0; // Set the starting position to the byte you want to resume from ResumeDownload(url, filePath, startPosition); return 0; } void ResumeDownload(const char* url, const char* filePath, DWORD startPosition) { HINTERNET hInternet, hConnect; DWORD bytesRead, totalBytesRead = startPosition; hInternet = InternetOpen(L"Download", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); if (hInternet == NULL) { // Handle error return; } // Use INTERNET_FLAG_RELOAD to enable resuming hConnect = InternetOpenUrlA(hInternet, url, NULL, 0, INTERNET_FLAG_RELOAD, 0); if (hConnect == NULL) { // Handle error InternetCloseHandle(hInternet); return; } // Set the starting position for resuming the download if (startPosition > 0) { LARGE_INTEGER li; // ダウンロードを再開するための開始位置を設定する li.QuadPart = startPosition; // 指定されたファイルハンドル(hConnect)に関連付けられたファイルの読み取りまたは書き込み位置を、 // 指定された新しい位置に移動する SetFilePointerEx(hConnect, li, NULL, FILE_BEGIN); } // ハンドルを使用して、後続のファイルへの書き込み処理が行う HANDLE hFile = CreateFileA(filePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { // Handle error InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return; } char buffer[BUFFER_SIZE]; while (InternetReadFile(hConnect, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0) { WriteFile(hFile, buffer, bytesRead, NULL, NULL); totalBytesRead += bytesRead; } CloseHandle(hFile); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); printf("Download completed. Total bytes read: %u\n", totalBytesRead); }