Results 1 to 5 of 5

Thread: URLDownloadToFile question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Question URLDownloadToFile question

    Hey All,

    I found this code on one of the threads...

    Code:
    Public Declare Function URLDownloadToFile Lib "urlmon" Alias _
        "URLDownloadToFileA" (ByVal pCaller As Long, _
        ByVal szURL As String, _
        ByVal szFileName As String, _
        ByVal dwReserved As Long, _
        ByVal lpfnCB As Long) As Long
    
    Public Function DownloadFile(URL As String, _
        LocalFilename As String) As Boolean
        Dim lngRetVal As Long
        lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
        If lngRetVal = 0 Then DownloadFile = True
    End Function

    I was wondering if anyone could find any problem with changing
    the last "IF" statement to something like the following...


    Code:
    If lngRetVal = 0 Then
       DownloadFile = True
       MsgBox "Download Complete!"
    Else
       DownloadFile = False
       MsgBox "You are NOT Connected to the Internet!"
    End If
    Thanks in advance,
    Ron

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I see no problem with it. Have you tried it?

  3. #3
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314
    Greetings and Salutatiions!


    Just wanted to drop a note to say thanks for the code sample. It was just what I was looking for.
    Steve Stunning

  4. #4
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Returns one of the following values:
    Private Const E_OUTOFMEMORY As Long = &H8007000E
    The buffer length is invalid or there was insufficient memory to complete the operation.
    Private Const S_OK As Long = &H0
    The operation succeeded.


    URLDownloadToFile will only return a non-zero value only if it doesn't have enough memory to do what it needs to.

    Try using InternetGetConnectedState to see if the user is online or not.

    VB Code:
    1. Private Const INTERNET_CONNECTION_CONFIGURED = &H40
    2. Private Const INTERNET_CONNECTION_LAN = &H2
    3. Private Const INTERNET_CONNECTION_MODEM = &H1
    4. Private Const INTERNET_CONNECTION_OFFLINE = &H20
    5. Private Const INTERNET_CONNECTION_PROXY = &H4
    6. Private Const INTERNET_RAS_INSTALLED = &H10
    7. Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long
    8. Private Sub Form_Load()
    9.     'KPD-Team 2001
    10.     'URL: [url]http://www.allapi.net/[/url]
    11.     'E-Mail: [email][email protected][/email]
    12.     Dim Ret As Long
    13.     Me.AutoRedraw = True
    14.     'retrieve the connection status
    15.     InternetGetConnectedState Ret, 0&
    16.     'show the result
    17.     If (Ret And INTERNET_CONNECTION_CONFIGURED) = INTERNET_CONNECTION_CONFIGURED Then Me.Print "Local system has a valid connection to the Internet, but it may or may not be currently connected."
    18.     If (Ret And INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN Then Me.Print "Local system uses a local area network to connect to the Internet."
    19.     If (Ret And INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM Then Me.Print "Local system uses a modem to connect to the Internet."
    20.     If (Ret And INTERNET_CONNECTION_OFFLINE) = INTERNET_CONNECTION_OFFLINE Then Me.Print "Local system is in offline mode."
    21.     If (Ret And INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY Then Me.Print "Local system uses a proxy server to connect to the Internet."
    22.     If (Ret And INTERNET_RAS_INSTALLED) = INTERNET_RAS_INSTALLED Then Me.Print "Local system has RAS installed."
    23. End Sub

  5. #5
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577

    Re: URLDownloadToFile question

    Originally posted by rdcody
    Hey All,

    I found this code on one of the threads...

    Code:
    Public Declare Function URLDownloadToFile Lib "urlmon" Alias _
        "URLDownloadToFileA" (ByVal pCaller As Long, _
        ByVal szURL As String, _
        ByVal szFileName As String, _
        ByVal dwReserved As Long, _
        ByVal lpfnCB As Long) As Long
    
    Public Function DownloadFile(URL As String, _
        LocalFilename As String) As Boolean
        Dim lngRetVal As Long
        lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
        If lngRetVal = 0 Then DownloadFile = True
    End Function

    I was wondering if anyone could find any problem with changing
    the last "IF" statement to something like the following...


    Code:
    If lngRetVal = 0 Then
       DownloadFile = True
       MsgBox "Download Complete!"
    Else
       DownloadFile = False
       MsgBox "You are NOT Connected to the Internet!"
    End If
    Thanks in advance,
    Ron
    The code is perfectly fine the way it is

    VB Code:
    1. If DownloadFile("http://www.vbforums.com/", "c:\a.txt") then
    2.  Msgbox "Download Complete"
    3. else
    4.  Mesgbox "There was some kind of error"
    5. End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width