[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 768
  • Last Modified:

InternetReadFile very slow on XP

I have a program the uses the wininet.dll for FTP.  My program is syncronizing the contents of a directory on a remote PC with an inhouse WinNT 4.0 FTP server.  Nothing fancy.

I use FtpPutFile and FtpGetFile to move whole files between computers and I use InternetReadFile to read the contents of smaller files that contain status information on each end.  I have used this program for close to 3 years without any problems.  However, within the last few months, I have been getting reports of long, long pauses in the process.  My log files indicate it is always during the call to "InternetReadFile".

One instance reads 10 bytes from a file on the remote end.  It looks like it locks up, but will eventually complete the read and be successful.  However, it takes almost 2 minutes!  It is almost like a reverse DNS timeout, but for the life of me can't imagine "InternetReadFile" doing any sort of DNS hit (?)

Calls to FtpPutFile and FtpGetFile work fine (i.e. a 400K file moves in 5-10 seconds or so on same connection)

All the machines so far are "new" and have either XP Home or Pro.  I've never seen it on 98,me or 2K, although I don't have many in the field any more...

Any thoughts?
  • 2
  • 2
1 Solution
Are you using InternetReadFile to read the file directly and obtain strings in text documents, or are you using it to download a file to the hard drive?
andersen58Author Commented:
I am using it to read a string from a text document.  The remote file is "updtdate.txt" and is just one line - the string of "05/04/2006"  (no quotes).  I read 10 bytes using InternetReadFile into a string variable (buffer).  It just takes 1-2 minutes on a few machines??
Okay, try this, I tried to make this about as fast as it could possible get.. If you experience lag then its probrably not due to the API itself but from the internet connection or something on the server end.

Option Explicit

Private Type InternetString
    MyData As String
End Type

Private Declare Function InternetOpenW Lib "wininet" ( _
    ByVal sAgent As Long, _
    ByVal lAccessType As Long, _
    ByVal sProxyName As Long, _
    ByVal sProxyBypass As Long, _
    ByVal lFlags As Long) As Long

Private Declare Function InternetOpenUrlW Lib "wininet" ( _
    ByVal hInternetSession As Long, _
    ByVal lpszUrl As Long, _
    ByVal lpszHeaders As Long, _
    ByVal dwHeadersLength As Long, _
    ByVal dwFlags As Long, _
    ByVal dwContext As Long) As Long

Private Declare Function InternetReadFile Lib "wininet" ( _
    ByVal hFile As Long, _
    ByVal lpBuffer As Long, _
    ByVal dwNumberOfBytesToRead As Long, _
    lpdwNumberOfBytesRead As Long) As Long
Private Declare Sub RtlMoveMemory Lib "kernel32" ( _
        Destination As Any, _
        Source As Any, _
        ByVal Length As Long)

Private Declare Sub RtlZeroMemory Lib "Kernel32.dll" ( _
        ByRef Destination As Any, _
        ByVal Length As Long)
Private Declare Function InternetCloseHandle Lib "wininet" ( _
    ByVal hInet As Long) As Long

Private Const DONT_CACHE As Long = &H4000000

Dim m_b()       As Byte
Dim IOpen       As Long
Dim ISession    As Long
Dim Dum         As Long

Private Function ReadStringFromUrl( _
    ByVal URL As String, _
    Optional nBytes As Long = 1024) As InternetString
    RtlZeroMemory ReadStringFromUrl.MyData, _
    IOpen = InternetOpenW(0, 0, 0, 0, 0)
    ISession = InternetOpenUrlW(IOpen, _
        StrPtr(URL), 0&, 0&, _
        DONT_CACHE, 0&)
    ReDim m_b(nBytes)
    ReadStringFromUrl.MyData = Space$(nBytes)
    If InternetReadFile(ISession, _
        ByVal VarPtr(m_b(0)), nBytes, Dum) Then
       RtlMoveMemory ByVal ReadStringFromUrl.MyData, _
        m_b(0), _
        nBytes + 1
        Debug.Print "InternetReadFile Failed."
    End If
    Erase m_b
    InternetCloseHandle ISession
    InternetCloseHandle IOpen
End Function

Private Sub Command1_Click()
   MsgBox ReadStringFromUrl("http://www.craphound.com/msftdrm.txt", 10).MyData
End Sub
andersen58Author Commented:
The above code works fine.  However, the actual problem ended up being
an issue with ZoneAlarm.  I have no idea why, but when ZoneAlarm pops up
it's "Allow or Disallow" screen and you choose to allow the program access,
it WILL let FTPGetFile and FTPPutFile commands to work, but InternetReadFile
is still getting block/delayed...

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now