Need a little code tweak

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • douglandmesser
    New Member
    • Feb 2008
    • 10

    Need a little code tweak

    Hey guys. I have this upload/download VB code that sort of works. When I run it, it will upload/download the file, but then it freezes and I need to close the app. Can anyone see why? I have the code below:

    START DECLARES HERE:
    [code=vb]
    Private Declare Function InternetOpen Lib "wininet.dl l" Alias "InternetOp enA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    Private Declare Function InternetConnect Lib "wininet.dl l" Alias "InternetConnec tA" (ByVal hInternetSessio n As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    Private Declare Function InternetCloseHa ndle Lib "wininet.dl l" (ByVal hInet As Long) As Integer
    Private Declare Function FtpPutFile Lib "wininet.dl l" Alias "FtpPutFile A" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
    Private Declare Function FtpGetFile Lib "wininet.dl l" Alias "FtpGetFile A" (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttri butes As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
    'END DECLARES HERE

    'START SUBS HERE:
    Private Sub Command1_Click( )
    lngINet = InternetOpen("M yFTP Control", 1, vbNullString, vbNullString, 0)
    lngINetConn = InternetConnect (lngINet, "ftp.servername .com", 0, "username", "password", 1, 0, 0)
    blnRC = FtpGetFile(lngI NetConn, "downloadme.txt ", "c:\downloadme. txt", 0, 0, 1, 0)
    'blnRC = FtpPutFile(lngI NetConn, "c:\uploadme.tx t", "uploadme.t xt", 1, 0)

    InternetCloseHa ndle lngINetConn
    InternetCloseHa ndle lngINet

    End Sub

    Private Sub Command2_Click( )
    lngINet = InternetOpen("M yFTP Control", 1, vbNullString, vbNullString, 0)
    lngINetConn = InternetConnect (lngINet, "ftp.servername .com", 0, "username", "password", 1, 0, 0)
    blnRC = FtpPutFile(lngI NetConn, "c:\uploadme.tx t", "uploadme.t xt", 1, 0)
    'blnRC = FtpGetFile(lngI NetConn, "downloadme.txt ", "c:\downloadme. txt", 0, 0, 1, 0)

    InternetCloseHa ndle lngINetConn
    InternetCloseHa ndle lngINet

    End Sub[/code]
    Basically one button uploads, and the other downloads a file. It will create the file, but nothing is in the file. Then it craps out and I need to terminate the whole program. Any guesses? Thanks in advance!
    Last edited by debasisdas; Apr 17 '08, 06:29 AM. Reason: added code=vb tags
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Which part of the code freezes tha application . Upload or Download ?

    Comment

    • douglandmesser
      New Member
      • Feb 2008
      • 10

      #3
      I'm sorry, I figured it out FINALLY! This took me forever to get going. The code is below:
      [code=vb]
      Const scUserAgent = "vb wininet"
      Const INTERNET_SERVIC E_FTP = 1
      Const INTERNET_OPEN_T YPE_DIRECT = 1
      Const INTERNET_FLAG_P ASSIVE = &H8000000
      Const FTP_TRANSFER_TY PE_BINARY = 0
      Const FILE_ATTRIBUTE_ ARCHIVE = &H20
      Private Declare Function InternetOpen Lib "wininet.dl l" Alias "InternetOp enA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
      Private Declare Function InternetConnect Lib "wininet.dl l" Alias "InternetConnec tA" (ByVal hInternetSessio n As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
      Private Declare Function FtpGetFile Lib "wininet.dl l" Alias "FtpGetFile A" (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttri butes As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
      Private Declare Function FtpPutFile Lib "wininet.dl l" Alias "FtpPutFile A" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
      Private Declare Function InternetCloseHa ndle Lib "wininet.dl l" (ByVal hInet As Long) As Integer


      Dim hOpen&, hConn&
      Dim lRes As Long
      hOpen = InternetOpen(sc UserAgent, INTERNET_OPEN_T YPE_DIRECT, vbNullString, vbNullString, 0)
      hConn = InternetConnect (hOpen, "ftp.thelwcf.co m", "21", "username", "password", INTERNET_SERVIC E_FTP, INTERNET_FLAG_P ASSIVE, 0)
      lRes = FtpPutFile(hCon n, "localfile" , "remotefile ", FTP_TRANSFER_TY PE_BINARY, 0&)
      InternetCloseHa ndle hConn
      InternetCloseHa ndle hOpen

      Dim hOpen&, hConn&
      Dim lRes As Long
      hOpen = InternetOpen(sc UserAgent, INTERNET_OPEN_T YPE_DIRECT, vbNullString, vbNullString, 0)
      hConn = InternetConnect (hOpen, "ftp.thelwcf.co m", "21", "username", "password", INTERNET_SERVIC E_FTP, INTERNET_FLAG_P ASSIVE, 0)
      lRes = FtpGetFile(hCon n, "remotefile ", "localfile" , False, FILE_ATTRIBUTE_ ARCHIVE, FTP_TRANSFER_TY PE_BINARY, 0&)
      InternetCloseHa ndle hConn
      InternetCloseHa ndle hOpen
      [/code]
      I guess my constants were off. It's working fast and perfectly now. Thanks anyway!
      Last edited by debasisdas; Apr 18 '08, 03:58 AM. Reason: added code=vb tags

      Comment

      Working...