Solved

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

Posted on 2007-04-11
9
849 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
Comment Utility
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
Comment Utility
Hi,
Do you mean changing the declaration to:
Private Const BINDF_GETNEWESTVERSION As Long = 0

Brian

0
 
LVL 3

Expert Comment

by:JipFromParis
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:bogorman
Comment Utility
Hi,
I now get PInvokeStackImbalance was detected
Brian
0
 
LVL 3

Accepted Solution

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

Author Comment

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

Author Comment

by:bogorman
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

728 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

12 Experts available now in Live!

Get 1:1 Help Now