Announcement

Collapse
No announcement yet.

How to do SFTP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to do SFTP

    Hi All

    I found the following program by Pierre from http://pages.videotron.com/lyra/Powe...ameCode=GotCha

    but it does FTP only . How to convert it to SFTP or Secure File Transfer Protocol ?

    All help appreciated. Thanks in advance

    Code:
    '
    
    #COMPILE EXE '#Win 8.04#
    #DIM ALL
    #INCLUDE "Win32Api.inc"
    #INCLUDE "CommCtrl.inc"
    #INCLUDE "WiniNet.inc"
    #RESOURCE "FtpDownload.pbr"
    
    DECLARE FUNCTION MakeSureDirectoryPathExists LIB "DbgHelp.dll" _
    ALIAS "MakeSureDirectoryPathExists"(lpDirName AS ASCIIZ) AS LONG
    
    TYPE GlobalVar
      hDlg               AS DWORD
      hSession           AS DWORD
      hConnect           AS DWORD
      FileSize           AS QUAD
      FilesSizeAdded     AS QUAD
      BytesSent          AS QUAD
      BytesSentTotal     AS QUAD
      BytesRecieved      AS QUAD
      BytesRecievedTotal AS QUAD
      FtpState           AS DWORD
      CallbackExit       AS LONG
      ProgressBarFile    AS LONG
      ProgressBarGroup   AS LONG
      ProgressBarMax     AS LONG
    END TYPE
    
    GLOBAL pg AS GlobalVar POINTER
    
    $AppName          = "FTP download"
    
    %LabelServer      = 101
    %LabelUser        = 102
    %LabelPassword    = 103
    %LabelFolder      = 104
    %LabelMask        = 105
    %LabelLocalPath   = 106
    
    %TextboxServer    = 201
    %TextboxUser      = 202
    %TextboxPassword  = 203
    %TextboxFolder    = 204
    %TextboxMask      = 205
    %TextboxLocalPath = 206
    %Textbox          = 207
    
    %ButtonDownload   = 301
    %ButtonCancel     = 302
    %ButtonExplore    = 303
    %ButtonClear      = 304
    
    %ProgressbarGroup = 501
    %ProgressbarFile  = 502
    
    %PBST_NORMAL       = &H0001
    %PBM_SETSTATE      = %WM_USER + 16
    
    %FTP_WAIT         = 00  'INTERNET_ASYNC_RESULT enum
    %FTP_ENUM         = 01
    %FTP_DOWNLOAD     = 02
    %FTP_DOWNLOADING  = 03
    %FTP_UPLOAD       = 04
    %FTP_UPLOADING    = 05
    %FTP_CREATINGDIR  = 06
    %FTP_CREATEDIR    = 07
    %FTP_REMOVINGDIR  = 08
    %FTP_REMOVEDIR    = 09
    %FTP_DELETINGFILE = 10
    %FTP_DELETEFILE   = 11
    %FTP_RENAMINGFILE = 12
    %FTP_RENAMEFILE   = 13
    %FTP_ENUMFILES    = 14
    
    '______________________________________________________________________________
    
    FUNCTION FileTimeToInternationalDate(File_Time AS FILETIME) AS STRING
     LOCAL Sys_Time AS SYSTEMTIME
    
     FileTimeToSystemTime(File_Time, Sys_Time)
    
     FUNCTION = FORMAT$(Sys_Time.wyear, "0000") & "/" & _
                FORMAT$(Sys_Time.wMonth,  "00") & "/" & _
                FORMAT$(Sys_Time.wDay,    "00")
    END FUNCTION
    '______________________________________________________________________________
    
    SUB Say(sBuffer AS STRING)
      sBuffer = sBuffer & $CRLF
      SendDlgItemMessage(@pg.hDlg, %Textbox, %EM_SETSEL, -2, -2)                           'Move the caret to the end of text
      SendDlgItemMessage(@pg.hDlg, %Textbox, %EM_REPLACESEL, %TRUE, BYVAL STRPTR(sBuffer)) 'Insert a string at caret position
    END SUB
    '______________________________________________________________________________
    
    SUB Bye()
      Say(STRING$(100, "¯")) 'CHR$(175)
      CONTROL ENABLE @pg.hDlg, %ButtonDownload
      CONTROL ENABLE @pg.hDlg, %ButtonClear
    END SUB
    '______________________________________________________________________________
    
    FUNCTION InternetStatusCallback(BYVAL hInternet AS DWORD, BYVAL dwContext AS DWORD, _
                                    BYVAL dwInternetStatus AS DWORD, BYVAL lpvStatusInfo AS DWORD, _
                                    BYVAL dwStatusInfoLength AS DWORD) AS LONG
     'Description: Callback routine for asynchronous WinInet operations, no value returned.
     'hInternet          The handle for which the callback function is called.
     'dwContext          Pointer to the application defined context.
     'dwInternetStatus   Status code indicating why the callback is called.
     'lpvStatusInfo      Pointer to a buffer holding callback specific data.
     'dwStatusInfoLength Specifies size of lpvStatusInformation buffer.
    
     STATIC sBuffer    AS STRING
     LOCAL  pBuffer    AS ASCIIZ POINTER
     LOCAL  pByteCount AS DWORD POINTER
     LOCAL  pStatus    AS DWORD POINTER
    
     IF LEN(sBuffer) < dwStatusInfoLength THEN 'Create a longer buffer
       sBuffer = NUL$(dwStatusInfoLength)
       pBuffer = STRPTR(sBuffer)
     ELSE
       sBuffer = NUL$(LEN(sBuffer)) 'Reset buffer
     END IF
    
     SELECT CASE dwInternetStatus
    
       CASE %INTERNET_STATUS_HANDLE_CREATED
         Say("HANDLE_CREATED")
    
       CASE %INTERNET_STATUS_RESOLVING_NAME
         MoveMemory(pBuffer, BYVAL lpvStatusInfo, dwStatusInfoLength)
         Say("RESOLVING_NAME: " & @pBuffer)
    
       CASE %INTERNET_STATUS_NAME_RESOLVED
         MoveMemory(pBuffer, BYVAL lpvStatusInfo, dwStatusInfoLength)
         Say("NAME_RESOLVED: " & @pBuffer)
    
       CASE %INTERNET_STATUS_CONNECTING_TO_SERVER
         MoveMemory(pBuffer, BYVAL lpvStatusInfo, dwStatusInfoLength)
         Say("CONNECTING_TO_SERVER: " & @pBuffer)
    
       CASE %INTERNET_STATUS_CONNECTED_TO_SERVER
         MoveMemory(pBuffer, BYVAL lpvStatusInfo, dwStatusInfoLength)
         Say("CONNECTED_TO_SERVER: " & @pBuffer)
    
       CASE %INTERNET_STATUS_REQUEST_SENT
         pByteCount         = lpvStatusInfo
         @pg.BytesSent      = @pg.BytesSent      + @pByteCount
         @pg.BytesSentTotal = @pg.BytesSentTotal + @pByteCount
         'Say("REQUEST_SENT: " & FORMAT$(@pByteCount, "0,") & " bytes")
    
       CASE %INTERNET_STATUS_RESPONSE_RECEIVED
         pByteCount             = lpvStatusInfo
         @pg.BytesRecieved      = @pg.BytesRecieved      + @pByteCount
         @pg.BytesRecievedTotal = @pg.BytesRecievedTotal + @pByteCount
    
         SELECT CASE @pg.FtpState
    
           CASE %FTP_DOWNLOADING
             'Say("Downloaded " & FORMAT$(@pg.BytesRecieved, "0,") & " of " & FORMAT$(@pg.FileSize, "0,"))
    
             @pg.ProgressBarFile = @pg.BytesRecieved / @pg.FileSize * @pg.ProgressBarMax
             SendDlgItemMessage(@pg.hDlg, %ProgressbarFile, %PBM_SETPOS, @pg.ProgressBarFile, 0)
    
             @pg.ProgressBarGroup = @pg.BytesRecievedTotal / @pg.FilesSizeAdded * @pg.ProgressBarMax
             SendDlgItemMessage(@pg.hDlg, %ProgressbarGroup, %PBM_SETPOS, @pg.ProgressBarGroup, 0)
    
           CASE ELSE
             Say("Response received: " & FORMAT$(@pByteCount, "0,") & " bytes")
    
         END SELECT
    
       CASE %INTERNET_STATUS_SENDING_REQUEST
    
         SELECT CASE @pg.FtpState
    
           CASE %FTP_UPLOADING
             Say("Uploaded " & FORMAT$(@pg.BytesSent) & " of " & FORMAT$(@pg.FileSize))
    
             @pg.ProgressBarFile = @pg.BytesSent / @pg.FileSize * @pg.ProgressBarMax
             SendDlgItemMessage(@pg.hDlg, %ProgressbarFile, %PBM_SETPOS, @pg.ProgressBarFile, 0)
    
             @pg.ProgressBarGroup = @pg.BytesSentTotal / @pg.FilesSizeAdded * @pg.ProgressBarMax
             SendDlgItemMessage(@pg.hDlg, %ProgressbarGroup, %PBM_SETPOS, @pg.ProgressBarGroup, 0)
    
           CASE ELSE
             'pByteCount = lpvStatusInfo
             'Say("Sending request: " & FORMAT$(@pByteCount, "0,") & " bytes")
    
         END SELECT
    
       CASE %INTERNET_STATUS_DETECTING_PROXY
         Say("DETECTING_PROXY")
    
       CASE %INTERNET_STATUS_REQUEST_COMPLETE
         Say("REQUEST_COMPLETE")
    
       CASE %INTERNET_STATUS_REDIRECT
         MoveMemory(pBuffer, BYVAL lpvStatusInfo, dwStatusInfoLength)
         Say("STATUS_REDIRECT: " & sBuffer)
    
       CASE %INTERNET_STATUS_INTERMEDIATE_RESPONSE
         Say("INTERMEDIATE_RESPONSE")
    
       CASE %INTERNET_STATUS_STATE_CHANGE '200
         'lpvStatusInformation is a pointer to additional informations
         'When the INTERNET_STATUS_STATE_CHANGE flag is set,
         'lpvStatusInformation points to a DWORD that contains one or more of the following flags:
         'Moved between a secure and a non-secure site.
         pStatus = lpvStatusInfo
         SELECT CASE @pStatus
           CASE %INTERNET_STATE_CONNECTED
             Say("STATE_CONNECTED: Moved between secure and nonsecure site")
           CASE %INTERNET_STATE_DISCONNECTED
             Say("STATE_DISCONNECTED")
           CASE %INTERNET_STATE_DISCONNECTED_BY_USER
             Say("DISCONNECTED_BY_USER")
           CASE %INTERNET_STATE_IDLE
             Say("STATE_IDLE")
           CASE %INTERNET_STATE_BUSY
             Say("STATE_BUSY")
           CASE %INTERNET_STATUS_USER_INPUT_REQUIRED
             Say("USER_INPUT_REQUIRED")
         END SELECT
    
       CASE %INTERNET_STATUS_CLOSING_CONNECTION
         Say("CLOSING_CONNECTION")
    
       CASE %INTERNET_STATUS_CONNECTION_CLOSED
         Say("CONNECTION_CLOSED")
    
       CASE %INTERNET_STATUS_HANDLE_CLOSING
         IF @pg.FtpState = %FTP_DOWNLOADING THEN
           @pg.FtpState = %FTP_WAIT
           Say("Downloaded " & FORMAT$(@pg.FileSize, "0,") & " of " & FORMAT$(@pg.FileSize, "0,"))
           Say("HANDLE_CLOSING, Download complete")
         END IF
         IF @pg.FtpState = %FTP_UPLOADING THEN '5
           @pg.FtpState = %FTP_WAIT
           Say("Uploaded " & FORMAT$(@pg.FileSize, "0,") & " of " & FORMAT$(@pg.FileSize, "0,"))
           Say("HANDLE_CLOSING, Upload complete")
         END IF
         SendDlgItemMessage(@pg.hDlg, %ProgressbarFile, %PBM_SETPOS, @pg.ProgressBarMax, 0)
    
       CASE %INTERNET_STATUS_RECEIVING_RESPONSE
       CASE %INTERNET_STATUS_CTL_RESPONSE_RECEIVED
       CASE %INTERNET_STATUS_PREFETCH
       CASE %INTERNET_STATUS_COOKIE_HISTORY
       CASE %INTERNET_STATUS_COOKIE_RECEIVED
       CASE %INTERNET_STATUS_COOKIE_SENT
       CASE %INTERNET_STATUS_P3P_HEADER
       CASE %INTERNET_STATUS_P3P_POLICYREF
       CASE %INTERNET_STATUS_PRIVACY_IMPACTED
       CASE %INTERNET_STATUS_USER_INPUT_REQUIRED
     END SELECT
    
    END FUNCTION
    '______________________________________________________________________________
    
    #IF %PB_REVISION < &H1000
    FUNCTION Download(BYVAL Blank AS LONG)AS LONG
    #ELSE
    THREAD FUNCTION Download(BYVAL Blank AS LONG)AS LONG
    #ENDIF
     LOCAL zFtpServer        AS ASCIIZ * %MAX_PATH
     LOCAL zFtpUser          AS ASCIIZ * %MAX_PATH
     LOCAL zFtpPassword      AS ASCIIZ * %MAX_PATH
     LOCAL zFtpSourceFolder  AS ASCIIZ * %MAX_PATH
     LOCAL zFtpCurrentFolder AS ASCIIZ * %MAX_PATH
     LOCAL zFtpMask          AS ASCIIZ * %MAX_PATH
     LOCAL zLocalPath        AS ASCIIZ * %MAX_PATH
     LOCAL FileInfo          AS WIN32_FIND_DATA
     LOCAL hFileInfo         AS DWORD
     LOCAL dwContext         AS DWORD
     LOCAL FileCount         AS LONG
     LOCAL FileIndex         AS LONG
    
     CONTROL DISABLE  @pg.hDlg, %ButtonDownload
     CONTROL DISABLE  @pg.hDlg, %ButtonClear
     CONTROL GET TEXT @pg.hDlg, %TextboxServer    TO zFtpServer
     CONTROL GET TEXT @pg.hDlg, %TextboxUser      TO zFtpUser
     CONTROL GET TEXT @pg.hDlg, %TextboxPassword  TO zFtpPassword
     CONTROL GET TEXT @pg.hDlg, %TextboxFolder    TO zFtpSourceFolder
     CONTROL GET TEXT @pg.hDlg, %TextboxMask      TO zFtpMask
     CONTROL GET TEXT @pg.hDlg, %TextboxLocalPath TO zLocalPath
    
     zFtpServer       = TRIM$(zFtpServer)
     zFtpSourceFolder = TRIM$(zFtpSourceFolder)
     zLocalPath       = TRIM$(zLocalPath)
    
     IF (LEN(zLocalPath) = 0) THEN 'No path specified
       GetTempPath(%MAX_PATH, zLocalPath) 'Get tmp path
       Say("Local folder not specyfied" & $CRLF & _
           "You could use something like " & $DQ & zLocalPath & $DQ & $CRLF & _
           "Please retry")
       Bye : EXIT FUNCTION
     END IF
     MakeSureDirectoryPathExists(zLocalPath)
     IF (GETATTR(zLocalPath) AND %FILE_ATTRIBUTE_DIRECTORY) <> %FILE_ATTRIBUTE_DIRECTORY THEN
       GetTempPath(%MAX_PATH, zLocalPath) 'Get tmp path
       Say("Unable to create local folder" & $CRLF & _
           "You could use something like " & $DQ & zLocalPath & $DQ & $CRLF & _
           "Please retry.")
       Bye
       EXIT FUNCTION
     END IF
     IF ASC(zLocalPath, - 1) <> 92 THEN zLocalPath = zLocalPath & "\"
    
     @pg.hSession = InternetOpen("FtpAgent", BYVAL %INTERNET_OPEN_TYPE_PRECONFIG, _  'Open a session
                             BYVAL %NULL, BYVAL %NULL, BYVAL 0)
     IF @pg.hSession THEN 'InternetOpen success
       Say("InternetOpen success")
    
       @pg.hConnect = InternetConnect(BYVAL @pg.hSession, zFtpServer, BYVAL %INTERNET_DEFAULT_FTP_PORT, _
                                  zFtpUser, zFtpPassword, BYVAL %INTERNET_SERVICE_FTP, BYVAL 0, BYVAL 0)
       IF @pg.hConnect THEN 'InternetConnect success
         Say("InternetConnect success, handle is 0x " & HEX$(@pg.hConnect))
         LOCAL InternetStatus AS DWORD
         InternetStatus = InternetSetStatusCallback(@pg.hConnect, BYVAL CODEPTR(InternetStatusCallback))
         'Returns the previously defined status callback function IF successful,
         'NULL if there was no previously defined status callback function,
         'or INTERNET_INVALID_STATUS_CALLBACK if the callback function is not valid.
         IF InternetStatus = %INTERNET_INVALID_STATUS_CALLBACK THEN
           MessageBox(@pg.hDlg, "InternetSetStatusCallback error", $AppName & " - Error", %MB_ICONINFORMATION OR %MB_OK OR %MB_TOPMOST)
         ELSE
           Say("InternetSetStatusCallback success")
         END IF
         FtpGetCurrentDirectory(@pg.hConnect, zFtpCurrentFolder, %MAX_PATH)
         Say("Current folder is " & $DQ & zFtpCurrentFolder & $DQ)
         IF LEN(zFtpSourceFolder) THEN
           IF zFtpCurrentFolder <> zFtpSourceFolder THEN
             IF FtpSetCurrentDirectory(@pg.hConnect, zFtpSourceFolder) THEN 'Set current directory
               Say("Folder is now " & zFtpSourceFolder)
             ELSE
               Say("Error in setting folder to " & $DQ & zFtpSourceFolder & $DQ)
               Bye
               EXIT FUNCTION
             END IF
           END IF
         END IF
    
         hFileInfo = FtpFindFirstFile(@pg.hConnect, zFtpMask, FileInfo, %INTERNET_FLAG_NEED_FILE, 0) 'Get first files name
         'Return zero if no files found, GetLastError returns ERROR_NO_MORE_FILES
         @pg.FilesSizeAdded = 0
         IF hFileInfo THEN
           Say("Files to download: ")
           DO
             IF (FileInfo.dwFileAttributes AND %FILE_ATTRIBUTE_DIRECTORY) THEN 'Its a folder
               Say("[" & FileInfo.cFileName & " subfolder will not be included]")
             ELSE 'Its a file
               INCR FileCount
               REDIM PRESERVE FileInfoArray(1 TO FileCount) AS WIN32_FIND_DATA
               FileInfoArray(FileCount) = FileInfo
               @pg.FileSize             = FileInfo.nFileSizeLow + FileInfo.nFileSizeHigh * %MAXDWORD
               @pg.FilesSizeAdded       = @pg.FilesSizeAdded + @pg.FileSize
               Say(FileTimeToInternationalDate(FileInfo.ftLastWriteTime) & $SPC & _
                   FileInfo.cFileName & $SPC & _
                   FORMAT$(@pg.FileSize, "0,") & " bytes")
             END IF
           LOOP WHILE InternetFindNextFile(hFileInfo, BYVAL VARPTR(FileInfo)) 'Get subsequents file name
    
           Say("Total bytes count is " & FORMAT$(@pg.FilesSizeAdded, "0,"))
           @pg.BytesRecievedTotal = 0
           SendDlgItemMessage(@pg.hDlg, %ProgressbarGroup, %PBM_SETPOS, 00, 0) 'GroupFileSize pos
    
           FOR FileIndex = 1 TO FileCount
             Say(STRING$(100, "-"))
             @pg.FileSize = FileInfoArray(FileIndex).nFileSizeLow + FileInfoArray(FileIndex).nFileSizeHigh * %MAXDWORD
             Say("Downloading " & FileInfoArray(FileIndex).cFileName & $SPC & _
                 FORMAT$(@pg.FileSize, "0,") & " bytes " & _
                 FileTimeToInternationalDate(FileInfoArray(FileIndex).ftLastWriteTime))
             @pg.FtpState = %FTP_DOWNLOADING 'Set current state for download
             @pg.BytesRecieved = 0
             SendDlgItemMessage(@pg.hDlg, %ProgressbarFile, %PBM_SETPOS, 0, 0) 'FileSize pos
             IF @pg.CallbackExit THEN
               dwContext = 0
               InternetSetStatusCallback(@pg.hConnect, 0)
               Say("Operation cancelled!")
               EXIT FUNCTION
             ELSE
               dwContext = 1
             END IF
             IF FtpGetFile(BYVAL @pg.hConnect, FileInfoArray(FileIndex).cFileName, zLocalPath & FileInfoArray(FileIndex).cFileName, %FALSE, _
                           %FILE_ATTRIBUTE_NORMAL, %FTP_TRANSFER_TYPE_BINARY OR %INTERNET_FLAG_RELOAD, dwContext) THEN 'Download was successfull
                           'IF dwContext = 0 THEN InternetStatusCallback won't be called
                Say("Downloaded successfully!")
              ELSE 'Download failed
                Say("Error on download!")
             END IF
           NEXT
           @pg.FtpState = 0
           Say(STRING$(100, "-"))
         ELSE
           Say("No file found with mask " & $DQ & zFtpMask & $DQ)
           Bye : EXIT FUNCTION
         END IF
    
         InternetSetStatusCallback(@pg.hConnect, BYVAL 0) 'Remove the existing callback
         InternetCloseHandle(@pg.hConnect) 'Free ftp handle
       ELSE
         Say("Unable to connect, might be wrong server name, user name, or password")
         Bye : EXIT FUNCTION
       END IF
       InternetCloseHandle(@pg.hSession) 'Free inet handle
     ELSE
       Say("Unable to open session!")
       Bye : EXIT FUNCTION
     END IF
    
     Say("Job all done...")
     Bye : EXIT FUNCTION
    
    END FUNCTION
    '______________________________________________________________________________
    
    CALLBACK FUNCTION DlgProc
     LOCAL  zLocalPath   AS ASCIIZ * %MAX_PATH
     STATIC sTextbox     AS STRING
     STATIC hTextbox     AS DWORD
     LOCAL  hThread      AS DWORD
     LOCAL  ClientWidth  AS LONG
     LOCAL  ClientHeight AS LONG
    
     SELECT CASE CBMSG
    
       CASE %WM_INITDIALOG
         hTextbox           = GetDlgItem(CBHNDL, %Textbox)
         @pg.ProgressBarMax = 1000
    
         CONTROL SET TEXT @pg.hDlg, %TextboxServer,    "ftp.server.net"
         CONTROL SET TEXT @pg.hDlg, %TextboxUser,      "UserName"
         CONTROL SET TEXT @pg.hDlg, %TextboxPassword,  "password"
         CONTROL SET TEXT @pg.hDlg, %TextboxFolder,    "ftpFolder"
         CONTROL SET TEXT @pg.hDlg, %TextboxMask,      "*.zip"
         CONTROL SET TEXT @pg.hDlg, %TextboxLocalPath, "C:\TmpFtp\"
    
         SendDlgItemMessage(@pg.hDlg, %ProgressbarGroup, %PBM_SETSTATE, %PBST_NORMAL, 0)
         SendDlgItemMessage(@pg.hDlg, %ProgressbarGroup, %PBM_SETRANGE32, 0, @pg.ProgressBarMax)
         SendDlgItemMessage(@pg.hDlg, %ProgressbarFile, %PBM_SETSTATE, %PBST_NORMAL, 0)
         SendDlgItemMessage(@pg.hDlg, %ProgressbarFile, %PBM_SETRANGE32, 0, @pg.ProgressBarMax)
    
       CASE %WM_COMMAND
         SELECT CASE CBCTL
    
           CASE %ButtonDownload
             IF (CBCTLMSG = %BN_CLICKED) OR (CBCTLMSG = 1) THEN
               @pg.BytesSent          = 0
               @pg.BytesSentTotal     = 0
               @pg.BytesRecieved      = 0
               @pg.BytesRecievedTotal = 0
               @pg.FilesSizeAdded     = 0
               @pg.CallbackExit       = %FALSE
               THREAD CREATE Download(%NULL) TO hThread
               THREAD CLOSE hThread TO hThread
             END IF
    
           CASE %ButtonCancel
             IF (CBCTLMSG = %BN_CLICKED) OR (CBCTLMSG = 1) THEN
               CONTROL ENABLE  @pg.hDlg, %ButtonDownload
               CONTROL ENABLE  @pg.hDlg, %ButtonClear
               @pg.CallbackExit = %TRUE
               InternetSetStatusCallback(@pg.hConnect, BYVAL 0) 'Remove the existing callback
               InternetCloseHandle(@pg.hConnect) 'Free ftp handle
               InternetCloseHandle(@pg.hSession) 'Free inet handle
             END IF
    
           CASE %ButtonExplore
             IF (CBCTLMSG = %BN_CLICKED) OR (CBCTLMSG = 1) THEN
               CONTROL GET TEXT @pg.hDlg, %TextboxLocalPath TO zLocalPath
               zLocalPath = TRIM$(zLocalPath)
               IF (GETATTR(zLocalPath) AND %FILE_ATTRIBUTE_DIRECTORY) <> %FILE_ATTRIBUTE_DIRECTORY THEN
                 Say("Local folder " & $DQ & zLocalPath & $DQ & "not created yet")
                 Bye
               ELSE
                 ShellExecute(%HWND_DESKTOP, "Explore", zLocalPath, "", "", %SW_SHOWNORMAL)
               END IF
             END IF
    
           CASE %ButtonClear
             IF (CBCTLMSG = %BN_CLICKED) OR (CBCTLMSG = 1) THEN
               IF SendDlgItemMessage(@pg.hDlg, %Textbox, %WM_GETTEXTLENGTH, 0, 0) THEN 'Textbox not empty
                 CONTROL GET TEXT @pg.hDlg, %Textbox TO sTextbox
                 CONTROL SET TEXT @pg.hDlg, %Textbox, ""
                 SendDlgItemMessage(@pg.hDlg, %ProgressbarGroup, %PBM_SETPOS, 0, 0)
                 SendDlgItemMessage(@pg.hDlg, %ProgressbarFile,  %PBM_SETPOS, 0, 0)
               ELSE 'Textbox empty
                 IF LEN(sTextbox) THEN
                   CONTROL SET TEXT @pg.hDlg, %Textbox, sTextbox
                   SendDlgItemMessage(@pg.hDlg, %ProgressbarGroup, %PBM_SETPOS, @pg.ProgressBarGroup, 0)
                   SendDlgItemMessage(@pg.hDlg, %ProgressbarFile,  %PBM_SETPOS, @pg.ProgressBarFile, 0)
                 END IF
               END IF
             END IF
    
         END SELECT
    
      END SELECT
    
    END FUNCTION
    '______________________________________________________________________________
    
    FUNCTION PBMAIN()
     LOCAL hIcon AS DWORD
     LOCAL g     AS GlobalVar
    
     pg = VARPTR(g) 'Une only one global
    
     DIALOG FONT "Segoe UI", 9
     DIALOG NEW %HWND_DESKTOP, $AppName, , , 350, 350, _
     %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU, 0 TO @pg.hDlg
    
     hIcon = LoadIcon(GetModuleHandle(BYVAL 0), BYVAL 101)
     SetClassLong(@pg.hDlg, %GCL_HICON, hIcon) 'Set an icon
     SendMessage(@pg.hDlg, %WM_SETICON, %ICON_SMALL, hIcon)
    
     CONTROL ADD LABEL, @pg.hDlg, %LabelServer,    "Server",        5, 05, 50, 11, %SS_RIGHT, %WS_EX_LEFT
     CONTROL ADD LABEL, @pg.hDlg, %LabelUser,      "User",          5, 19, 50, 11, %SS_RIGHT, %WS_EX_LEFT
     CONTROL ADD LABEL, @pg.hDlg, %LabelPassword,  "Password",      5, 33, 50, 11, %SS_RIGHT, %WS_EX_LEFT
     CONTROL ADD LABEL, @pg.hDlg, %LabelFolder,    "Server folder", 5, 47, 50, 11, %SS_RIGHT, %WS_EX_LEFT
     CONTROL ADD LABEL, @pg.hDlg, %LabelMask,      "File mask",     5, 61, 50, 11, %SS_RIGHT, %WS_EX_LEFT
     CONTROL ADD LABEL, @pg.hDlg, %LabelLocalPath, "Local folder",  5, 75, 50, 11, %SS_RIGHT, %WS_EX_LEFT
    
     CONTROL ADD TEXTBOX, @pg.hDlg, %TextboxServer,    "", 65, 05, 280, 12, %ES_LEFT OR %WS_TABSTOP, %WS_EX_LEFT OR %WS_EX_CLIENTEDGE
     CONTROL ADD TEXTBOX, @pg.hDlg, %TextboxUser,      "", 65, 19, 280, 12, %ES_LEFT OR %WS_TABSTOP, %WS_EX_LEFT OR %WS_EX_CLIENTEDGE
     CONTROL ADD TEXTBOX, @pg.hDlg, %TextboxPassword,  "", 65, 33, 280, 12, %ES_LEFT OR %WS_TABSTOP OR %ES_PASSWORD, %WS_EX_LEFT OR %WS_EX_CLIENTEDGE
     CONTROL ADD TEXTBOX, @pg.hDlg, %TextboxFolder,    "", 65, 47, 280, 12, %ES_LEFT OR %WS_TABSTOP, %WS_EX_LEFT OR %WS_EX_CLIENTEDGE
     CONTROL ADD TEXTBOX, @pg.hDlg, %TextboxMask,      "", 65, 61, 280, 12, %ES_LEFT OR %WS_TABSTOP, %WS_EX_LEFT OR %WS_EX_CLIENTEDGE
     CONTROL ADD TEXTBOX, @pg.hDlg, %TextboxLocalPath, "", 65, 75, 280, 12, %ES_LEFT OR %WS_TABSTOP, %WS_EX_LEFT OR %WS_EX_CLIENTEDGE
    
     CONTROL ADD BUTTON, @pg.hDlg, %ButtonDownload, "&Download",      030, 95, 50, 15
     CONTROL ADD BUTTON, @pg.hDlg, %ButtonCancel,   "&Cancel",        115, 95, 50, 15
     CONTROL ADD BUTTON, @pg.hDlg, %ButtonExplore,  "&Explore",       195, 95, 50, 15
     CONTROL ADD BUTTON, @pg.hDlg, %ButtonClear,    "&Clear textbox", 270, 95, 50, 15
    
     CONTROL ADD "MSCTLS_PROGRESS32", @pg.hDlg, %ProgressbarFile,  "", 5, 115, 340, 10, %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %WS_BORDER OR %WS_CHILD OR %PBS_SMOOTH
     CONTROL ADD "MSCTLS_PROGRESS32", @pg.hDlg, %ProgressbarGroup, "", 5, 130, 340, 10, %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %WS_BORDER OR %WS_CHILD OR %PBS_SMOOTH
    
     CONTROL ADD TEXTBOX, @pg.hDlg, %Textbox, "", 5, 145, 340, 200, %WS_CHILD OR %WS_VISIBLE OR _
     %WS_TABSTOP OR %WS_HSCROLL OR %WS_VSCROLL OR %ES_LEFT OR %ES_MULTILINE OR _
     %ES_READONLY OR %ES_AUTOHSCROLL OR %ES_AUTOVSCROLL OR %ES_NOHIDESEL OR %ES_WANTRETURN, _
     %WS_EX_CLIENTEDGE OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR
    
     DIALOG SHOW MODAL @pg.hDlg CALL DlgProc
    
     DestroyIcon(hIcon)
    
    END FUNCTION


  • #2
    Originally posted by Tim Lakinir View Post
    How to convert it to SFTP or Secure File Transfer Protocol ?
    Not sure if intentional, but SFTP is Secure File Transfer Protocol. As for your question: not possible (with WinInet.dll = this sample code), as WinInet doesn't support SFTP.

    And as usually at this point, I recommend to checkout Catalyst's SocketTools library. It will take care of your "all things internet" needs, including SFTP. Bonus point: it comes with PB declares and examples.

    Comment


    • #3
      Thanks Knuth
      Will test it out

      Comment


      • #4
        Also: Mike Stefanik of Catalyst is a member of this forum, so you can't get much closer to "information at your fingertips" than this.

        Comment


        • #5
          Hi Mike Stefanik

          Could you please help me to get started with Socket Tools by :

          1. Providing a working example in PB on SFTP a small file to a server

          2. Providing a URL to download an evaluation copy of relevant Socket Tools dll or sll
          in order to test out this PB program

          I need to test it out before escalating the matter to my supervisor to purchase this Socket Tools
          Thanks

          Comment


          • #6
            From one of the version 9.3 Socket Tools PDF files:
            Introduction

            The SocketTools Library Edition includes standard Windows dynamic link libraries (DLLs) which can be used
            in a wide variety of programming languages such as Visual C++, Visual C#, Visual Basic and Delphi. The
            Library Edition is ideal for the developer who requires the high performance, minimum resource utilization
            and flexibility of a lower level interface, without the inherent overhead of ActiveX components or the .NET
            Framework. The SocketTools Library Edition API has over 950 functions which can be used to develop
            applications that meet a wide range of needs. SocketTools covers it all, including uploading and
            downloading files, sending and retrieving email, remote command execution, terminal emulation, and
            much more.

            The SocketTools Library Edition also includes support for the industry standard Secure Sockets Layer (SSL),
            Transport Security Layer (TLS) and Secure Shell (SSH) protocols which are used to ensure that data
            exchanged between the local system and a server is secure and encrypted. The Library Edition implements
            the major secure protocols such as HTTPS, FTPS, SFTP, SSH, SMTPS, POP3S, IMAPS and more. Your data is
            protected with up to 256-bit encryption and full support for client certificates. Using the popular
            SocketWrench API that is part of SocketTools, you can also write your own, custom secure server
            applications. And there's no need for you to understand the details of certificate management, data
            encryption or how the security protocols work. All it takes is a few lines of code to enable the security
            features, and SocketTools handles the rest.

            I'm new to ST, and not well versed enough to provide you with code or guidance.

            And since this is not the 3rd Party forum, I must refrain from heaping justified high praise on this toolkit, and
            simply mention that it's helped me do things above my pay grade.

            -John

            Comment


            • #7
              Tim followed up with me directly, but if folks are interested, I can also post the code to do a simple SFTP file transfer in the 3rd party forum. Basically, SFTP support is part of the general FTP API that we have, it just involves some different options. If you have existing code using our API for FTP/FTPS, modifying it to use SFTP is fairly simple stuff.

              Edit:
              I just went ahead and posted an example in the 3rd party forum here, hopefully that's okay.
              Mike Stefanik
              sockettools.com

              Comment


              • #8
                Originally posted by John Montenigro View Post
                And since this is not the 3rd Party forum, I must refrain from heaping justified high praise on this toolkit, and
                simply mention that it's helped me do things above my pay grade.
                I'm pretty sure that if (satisfied) users of a PB related 3rd party product mentions it outside the 3rd party forum in an answer to a question of a fellow PB coder, no one in his right mind would object to that. IIRC it's only advertising (and sample code) of a product by the 3rd party, that should go into the 3rd party forum, exactly like Mike did above.

                Comment


                • #9
                  Yep Knuth, we will need to do some real testings on these socket tools to gauge its efficacies

                  What are the available socket tools that can be used in PB or those that you have experience using ?
                  As on its website, there is a wide range of products, I'm not sure which one is applicable for PB

                  For example, I believe that ActiveX products are no longer applicable while the DLL would be quite likely be used for PB?

                  Comment


                  • #10
                    Tim, I had sent you a link to the download page for the SocketTools edition you'd want to use. For PowerBASIC, it's the Library Edition.
                    Mike Stefanik
                    sockettools.com

                    Comment

                    Working...
                    X