Solved

UrlDownloadToFile() works in Word macro but not in vb.net

Posted on 2007-04-11
9
858 Views
Last Modified: 2013-11-26
I am using urldownloadtofile() to download a file from the web to a local machine.  The coding works fine in a Word macro but does not work in vb.net. The strange thing is that it used to work and as far as I know I have not changed any of the settings. I now get
AccessVioationException was unhandled (attempted to read or write protected memory) on the line DownloadFile2 = URLDownloadToFile(0&, _    .....

The coding is:

Option Explicit On


Public Class Form1
    Private 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

    Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
       Alias "DeleteUrlCacheEntryA" _
      (ByVal lpszUrlName As String) As Long

    Private Const ERROR_SUCCESS As Long = 0
    Private Const BINDF_GETNEWESTVERSION As Long = &H10
    Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call DownloadFromWeb()
    End Sub


    Sub DownloadFromWeb()
        '
        ' DownloadFromWeb Macro
        ' Macro created 3/16/2007 by Brian O'Gorman
        '
        Dim sSourceUrl As String
        Dim sLocalFile As String


        sSourceUrl = "http://63.134.211.113/TestText.csv"
        sLocalFile = "c:\Web Design\LIFEnet\TestText.csv"

        'Label1.Caption = sSourceUrl
        'Label2.Caption = sLocalFile

        'Attempt to delete any cached version of
        'the file. Since we're only interested in
        'nuking the file, the routine is called as
        'a sub. If the return value is requires
        '(calling as a function), DeleteUrlCacheEntry
        'returns 1 if successful, or 0 otherwise, e.g.
        '  If DeleteUrlCacheEntry(sourceUrl) = 1 Then
        '     Debug.Print "cached file found and deleted"
        '  Else
        '     Debug.Print "no cached file for " & sourceUrl
        '  End If
        'Note that the remote URL is passed as this is the
        'name the cached file is known by. This does NOT
        'delete the file from the remote server.
        Call DeleteUrlCacheEntry(sSourceUrl)

        If DownloadFile2(sSourceUrl, sLocalFile) = True Then

           

        End If

    End Sub


    Private Function DownloadFile2(ByVal sSourceUrl As String, _
    ByVal sLocalFile As String) As Boolean

        'Download the file. BINDF_GETNEWESTVERSION forces
        'the API to download from the specified source.
        'Passing 0& as dwReserved causes the locally-cached
        'copy to be downloaded, if available. If the API
        'returns ERROR_SUCCESS (0), DownloadFile returns True.
        DownloadFile2 = URLDownloadToFile(0&, _
                                         sSourceUrl, _
                                         sLocalFile, _
                                         BINDF_GETNEWESTVERSION, _
                                         0&) = ERROR_SUCCESS

    End Function
End Class

Would be grateful for help on this.
Brian



                                       
0
Comment
Question by:bogorman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18889384
Strangely, any documentation, including MSDN2 web site state that the 4th parameter for URLDownloadToFile should be 0. Please, first make your code conforming with documentation, then try again an let me know if you are still in trouble.
0
 

Author Comment

by:bogorman
ID: 18889408
Hi,
Do you mean changing the declaration to:
Private Const BINDF_GETNEWESTVERSION As Long = 0

Brian

0
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18889502
Not exactly. The declaration is fine, however using BINDF_GETNEWESTVERSION as the 4th parameter doesn't conform to the documentation. Fix suggestion is :

DownloadFile2 = URLDownloadToFile(0&, _
                                         sSourceUrl, _
                                         sLocalFile, _
                                         0&, _
                                         0&) = ERROR_SUCCESS
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:bogorman
ID: 18889941
Hi,
I now get PInvokeStackImbalance was detected
Brian
0
 
LVL 3

Accepted Solution

by:
JipFromParis earned 500 total points
ID: 18890163
Much better ;-) This is a problem with the way Interop functions is declared. I will check later.
0
 

Author Comment

by:bogorman
ID: 19133523
Hi JipFromParis,
Di you ever find out anything more about the causes of this problem?
Regards
Brian
0
 

Author Comment

by:bogorman
ID: 19332263
Hi,
Although JipFromParis did not complete the answer to the question he did try, so I have assigned the points to him
Brian
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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