InternetReadFile very slow on XP

Posted on 2006-05-17
Last Modified: 2009-12-16
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?
Question by:andersen58
    LVL 29

    Expert Comment

    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?

    Author Comment

    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??
    LVL 29

    Accepted Solution

    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("", 10).MyData
    End Sub

    Author Comment

    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...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Title # Comments Views Activity
    Swap listbox items in VB6. 4 37
    use like in If/Then vba 4 43
    Problem to skip loop 6 40
    VBA open file from excel cell 4 29
    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now