🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages Started by ANTHONY WEST Thu, 17 Jul 2008 07:39
FTP file download for Indy 9
#4
Author: ANTHONY WEST
Date: Thu, 17 Jul 2008 07:39
84 lines
3828 bytes
I am using Borland builder 4 standard. I have downloaded
Indy 9 source code that I am attempting to figure out how to use,
but I do not have a clue on how to begin. My goal is to download a
file from my ftp site using a username and password. The main
purpose is to have my app check for updates and download
them if available. I am already using code to download a version
information file via HTTP.

This is what is left that needs to be done:

- log in to my FTP site using name and password
- download the file while showing the download progress

I have googled all day for C++ code on how to do this. No
luck.

Here is my source code so far:

void __fastcall TImageForm::UpdatesBtnClick(TObject *Sender)
{

  int latestVersion, i;
  AnsiString fileName, updateLink, thisVersion;
  ifstream in;


    Screen->Cursor = crAppStart;
    ShowStatusBarMessage("Connecting to update server...");
    updateLink =
"http://www.aswsoftware.com/products/bibleart/currentVersion.upd";
    fileName = MainMenuForm->DirLbl->Caption + "LatestVersion.upd";

    if(URLDownloadToFile(0,updateLink.c_str(), fileName.c_str(),0,0))
    {
      MessageDlg("Unable to connect to update server. Please make\nsure "
                 "that you are connected to the internet and\n try "
                 "again. If you are connected to the internet, then\n"
                 "the update server is not responding. Please visit\n"
                 "www.aswsoftware.com\nto manually check for the latest "
                 "version.", mtError, TMsgDlgButtons() << mbOK, 0);
      ShowStatusBarMessage("Failed to obtain update information.");
    }
    else
    {
      ShowStatusBarMessage("Getting latest version information from
server...");
      in.open(fileName.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("Downloading update.");

          //  This is where I need FTP code

          ShowMessage ("Update Downloaded.");
        }
      }
    }
    Screen->Cursor = crDefault;
}

--
Anthony West
<-- What a great day to be alive :) -->
Re: FTP file download for Indy 9
#5
Author: Remy Lebeau (TeamB)
Date: Thu, 17 Jul 2008 18:03
49 lines
2214 bytes
"ANTHONY WEST" <teroni@email.uophx.edu> wrote in message 
news:487ef727$1@newsgroups.borland.com...

> I am using Borland builder 4 standard. I have downloaded
> Indy 9 source code that I am attempting to figure out how
> to use, but I do not have a clue on how to begin.

Indy does not officially support BCB 4 anymore.

> - log in to my FTP site using name and password

TIdFTP has UserName and Password properties for that.

> - download the file while showing the download progress

TIdFTP has a Get() method and OnWork... events for that.

Note - in order to use the OnWork events for a progress bar, you have to 
retreive the file's size before downloading the actual file.  You can use 
TIdFTP::Size() or TIdFTP::List() for that.

>          //  This is where I need FTP code

IdFTP1->UserName = "...";
IdFTP1->Password = "...";
IdFTP1->Host = "...";
IdFTP1->Port = ...;
try
{
    IdFTP1->Connect();
    try
    {
        IdFTP1->Get("...");
    }
    __finally
    {
        IdFTP1->Disconnect();
    }
}
catch(const Exception &e)
{
    MessageDlg("Unable to download update. " + e.Message, mtError, 
TMsgDlgButtons() << mbOK, 0);
    ShowStatusBarMessage("Failed to obtain update.");
}


Gambit 
Re: FTP file download for Indy 9
#6
Author: Bob Gonder
Date: Thu, 17 Jul 2008 19:01
45 lines
2107 bytes
ANTHONY WEST wrote:

>I am using Borland builder 4 standard. I have downloaded
>Indy 9 source code that I am attempting to figure out how to use,
>    if(URLDownloadToFile(0,updateLink.c_str(), fileName.c_str(),0,0))

That isn't an Indy function. It is a Win32 API function.
Microsoft also supports FTP with FtpGetFile() (no interaction)
or use the FtpOpenFile() and InternetReadFile() to report progress.
There is a fairly decent overview of the MS FTP here:
http://msdn.microsoft.com/en-us/library/aa384180(VS.85).aspx

InternetOpen()
InternetConnect() <-username and password here
FtpGetFile()
InternetCloseHandle()


if( NULL != (hInternetSession = InternetOpen(
			__argv[0],
			INTERNET_OPEN_TYPE_PRECONFIG,
			NULL, NULL, 0)) )
{
	hFTPSession = InternetConnect(
			hInternetSession,
			FtpServerURL,// ftp.MySite.com
			INTERNET_INVALID_PORT_NUMBER,
			Name,
			Password,
			INTERNET_SERVICE_FTP,
			0, (DWORD)0);
	if( NULL != hFTPSession )
	{
		bResult = FtpGetFile(
			hFTPSession,
			FromFilename,ToFilename,
		        FALSE,
		       	FILE_ATTRIBUTE_NORMAL,
			FTP_TRANSFER_TYPE_BINARY,
			0/*context*/);

		InternetCloseHandle( hFTPSession );
	};
	InternetCloseHandle( hInternetSession );
};
Re: FTP file download for Indy 9
#7
Author: ANTHONY WEST
Date: Fri, 18 Jul 2008 07:07
170 lines
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
Re: FTP file download for Indy 9
#8
Author: Remy Lebeau (TeamB)
Date: Fri, 18 Jul 2008 16:47
38 lines
2175 bytes
"ANTHONY WEST" <teroni@email.uophx.edu> wrote in message 
news:48804143$1@newsgroups.borland.com...

> - After including the wininet.h and some FTP code, I
> received the "Unresolved external symbol referenced
> from module" error.

You need to add wininet.lib to your project.

> I could not find a wininet.lib or .obj that I could add
> to the project to remove this error.

It should be in the $(BCB)\lib or $(BCB)\lib\psdk folder.

> 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.)

BCB should have its own copy of that file as well.

> - 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.

Download a newer copy of the WinInet API headers from Microsoft.  Or get the 
Borland Platform SDK from http://bccsdk.sourceforge.net.

> This function must be a newer one that was not available when
> BCB 4 was released.

Correct.


Gambit 
Re: FTP file download for Indy 9
#9
Author: ANTHONY WEST
Date: Fri, 18 Jul 2008 18:54
35 lines
1956 bytes
> It should be in the $(BCB)\lib or $(BCB)\lib\psdk folder.

Wininet.lib is not anywhere in BCB 4 standard.
Here is a list of all wininet files according to windows search:

wininet.h
wininet.hpp
wininet.dcu //a delphi object file?
wininet.pas

> BCB should have its own copy of that file as well.

Here is a list of all urlmon files:

urlmon.h
urlmon.hpp
urlmon.dcu // another delphi object file?
urlmon.pas

That is all BCB 4 standard contains. I am not sure
what the .dcu files are for. They don't work when
attempting to add them as an object file, but they are
in the Lib\Object\ directory.

> Download a newer copy of the WinInet API headers from Microsoft.
> Or get the Borland Platform SDK from http://bccsdk.sourceforge.net.

That would have saved me a lot of time lol. I downloaded it and that has
everything that I have been missing on several projects that I have had
to improvise on.

Thank you very much for that information.

--
Anthony West
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads