ftp and tprogress

Postby Enrico Maria Giordano » Mon Feb 11, 2008 4:08 pm

Ok, here it is:

Code: Select all  Expand view  RUN
#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 )

    hDest = FTPOPENFILE( hConnect, "/emagsoftware.it/test/atest.prg", GENERIC_WRITE, 0, 0 )

    oPrg:SetPos( 0 )

    oPrg:SetRange( 0, FSIZE( "atest.prg" ) )

    hSource = FOPEN( "atest.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( 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 ), 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
Last edited by Enrico Maria Giordano on Tue Feb 12, 2008 3:34 pm, edited 1 time in total.
User avatar
Enrico Maria Giordano
 
Posts: 8716
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Silvio » Tue Feb 12, 2008 12:22 am

Emg,
the download run ok
but the upload make me Upload Error

why ?
Best Regards, Saludos

Falconi Silvio
User avatar
Silvio
 
Posts: 3107
Joined: Fri Oct 07, 2005 6:28 pm
Location: Teramo,Italy

Postby Enrico Maria Giordano » Tue Feb 12, 2008 8:34 am

I did not receive my crystal ball yet. :-)

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

Postby Silvio » Tue Feb 12, 2008 10:49 am

And if I must use a proxy account
wich function I must use for this proxy to dowload/upload a file ?
Best Regards, Saludos

Falconi Silvio
User avatar
Silvio
 
Posts: 3107
Joined: Fri Oct 07, 2005 6:28 pm
Location: Teramo,Italy

Postby Marc Vanzegbroeck » Tue Feb 12, 2008 11:06 am

Hi,

I must be missing something. I get this error:

Code: Select all  Expand view  RUN
Error: Unresolved external 'InternetOpenA' referenced from C:\FWH\PROJECTS\_TEST
\FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'InternetCloseHandle' referenced from C:\FWH\PROJECTS
\_TEST\FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'InternetConnectA' referenced from C:\FWH\PROJECTS\_T
EST\FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'FtpOpenFileA' referenced from C:\FWH\PROJECTS\_TEST\
FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'FtpGetFileSize' referenced from C:\FWH\PROJECTS\_TES
T\FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'InternetReadFile' referenced from C:\FWH\PROJECTS\_T
EST\FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'InternetWriteFile' referenced from C:\FWH\PROJECTS\_
TEST\FTP\OBJ\MFTP.OBJ


Regards,
Marc
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Postby Enrico Maria Giordano » Tue Feb 12, 2008 11:08 am

Silvio wrote:And if I must use a proxy account
wich function I must use for this proxy to dowload/upload a file ?


http://fivetechsoft.com/forums/viewtopic.php?p=48068#48068

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

Postby Enrico Maria Giordano » Tue Feb 12, 2008 11:08 am

Marc Vanzegbroeck wrote:Hi,

I must be missing something. I get this error:

Code: Select all  Expand view  RUN
Error: Unresolved external 'InternetOpenA' referenced from C:\FWH\PROJECTS\_TEST
\FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'InternetCloseHandle' referenced from C:\FWH\PROJECTS
\_TEST\FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'InternetConnectA' referenced from C:\FWH\PROJECTS\_T
EST\FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'FtpOpenFileA' referenced from C:\FWH\PROJECTS\_TEST\
FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'FtpGetFileSize' referenced from C:\FWH\PROJECTS\_TES
T\FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'InternetReadFile' referenced from C:\FWH\PROJECTS\_T
EST\FTP\OBJ\MFTP.OBJ
Error: Unresolved external 'InternetWriteFile' referenced from C:\FWH\PROJECTS\_
TEST\FTP\OBJ\MFTP.OBJ


Regards,
Marc


You have to link wininet.lib.

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

Postby Marc Vanzegbroeck » Tue Feb 12, 2008 11:46 am

Thanks, now it's working.

Marc
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Postby Marc Vanzegbroeck » Tue Feb 12, 2008 1:19 pm

Enrico,

I found a little error in your upload example.

Code: Select all  Expand view  RUN
hDest = FTPOPENFILE( hConnect, "/emagsoftware.it/test/atest.prg", GENERIC_READ, 0, 0 )


should be

Code: Select all  Expand view  RUN
hDest = FTPOPENFILE( hConnect, "/emagsoftware.it/test/atest.prg", GENERIC_WRITE, 0, 0 )


Regards,
Marc
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Postby Enrico Maria Giordano » Tue Feb 12, 2008 3:34 pm

Marc Vanzegbroeck wrote:Enrico,

I found a little error in your upload example.

Code: Select all  Expand view  RUN
hDest = FTPOPENFILE( hConnect, "/emagsoftware.it/test/atest.prg", GENERIC_READ, 0, 0 )


should be

Code: Select all  Expand view  RUN
hDest = FTPOPENFILE( hConnect, "/emagsoftware.it/test/atest.prg", GENERIC_WRITE, 0, 0 )


Regards,
Marc


Damn! A copy and paste error! That explains why Silvio couldn't get my sample to work! Sorry :-(

I'm going to edit my message...

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

Previous

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 61 guests