Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2007-04-11
9
Medium Priority
?
882 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

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

Accepted Solution

by:
JipFromParis earned 1500 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
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…
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…
Suggested Courses

926 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