Article View: borland.public.cppbuilder.internet.socket
Article #7Re: FTP file download for Indy 9
From: ANTHONY WEST
Date: Fri, 18 Jul 2008 07:07
Date: Fri, 18 Jul 2008 07:07
170 lines
7481 bytes
7481 bytes
Thanks guys for all of your help. I was able to make it work
with the MSDN articles that Bob pointed me to. I have pasted
a basic example of what I am now able to do and it works
great. This is what I had to do to make it work with BCB 4:
- After including the wininet.h and some FTP code, I received the
"Unresolved external symbol referenced from module" error. I
could not find a wininet.lib or .obj that I could add to the
project to remove this error. I searched my hard drive and found
the wininet.dll in my windows/system32 folder. I created a lib at
the command prompt using the implib wininet.lib wininet.dll command.
I added this new wininet.lib to the project and it compiled fine. (I
also had to do this with urlmon.lib.)
- Then I added the function FtpGetFileSize to my code and received
a compiler error saying there was no such function. I examined the
wininet.lib file and found it in there. Then I examined the wininet.h
file and it was not in there. This function must be a newer one that
was not available when BCB 4 was released. So, I modified the
wininet.h file and added the FtpGetFileSize function to match the
function call prototype on MSDN. Again it compiled fine. (I also
had to modify the urlmon.h)
Whew.....what a days work this has been. Maybe this info will be
helpful to us programmers still using outdated compilers lol.
Here is my code:
#include "urlmon.h"//for http - modified for BCB 4
#include "wininet.h"//for ftp - modified for BCB 4
int i, latestVersion, percent;
unsigned long int fileSizeBytes, totalBytesRead, bytesRead;
unsigned char buffer[9999];
AnsiString versionFileName, updateURLLink, thisVersion;
AnsiString updateFTPFileName, updateDownloadName;
ifstream in;
ofstream out;
HINTERNET hInternetSession, hFTPSession, hFTPOpenFile;
Screen->Cursor = crAppStart;
ShowStatusBarMessage("Connecting to update server...");
updateURLLink =
http://www.aswsoftware.com/products/bibleart/
"currentVersion.upd";
versionFileName = MainMenuForm->DirLbl->Caption +
"LatestVersion.upd";
updateFTPFileName = "htdocs/temp/100_5730.JPG";
updateDownloadName = "test.JPG";
if(URLDownloadToFile(0,updateURLLink.c_str(),
versionFileName.c_str(),0,0))
{
MessageDlg("Unable to connect to update server.",
mtError, TMsgDlgButtons() << mbOK, 0);
ShowStatusBarMessage("Failed to obtain update information.");
}
else
{
ShowStatusBarMessage("Getting latest version information from
server...");
in.open(versionFileName.c_str());
if (in.fail())
{
MessageDlg("Error obtaining update information.", mtError,
TMsgDlgButtons() << mbOK, 0);
}
else
{
in >> latestVersion;
in.close();
thisVersion = MainMenuForm->VersionLbl->Caption;
for (i = 1; i <= thisVersion.Length(); i++)
{
if (thisVersion[i] == '.')
thisVersion.Delete(i, 1);
}
if (thisVersion.ToInt() >= latestVersion)
{
ShowStatusBarMessage("No updates are available at this time.");
ShowMessage ("There is not an update available at this time.");
}
else
{
ShowStatusBarMessage("An update is available.");
ShowMessage ("There is an update available.");
out.open(updateDownloadName.c_str(), ios::out|ios::binary);
if (out.fail())
{
MessageDlg("Unable to open the destination file for writing.\n"
"Please make sure the disk is accessible and that\n"
"the file has read/write access.",
mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
if (NULL != (hInternetSession = InternetOpen(__argv[0],
INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0)) )
{
hFTPSession = InternetConnect(hInternetSession,
"ftp.powweb.com",
INTERNET_INVALID_PORT_NUMBER,
"myUserName", "myPassword",
INTERNET_SERVICE_FTP, 0,
(DWORD)0);
if( NULL != hFTPSession )
{
hFTPOpenFile = FtpOpenFile(hFTPSession,
updateFTPFileName.c_str(),
GENERIC_READ,
FTP_TRANSFER_TYPE_BINARY, 0);
fileSizeBytes = FtpGetFileSize(hFTPOpenFile, NULL);
if(fileSizeBytes > 0)
{
ShowStatusBarMessage("0% - downloading update...");
totalBytesRead = 0;
int i = 0;
while(totalBytesRead < fileSizeBytes)
{
//get some file bytes from ftp site
InternetReadFile(hFTPOpenFile, buffer, 9999,
&bytesRead);
//write some bytes to the output file.
out.write(reinterpret_cast<char*>(buffer),
sizeof(char) * bytesRead);
totalBytesRead += bytesRead;
i++;
if (i % 5 == 0)
{
percent = (totalBytesRead/(double)fileSizeBytes) * 100.;
ShowStatusBarMessage(IntToStr(percent) +
"% - downloading update...");
Application->ProcessMessages();
}
}
ShowStatusBarMessage("100% - download complete.");
}
else
{
ShowMessage ("Error downloading update: "
"File size is zero or
unavailable.");
}
InternetCloseHandle( hFTPOpenFile );
InternetCloseHandle( hFTPSession );
}
else
{
ShowMessage ("Error connecting to download server.");
}
InternetCloseHandle( hInternetSession );
}
else
{
ShowMessage ("Error opening the internet.");
}
out.close();
}
}
}
Screen->Cursor = crDefault;
--
Anthony West
Technical Details
Message-ID:
<48804143$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<487ef727$1@newsgroups.borland.com> <4j3v745gekhkrf5skft0spuun1s7uo0hn1@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,46736b455fe961ab X-Google-Attributes: gid11508e,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "ANTHONY WEST" <teroni@email.uophx.edu> Newsgroups: borland.public.cppbuilder.internet.socket References: <487ef727$1@newsgroups.borland.com> <4j3v745gekhkrf5skft0spuun1s7uo0hn1@4ax.com> Subject: Re: FTP file download for Indy 9 Date: Fri, 18 Jul 2008 00:07:49 -0700 Lines: 172 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-Antivirus: avast! (VPS 080718-0, 07/18/2008), Outbound message X-Antivirus-Status: Clean NNTP-Posting-Host: 72.208.250.215 Message-ID: <48804143$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1216364867 72.208.250.215 (18 Jul 2008 00:07:47 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail