Solved

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

Posted on 2007-04-11
9
860 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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 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