I'm having a problem with InternetReadFile (for FTP downloads), and I'm wondering if anyone has any solutions. I need to know if the transfer was interrupted - say, either from RF errors or the FTP server was shutdown. Here is the code:
The function never returns 2. If I shut down the FTP server in the middle of the download, it simply returns 1 and leaves me with a half-downloaded file. InternetReadFile is blocking I believe so at the very least, I would hope that it would lock up totally if an error occurred, at which point I could detect that via some thread controls.
Alternatively is there a way for me to figure out what the size of the file on the server is, then check to see if the downloaded file size is the same?
Thanks,
ryanc
- Code:
DWORD ftp_get_file_binary(char * remote_file, char * local_file)
{
DWORD BytesWritten;
DWORD BytesRead = 1;
HANDLE fd_local;
HINTERNET fd_remote;
char buffer[100];
wchar_t remote[85];
wchar_t local[85];
mbstowcs(remote, remote_file, 85);
mbstowcs(local, local_file, 85);
// attempt to locate remote file on the server
fd_remote = FtpOpenFile(g_hConnect, remote, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 0);
if (!fd_remote)
return 0;
// open the local file
fd_local = CreateFile(local, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
// get file in 100-byte intervals
while (BytesRead)
{
if (!InternetReadFile(fd_remote, &buffer, 100, &BytesRead))
{
InternetCloseHandle(fd_remote);
CloseHandle(fd_local);
return 2;
}
WriteFile(fd_local, buffer, BytesRead, &BytesWritten, NULL);
}
InternetCloseHandle(fd_remote);
CloseHandle(fd_local);
return 1;
}
The function never returns 2. If I shut down the FTP server in the middle of the download, it simply returns 1 and leaves me with a half-downloaded file. InternetReadFile is blocking I believe so at the very least, I would hope that it would lock up totally if an error occurred, at which point I could detect that via some thread controls.
Alternatively is there a way for me to figure out what the size of the file on the server is, then check to see if the downloaded file size is the same?
Thanks,
ryanc