Call to WinInet InternetOpen Works on Emulator and Sample App but not in My App

Call to WinInet InternetOpen Works on Emulator and Sample App but not in My App

Post by David Morga » Wed, 09 Jan 2002 18:33:24



Hello

In my eVB Application I have a requirement to connect to an FTP Server and download a file.  I started with the 'FTP Put' sample available from Microsoft, which works fine in the Emulator and my iPAQ H3600.

When I copy the various API calls and calling subroutines to my application the InternetOpen call fails.  When checking GetLastError I was getting 87 which I believe points to 'invalid argument'.  I then copied the code to a seperate module and now I get 'Unable to load library wininet needed for call InternetOpen.'  I think this might be a different problem than error 87 but I am stumped.  My colleague logged a problem like this in June 2000 to which there was no response except 'I gave up using WinInet'!

My problem is that I don't know where to go next.

The code from my module is below.  It is the call to InitilizeFTP that fails.

Many thanks

David Morgan

--------------------------------------------------------------------------------

Option Explicit

Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Public Const INTERNET_OPEN_TYPE_DIRECT = 1
Public Const INTERNET_OPEN_TYPE_PROXY = 3
Public Const INTERNET_FLAG_RELOAD = &H80000000

Public Const FTP_TRANSFER_TYPE_UNKNOWN As Long = 0
Public Const FTP_TRANSFER_TYPE_ASCII As Long = 1
Public Const FTP_TRANSFER_TYPE_BINARY As Long = 2

Public Const INTERNET_DEFAULT_FTP_PORT As Long = 21
Public Const INTERNET_FLAG_PASSIVE As Long = &H8000000
Public Const INTERNET_SERVICE_FTP As Long = 1

Declare Function FtpPutFile Lib "wininet" Alias "FtpPutFileW" ( _
    ByVal hFtp As Long, _
    ByVal lpszLocalFile As String, _
    ByVal lpszNewRemoteFile As String, _
    ByVal dwFlags As Long, _
    ByVal dwContext As Long) As Long

Declare Function FtpGetFile Lib "wininet" Alias "FtpGetFileW" ( _
    ByVal hConnect As Long, _
    ByVal lpszRemoteFile As String, _
    ByVal lpszNewFile As String, _
    ByVal fFailIfExists As Boolean, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal dwFlags As Long, _
    ByVal dwContext As Long) As Boolean

Declare Function InternetCloseHandle Lib "wininet" ( _
    ByVal hInet As Long) As Long

Declare Function InternetConnect Lib "wininet" Alias "InternetConnectW" ( _
    ByVal hInet As Long, _
    ByVal lpszServerName As String, _
    ByVal nServerPort As Long, _
    ByVal lpszUsername As String, _
    ByVal lpszPassword As String, _
    ByVal dwService As Long, _
    ByVal dwFlags As Long, _
    ByVal dwContext As Long) As Long

Declare Function InternetOpen Lib "wininet" Alias "InternetOpenW" ( _
    ByVal lpszAgent As String, _
    ByVal dwAccessType As Long, _
    ByVal lpszProxyName As String, _
    ByVal lpszProxyBypass As String, _
    ByVal dwFlags As Long) As Long

Public lngInternetHandle As Long
Public lngFtpHandle As Long
Public hOpenUrl As Long

Public Function InitializeFTP() As Boolean

    On Error Resume Next
    Dim sUrl As String

    lngInternetHandle = InternetOpen("eVB OpenUrl", _
        INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    If Err.Number <> 0 Or lngInternetHandle = 0 Then
        MsgBox "Failed to initialize communications to transfer data (InternetOpen)" & _
            vbCrLf & Err.Description
        InitializeFTP = False
    Else
        InitializeFTP = True
    End If

End Function

Public Function ConnectToFTPServer(ByVal strFTPServerName As String, _
    ByVal strUsername As String, ByVal strPassword As String) As Boolean

    On Error Resume Next

    lngFtpHandle = InternetConnect(lngInternetHandle, strFTPServerName, _
        INTERNET_DEFAULT_FTP_PORT, strUsername, strPassword, _
        INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0)

    If lngFtpHandle = 0 Or Err.Number <> 0 Then
        MsgBox "Failed to connect to server to transfer data. (InternetConnect) Error: " & _
            CStr(GetLastError)
        ConnectToFTPServer = False
    Else
        ConnectToFTPServer = True
    End If

End Function

Public Function PutFileOnFTPServer(ByVal strSourceFilename As String, _
    ByVal strDestFilename As String, _
    ByVal blnBinaryFile As String) As Boolean

    On Error Resume Next

    Dim blnTransferredOK  As Boolean
    Dim lngTransferType As Long

    If blnBinaryFile Then
        lngTransferType = FTP_TRANSFER_TYPE_BINARY
    Else
        lngTransferType = FTP_TRANSFER_TYPE_ASCII
    End If

    blnTransferredOK = FtpPutFile(lngFtpHandle, strSourceFilename, _
        strDestFilename, lngTransferType, 0)

    If blnTransferredOK = 0 Then
        MsgBox "Transfer of file " & strSourceFilename & _
            " to server failed. (FtpPutFile) Error: " & CStr(GetLastError())
    Else
        MsgBox "Transfer of file" & strSourceFilename & " succeeded!"
    End If
    PutFileOnFTPServer = blnTransferredOK

End Function

Function GetFileOffFTPServer(strRemoteFile As String, _
    ByVal strLocalFile As String, _
    ByVal blnFailIfExists As Boolean) As Boolean

    On Error Resume Next

    Dim blnTransferredOK As Boolean

    blnTransferredOK = FtpGetFile(lngFtpHandle, strRemoteFile, strLocalFile, blnFailIfExists, 0, FTP_TRANSFER_TYPE_BINARY + INTERNET_FLAG_RELOAD, 0)

    If blnTransferredOK = 0 Then
        MsgBox "Transfer of file " & strSourceFilename & _
            " from server failed. (FtpGetFile) Error: " & CStr(GetLastError())
    Else
        MsgBox "Transfer of file" & strRemoteFile & " succeeded!"
    End If
    GetFileOffFTPServer = blnTransferredOK
End Function

Public Sub CloseFTP()

    On Error Resume Next

    InternetCloseHandle lngFtpHandle
    InternetCloseHandle lngInternetHandle

End Sub

--------------------------------------------------------------------------------

 
 
 

Call to WinInet InternetOpen Works on Emulator and Sample App but not in My App

Post by David Morga » Wed, 09 Jan 2002 19:09:04


Okay, so on further investigation I see that using GetLastError, it is still Error 87 being returned, it was just a difference in my error handler.  Interestingly the source of the error as per Err.Source is Language Extensions and Err.Number 5.


  Hello

  In my eVB Application I have a requirement to connect to an FTP Server and download a file.  I started with the 'FTP Put' sample available from Microsoft, which works fine in the Emulator and my iPAQ H3600.

  When I copy the various API calls and calling subroutines to my application the InternetOpen call fails.  When checking GetLastError I was getting 87 which I believe points to 'invalid argument'.  I then copied the code to a seperate module and now I get 'Unable to load library wininet needed for call InternetOpen.'  I think this might be a different problem than error 87 but I am stumped.  My colleague logged a problem like this in June 2000 to which there was no response except 'I gave up using WinInet'!

  My problem is that I don't know where to go next.

  The code from my module is below.  It is the call to InitilizeFTP that fails.

  Many thanks

  David Morgan

------------------------------------------------------------------------------

  Option Explicit

  Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
  Public Const INTERNET_OPEN_TYPE_DIRECT = 1
  Public Const INTERNET_OPEN_TYPE_PROXY = 3
  Public Const INTERNET_FLAG_RELOAD = &H80000000

  Public Const FTP_TRANSFER_TYPE_UNKNOWN As Long = 0
  Public Const FTP_TRANSFER_TYPE_ASCII As Long = 1
  Public Const FTP_TRANSFER_TYPE_BINARY As Long = 2

  Public Const INTERNET_DEFAULT_FTP_PORT As Long = 21
  Public Const INTERNET_FLAG_PASSIVE As Long = &H8000000
  Public Const INTERNET_SERVICE_FTP As Long = 1

  Declare Function FtpPutFile Lib "wininet" Alias "FtpPutFileW" ( _
      ByVal hFtp As Long, _
      ByVal lpszLocalFile As String, _
      ByVal lpszNewRemoteFile As String, _
      ByVal dwFlags As Long, _
      ByVal dwContext As Long) As Long

  Declare Function FtpGetFile Lib "wininet" Alias "FtpGetFileW" ( _
      ByVal hConnect As Long, _
      ByVal lpszRemoteFile As String, _
      ByVal lpszNewFile As String, _
      ByVal fFailIfExists As Boolean, _
      ByVal dwFlagsAndAttributes As Long, _
      ByVal dwFlags As Long, _
      ByVal dwContext As Long) As Boolean

  Declare Function InternetCloseHandle Lib "wininet" ( _
      ByVal hInet As Long) As Long

  Declare Function InternetConnect Lib "wininet" Alias "InternetConnectW" ( _
      ByVal hInet As Long, _
      ByVal lpszServerName As String, _
      ByVal nServerPort As Long, _
      ByVal lpszUsername As String, _
      ByVal lpszPassword As String, _
      ByVal dwService As Long, _
      ByVal dwFlags As Long, _
      ByVal dwContext As Long) As Long

  Declare Function InternetOpen Lib "wininet" Alias "InternetOpenW" ( _
      ByVal lpszAgent As String, _
      ByVal dwAccessType As Long, _
      ByVal lpszProxyName As String, _
      ByVal lpszProxyBypass As String, _
      ByVal dwFlags As Long) As Long

  Public lngInternetHandle As Long
  Public lngFtpHandle As Long
  Public hOpenUrl As Long

  Public Function InitializeFTP() As Boolean

      On Error Resume Next
      Dim sUrl As String

      lngInternetHandle = InternetOpen("eVB OpenUrl", _
          INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
      If Err.Number <> 0 Or lngInternetHandle = 0 Then
          MsgBox "Failed to initialize communications to transfer data (InternetOpen)" & _
              vbCrLf & Err.Description
          InitializeFTP = False
      Else
          InitializeFTP = True
      End If

  End Function

  Public Function ConnectToFTPServer(ByVal strFTPServerName As String, _
      ByVal strUsername As String, ByVal strPassword As String) As Boolean

      On Error Resume Next

      lngFtpHandle = InternetConnect(lngInternetHandle, strFTPServerName, _
          INTERNET_DEFAULT_FTP_PORT, strUsername, strPassword, _
          INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0)

      If lngFtpHandle = 0 Or Err.Number <> 0 Then
          MsgBox "Failed to connect to server to transfer data. (InternetConnect) Error: " & _
              CStr(GetLastError)
          ConnectToFTPServer = False
      Else
          ConnectToFTPServer = True
      End If

  End Function

  Public Function PutFileOnFTPServer(ByVal strSourceFilename As String, _
      ByVal strDestFilename As String, _
      ByVal blnBinaryFile As String) As Boolean

      On Error Resume Next

      Dim blnTransferredOK  As Boolean
      Dim lngTransferType As Long

      If blnBinaryFile Then
          lngTransferType = FTP_TRANSFER_TYPE_BINARY
      Else
          lngTransferType = FTP_TRANSFER_TYPE_ASCII
      End If

      blnTransferredOK = FtpPutFile(lngFtpHandle, strSourceFilename, _
          strDestFilename, lngTransferType, 0)

      If blnTransferredOK = 0 Then
          MsgBox "Transfer of file " & strSourceFilename & _
              " to server failed. (FtpPutFile) Error: " & CStr(GetLastError())
      Else
          MsgBox "Transfer of file" & strSourceFilename & " succeeded!"
      End If
      PutFileOnFTPServer = blnTransferredOK

  End Function

  Function GetFileOffFTPServer(strRemoteFile As String, _
      ByVal strLocalFile As String, _
      ByVal blnFailIfExists As Boolean) As Boolean

      On Error Resume Next

      Dim blnTransferredOK As Boolean

      blnTransferredOK = FtpGetFile(lngFtpHandle, strRemoteFile, strLocalFile, blnFailIfExists, 0, FTP_TRANSFER_TYPE_BINARY + INTERNET_FLAG_RELOAD, 0)

      If blnTransferredOK = 0 Then
          MsgBox "Transfer of file " & strSourceFilename & _
              " from server failed. (FtpGetFile) Error: " & CStr(GetLastError())
      Else
          MsgBox "Transfer of file" & strRemoteFile & " succeeded!"
      End If
      GetFileOffFTPServer = blnTransferredOK
  End Function

  Public Sub CloseFTP()

      On Error Resume Next

      InternetCloseHandle lngFtpHandle
      InternetCloseHandle lngInternetHandle

  End Sub

------------------------------------------------------------------------------

 
 
 

1. IE6 and basic internet apps work, most 3rd party apps do not.

IE6, all communication tools such as instant messenger
and E-mail can connect to the internet fine.  Other
programs such as the Opera web browser (which I was
testing) and many smaller utilites auto-update features,
including installing Apple Quick time, cannot connect to
the internet.  They act the same way programs do if there
was not an active connection of any kind.

Any suggestions?

Thank you

--- Jeremy

2. Chat won't work

3. Java app calling Java app through command.com loses env vars

4. WMP7 & Adaptec EZ CD Creator

5. App Works on HPC emulator but not device.

6. WMP 7

7. SHCreateMenuBar fails with LE=120="call not impl" when app called from file explorer

8. connecting to internet

9. Mac: wxTimer not called when app not active?

10. Problems including a lib in a SDI app that works perfectly in a dialog based app

11. Has anyone out there got the sample Batcher VB app to work ?

12. Has ANYONE gotten the format sdk audioplayer sample app to work with DRM?

13. wininet call through emulator