FTP access suddenly doesn't work!?

Re: FTP access suddenly doesn't work!?

Postby codemaker » Sun Jan 08, 2012 9:46 pm

Enrico,

Is there any chance you can help me with the code you suggested?
It works perfect and thanks again, but I need some improovement and have no idea how to implement it.

What I need is some kind of METER so the user can follow the file uploading or downloading process from FTP server.
The example you sent me does the job excellent, but if the upload or download needs more time (much bigger file(s)), it would be nice if I can show undergoing proceess....

If you have any idea how to implement this, you will make one more programmer very happy, because we got used to your efficient solutions :)

Thanks for any advice
Boris
User avatar
codemaker
 
Posts: 208
Joined: Wed Dec 03, 2008 4:48 pm
Location: Belgrade, Serbia

Re: FTP access suddenly doesn't work!?

Postby Enrico Maria Giordano » Mon Jan 09, 2012 9:09 am

Try this:

Code: Select all  Expand view
#include "Fivewin.ch"


//
// File attributes
//

#define FILE_ATTRIBUTE_READONLY  1
#define FILE_ATTRIBUTE_HIDDEN    2
#define FILE_ATTRIBUTE_SYSTEM    4
#define FILE_ATTRIBUTE_DIRECTORY 16
#define FILE_ATTRIBUTE_ARCHIVE   32
#define FILE_ATTRIBUTE_NORMAL    128
#define FILE_ATTRIBUTE_TEMPORARY 256


//
// access types for InternetOpen()
//

#define INTERNET_OPEN_TYPE_PRECONFIG                    0   // use registry configuration
#define INTERNET_OPEN_TYPE_DIRECT                       1   // direct to net
#define INTERNET_OPEN_TYPE_PROXY                        3   // via named proxy
#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  4   // prevent using java/script/INS


//
// manifests
//

#define INTERNET_INVALID_PORT_NUMBER    0           // use the protocol-specific default

#define INTERNET_DEFAULT_FTP_PORT       21          // default for FTP servers
#define INTERNET_DEFAULT_GOPHER_PORT    70          //    "     " gopher "
#define INTERNET_DEFAULT_HTTP_PORT      80          //    "     " HTTP   "
#define INTERNET_DEFAULT_HTTPS_PORT     443         //    "     " HTTPS  "
#define INTERNET_DEFAULT_SOCKS_PORT     1080        // default for SOCKS firewall servers.


//
// service types for InternetConnect()
//

#define INTERNET_SERVICE_FTP     1
#define INTERNET_SERVICE_GOPHER  2
#define INTERNET_SERVICE_HTTP    3


//
// flags for FTP
//

#define INTERNET_FLAG_TRANSFER_ASCII  1
#define INTERNET_FLAG_TRANSFER_BINARY 2


//
// file access types
//

#define GENERIC_READ  2147483648
#define GENERIC_WRITE 1073741824


FUNCTION MAIN()

    LOCAL oDlg, oPrg

    DEFINE DIALOG oDlg

    @ 2, 2 PROGRESS oPrg;
           SIZE 100, 15

    @ 3, 2 BUTTON "FTP upload";
           ACTION UPLOAD( oPrg )

    ACTIVATE DIALOG oDlg;
             CENTER

    RETURN NIL


STATIC FUNCTION UPLOAD( oPrg )

    LOCAL hInternet, hConnect, hSource, hDest, nRead

    LOCAL cData := SPACE( 1024 )

    LOCAL nPos := 0

    hInternet = INTERNETOPEN( "Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )

    hConnect = INTERNETCONNECT( hInternet, "myftpaddress", INTERNET_INVALID_PORT_NUMBER, "myuserid", "mypassword", INTERNET_SERVICE_FTP, 0, 0 )

    hDest = FTPOPENFILE( hConnect, "/emagsoftware.it/FTPWRITEFILE.PRG", GENERIC_WRITE, 0, 0 )

    oPrg:SetPos( 0 )

    oPrg:SetRange( 0, FSIZE( "FTPWRITEFILE.PRG" ) )

    hSource = FOPEN( "FTPWRITEFILE.PRG" )

    WHILE .T.
        nRead = FREAD( hSource, @cData, LEN( cData ) )

        IF nRead = 0
            IF FERROR() = 0
                ? "Upload OK"
            ELSE
                ? "Read error"
            ENDIF

            EXIT
        ENDIF

        IF !INTERNETWRITEFILE( hDest, @cData, nRead )
            ? "Upload error"
            EXIT
        ENDIF

        nPos += LEN( cData )

        oPrg:SetPos( nPos )
    ENDDO

    FCLOSE( hSource )

    INTERNETCLOSEHANDLE( hDest )

    INTERNETCLOSEHANDLE( hConnect )

    INTERNETCLOSEHANDLE( hInternet )

    RETURN NIL


#pragma BEGINDUMP

#include "windows.h"
#include "wininet.h"
#include "hbapi.h"


HB_FUNC( INTERNETOPEN )
{
    hb_retnl( ( LONG ) InternetOpen( hb_parc( 1 ), hb_parnl( 2 ), hb_parc( 3 ), hb_parc( 4 ), hb_parnl( 5 ) ) );
}


HB_FUNC( INTERNETCLOSEHANDLE )
{
    hb_retl( InternetCloseHandle( ( HINTERNET ) hb_parnl( 1 ) ) );
}


HB_FUNC( INTERNETCONNECT )
{
    hb_retnl( ( LONG ) InternetConnect( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( INTERNET_PORT ) hb_parnl( 3 ), hb_parc( 4 ), hb_parc( 5 ), hb_parnl( 6 ), hb_parnl( 7 ), hb_parnl( 8 ) ) );
}


HB_FUNC( FTPOPENFILE )
{
    hb_retnl( ( LONG ) FtpOpenFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}


HB_FUNC( FTPGETFILESIZE )
{
    DWORD nFileSizeHigh;

    hb_retnl( ( LONG ) FtpGetFileSize( ( HINTERNET ) hb_parnl( 1 ), &nFileSizeHigh ) );
}


HB_FUNC( INTERNETREADFILE )
{
    DWORD nBytesRead;

    BOOL lSuccess = InternetReadFile( ( HINTERNET ) hb_parnl( 1 ), ( LPVOID ) hb_parc( 2 ), hb_parclen( 2 ), &nBytesRead );

    if ( !lSuccess )
        hb_retnl( -1 );
    else
        hb_retnl( nBytesRead );
}


HB_FUNC( INTERNETWRITEFILE )
{
    DWORD nBytesWritten;

    BOOL lSuccess = InternetWriteFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), &nBytesWritten );

    hb_retl( lSuccess );
}

#pragma ENDDUMP


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: FTP access suddenly doesn't work!?

Postby codemaker » Mon Jan 09, 2012 1:39 pm

Thank you Enrico,

I will try this today or tomorrow, right now I am in big rush.

One more thing, I hope you don't mind.
Any sample for DOWNLOADING the file from FTP?





I wish I have enough knowledge to help you one day :)

Ragards
Boris
User avatar
codemaker
 
Posts: 208
Joined: Wed Dec 03, 2008 4:48 pm
Location: Belgrade, Serbia

Re: FTP access suddenly doesn't work!?

Postby Enrico Maria Giordano » Mon Jan 09, 2012 1:43 pm

Here it is:

Code: Select all  Expand view
#include "Fivewin.ch"


//
// File attributes
//

#define FILE_ATTRIBUTE_READONLY  1
#define FILE_ATTRIBUTE_HIDDEN    2
#define FILE_ATTRIBUTE_SYSTEM    4
#define FILE_ATTRIBUTE_DIRECTORY 16
#define FILE_ATTRIBUTE_ARCHIVE   32
#define FILE_ATTRIBUTE_NORMAL    128
#define FILE_ATTRIBUTE_TEMPORARY 256


//
// access types for InternetOpen()
//

#define INTERNET_OPEN_TYPE_PRECONFIG                    0   // use registry configuration
#define INTERNET_OPEN_TYPE_DIRECT                       1   // direct to net
#define INTERNET_OPEN_TYPE_PROXY                        3   // via named proxy
#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  4   // prevent using java/script/INS


//
// manifests
//

#define INTERNET_INVALID_PORT_NUMBER    0           // use the protocol-specific default

#define INTERNET_DEFAULT_FTP_PORT       21          // default for FTP servers
#define INTERNET_DEFAULT_GOPHER_PORT    70          //    "     " gopher "
#define INTERNET_DEFAULT_HTTP_PORT      80          //    "     " HTTP   "
#define INTERNET_DEFAULT_HTTPS_PORT     443         //    "     " HTTPS  "
#define INTERNET_DEFAULT_SOCKS_PORT     1080        // default for SOCKS firewall servers.


//
// service types for InternetConnect()
//

#define INTERNET_SERVICE_FTP     1
#define INTERNET_SERVICE_GOPHER  2
#define INTERNET_SERVICE_HTTP    3


//
// flags for FTP
//

#define INTERNET_FLAG_TRANSFER_ASCII  1
#define INTERNET_FLAG_TRANSFER_BINARY 2


//
// file access types
//

#define GENERIC_READ  2147483648
#define GENERIC_WRITE 1073741824


FUNCTION MAIN()

    LOCAL oDlg, oPrg

    DEFINE DIALOG oDlg

    @ 2, 2 PROGRESS oPrg;
           SIZE 100, 15

    @ 3, 2 BUTTON "FTP download";
           ACTION DOWNLOAD( oPrg )

    ACTIVATE DIALOG oDlg;
             CENTER

    RETURN NIL


STATIC FUNCTION DOWNLOAD( oPrg )

    LOCAL hInternet, hConnect, hSource, hDest, nRead

    LOCAL cData := SPACE( 1024 )

    LOCAL nPos := 0

    hInternet = INTERNETOPEN( "Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )

    hConnect = INTERNETCONNECT( hInternet, "myftpaddress", INTERNET_INVALID_PORT_NUMBER, "myuserid", "mypassword", INTERNET_SERVICE_FTP, 0, 0 )

    hSource = FTPOPENFILE( hConnect, "/emagsoftware.it/FTPWRITEFILE.PRG", GENERIC_READ, 0, 0 )

    oPrg:SetPos( 0 )

    oPrg:SetRange( 0, FTPGETFILESIZE( hSource ) )

    hDest = FCREATE( "FTPWRITEFILENEW.PRG" )

    WHILE .T.
        nRead = INTERNETREADFILE( hSource, @cData )

        IF nRead = -1
            ? "Download error"
            EXIT
        ENDIF

        IF nRead = 0
            ? "Download OK"
            EXIT
        ENDIF

        FWRITE( hDest, cData, nRead )

        nPos += LEN( cData )

        oPrg:SetPos( nPos )
    ENDDO

    FCLOSE( hDest )

    INTERNETCLOSEHANDLE( hSource )

    INTERNETCLOSEHANDLE( hConnect )

    INTERNETCLOSEHANDLE( hInternet )

    RETURN NIL


#pragma BEGINDUMP

#include "windows.h"
#include "wininet.h"
#include "hbapi.h"


HB_FUNC( INTERNETOPEN )
{
    hb_retnl( ( LONG ) InternetOpen( hb_parc( 1 ), hb_parnl( 2 ), hb_parc( 3 ), hb_parc( 4 ), hb_parnl( 5 ) ) );
}


HB_FUNC( INTERNETCLOSEHANDLE )
{
    hb_retl( InternetCloseHandle( ( HINTERNET ) hb_parnl( 1 ) ) );
}


HB_FUNC( INTERNETCONNECT )
{
    hb_retnl( ( LONG ) InternetConnect( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( INTERNET_PORT ) hb_parnl( 3 ), hb_parc( 4 ), hb_parc( 5 ), hb_parnl( 6 ), hb_parnl( 7 ), hb_parnl( 8 ) ) );
}


HB_FUNC( FTPOPENFILE )
{
    hb_retnl( ( LONG ) FtpOpenFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}


HB_FUNC( FTPGETFILESIZE )
{
    DWORD nFileSizeHigh;

    hb_retnl( ( LONG ) FtpGetFileSize( ( HINTERNET ) hb_parnl( 1 ), &nFileSizeHigh ) );
}


HB_FUNC( INTERNETREADFILE )
{
    DWORD nBytesRead;

    BOOL lSuccess = InternetReadFile( ( HINTERNET ) hb_parnl( 1 ), ( LPVOID ) hb_parc( 2 ), hb_parclen( 2 ), &nBytesRead );

    if ( !lSuccess )
        hb_retnl( -1 );
    else
        hb_retnl( nBytesRead );
}

HB_FUNC( INTERNETWRITEFILE )
{
    DWORD nBytesWritten;

    BOOL lSuccess = InternetWriteFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), &nBytesWritten );

    hb_retl( lSuccess );
}

#pragma ENDDUMP


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: FTP access suddenly doesn't work!?

Postby RAMESHBABU » Mon Jan 09, 2012 2:32 pm

Hello Mr.Enrico,

Code: Select all  Expand view


Error: Unresolved external 'InternetOpenA' referenced from C:\FWH\TESTS\FTPDLOAD.OBJ
Error: Unresolved external 'InternetCloseHandle' referenced from C:\FWH\TESTS\FTPDLOAD.OBJ
Error: Unresolved external 'InternetConnectA' referenced from C:\FWH\TESTS\FTPDLOAD.OBJ
Error: Unresolved external 'FtpOpenFileA' referenced from C:\FWH\TESTS\FTPDLOAD.OBJ
Error: Unresolved external 'FtpGetFileSize' referenced from C:\FWH\TESTS\FTPDLOAD.OBJ
Error: Unresolved external 'InternetReadFile' referenced from C:\FWH\TESTS\FTPDLOAD.OBJ
Error: Unresolved external 'InternetWriteFile' referenced from C:\FWH\TESTS\FTPDLOAD.OBJ
* Linking errors *

 


Please tell me the xHarbour/FWH lib, which is missing the above functions, in my link script.

My best regards to you,

Ramesh Babu P
User avatar
RAMESHBABU
 
Posts: 624
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: FTP access suddenly doesn't work!?

Postby Enrico Maria Giordano » Mon Jan 09, 2012 3:04 pm

wininet.lib from your C compiler.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: FTP access suddenly doesn't work!?

Postby codemaker » Mon Jan 09, 2012 4:22 pm

Enrico Maria Giordano wrote:Here it is:

EMG

:)
I am proud I virtually know you for so many years by now... a FWH legend :)
Thank you again.

Maybe I have some job in Italy this year in Milano. I would be happy to buy you a dinner, lunch, branch - name it, it will be done :)
User avatar
codemaker
 
Posts: 208
Joined: Wed Dec 03, 2008 4:48 pm
Location: Belgrade, Serbia


Re: FTP access suddenly doesn't work!?

Postby RAMESHBABU » Tue Jan 10, 2012 11:11 am

Mr.Enrico,

Thank you very much.

- Ramesh Babu P
User avatar
RAMESHBABU
 
Posts: 624
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: FTP access suddenly doesn't work!?

Postby Patricio Avalos Aguirre » Thu Apr 18, 2013 10:00 pm

Hola

me ha servidor estas funciones de FTP ya que estaba saliendo un mensaje Cannot load WinINet.dll

necesito saber si alguien tiene un ejemplo de como sacar el directorio del ftp, como ejemplo DirFTP()
Saludos
Patricio

__________________________________________________________________
Version: Harbour 3.2.0dev (r1307082134),Compiler: Borland C++ 5.8.2 (32-bit)
PCode version: 0.3, FWH 13.2
http://www.sialm.cl
User avatar
Patricio Avalos Aguirre
 
Posts: 1060
Joined: Fri Oct 07, 2005 1:56 pm
Location: La Serena, Chile

Re: FTP access suddenly doesn't work!?

Postby Wanderson » Mon Apr 22, 2013 11:45 pm

Enrico can you post a sample to verify if a file exist in a ftp?

Thanks.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: FTP access suddenly doesn't work!?

Postby Verhoven » Thu Apr 25, 2013 11:23 am

The answer is in the return value of the function you use to write de file via FTP:

HB_FUNC( INTERNETWRITEFILE )
{
DWORD nBytesWritten;

BOOL lSuccess = InternetWriteFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), &nBytesWritten );

hb_retl( lSuccess );
}

So if it is .T. the file was writen in the destination.
Verhoven
 
Posts: 521
Joined: Sun Oct 09, 2005 7:23 pm

Re: FTP access suddenly doesn't work!?

Postby Enrico Maria Giordano » Thu Apr 25, 2013 12:56 pm

Wanderson wrote:Enrico can you post a sample to verify if a file exist in a ftp?

Thanks.


Try checking the return value of FTPOPENFILE().

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: FTP access suddenly doesn't work!?

Postby Verhoven » Sun Apr 28, 2013 7:54 pm

How can we make DirectoryFTP.
I've been trying to do from the Enricco's code as the example and looking into the wininet.h but I can´t .
I can´t find the way to build the HB_FUNC.
Verhoven
 
Posts: 521
Joined: Sun Oct 09, 2005 7:23 pm

Re: FTP access suddenly doesn't work!?

Postby Verhoven » Thu May 02, 2013 2:51 pm

Trying to do de FTPDirectory() I have include this code:

Code: Select all  Expand view
HB_FUNC( FTPFINDFIRSTFILE )  
{
    hb_retnl( ( LONG ) FtpFindFirstFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( WIN32_FIND_DATA * ) hb_parc( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}

HB_FUNC( INTERNETFINDNEXTFILE )  
{
    BOOL lSuccess = InternetFindNextFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) ;
   
    hb_retl( lSuccess );
}
 


But I get an error when executing the program and I can´t find the reason why. The code for the FTPDirectory command is this:
Code: Select all  Expand view
function FTPDirectory( cMask,cIP,cUser,cPass )
   local hFTPDir, aFiles := {}
   local oWin32FindData, cBuffer
   local i:=0

   default cMask := "*.*"

   STRUCT oWin32FindData
      MEMBER nFileAttributes  AS DWORD
      MEMBER nCreationTime    AS STRING LEN 8
      MEMBER nLastReadAccess  AS STRING LEN 8
      MEMBER nLastWriteAccess AS STRING LEN 8
      MEMBER nSizeHight       AS DWORD
      MEMBER nSizeLow         AS DWORD
      MEMBER nReserved0       AS DWORD
      MEMBER nReserved1       AS DWORD
      MEMBER cFileName        AS STRING LEN 260
      MEMBER cAltName         AS STRING LEN  14
   ENDSTRUCT

   hInternet = INTERNETOPEN( "Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )
   hConnect = INTERNETCONNECT( hInternet, cIP, INTERNET_INVALID_PORT_NUMBER, cUser, cPass, INTERNET_SERVICE_FTP, 0, 0 )
   //msginfo(hConnect)
   
   if hConnect != nil
      cBuffer = oWin32FindData:cBuffer
      hFTPDir = FtpFindFirstFile( hConnect, cMask, @cBuffer, 0, 0 )
      oWin32FindData:cBuffer = cBuffer
      if ! empty( oWin32FindData:cFileName )
         aadd( aFiles, { oWin32FindData:cFileName,;
                         oWin32FindData:nSizeLow } )
         while InternetFindNextFile( hFTPDir, @cBuffer )
            oWin32FindData:cBuffer = cBuffer
            aadd( aFiles, { oWin32FindData:cFileName,;
                            oWin32FindData:nSizeLow } )
         end
      endif
      InternetCloseHandle( hFTPDir )
   endif
   
   for i=1 TO len(aFiles)
      msginfo(aFiles[i,1])
   next i  

return aFiles


The error is: hb_xfree(00E5C368) Pointer Overflow'' ''
Verhoven
 
Posts: 521
Joined: Sun Oct 09, 2005 7:23 pm

Previous

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 89 guests