urldownloadtofile
-
Friday, April 06, 2012 3:03 PM
I have used urldownloaddtofile to fiel successfully in VBA in access2010 to download a TEXT list from a PHP/mysql website.
however, now I am using VSutdio2010 (VB.net) and trying to use webclient. This works fin when accessing various pages and I can set the webclient.credentials correctly, becauseI can download the admin page correctly. Before with URLDownloadtofile, I was passing the user/password intot the url address, and it wass working. However ONLy when tryng to get the lists, I get a blank file with message "Invalid Username/Password or no list download permission: ||"
my previous code in VBA was:
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 Longand calling : (*where serverip=ip address") as you can see - I was first downloading the admin page to make sure I have logged on to the server then I fteh the TEXT list.
completeserver = username & ":" & password & "@" & serverip
LISTT = pathonForm & "LIBRARY.HTM"
returnValue = URLDownloadToFile(0, "http://" & completeserver & "/admin.php", _
"c:\users\v.jhurry\admin.htm", 0, 0)
If ID = "listtable" Then
'http://172.16.0.252/goautodial-admin/admin.php?ADD=100
returnValue = URLDownloadToFile(0, "http://" & completeserver & "/goautodial-admin/admin.php?ADD=100", _
LISTT, 0, 0)returnValue = URLDownloadToFile(0, "http://" & serverip & "/goautodial-admin/list_download.php?list_id=" & ID, _
path0, 0, 0)NOW: on VS2010 , when I use webclient, this function:
Function downloadfile(ByVal zurl As String, ByVal saveas As String)
Dim xurl As New System.Net.WebClient
Dim f2 As New FileIOPermission(FileIOPermissionAccess.Read, "C:\test_r")
f2.AddPathList(FileIOPermissionAccess.Write Or FileIOPermissionAccess.Read, "C:\users\v.jhurry\download\test.txt")
f2.AddPathList(FileIOPermissionAccess.Write Or FileIOPermissionAccess.Read, "C:\users\v.jhurry\download\admin.htm")
f2.AddPathList(FileIOPermissionAccess.Write Or FileIOPermissionAccess.Read, "c:\users\v.jhurry\download\rlist.txt")f2.AddPathList(FileIOPermissionAccess.Write Or FileIOPermissionAccess.Read, saveas)
Try
f2.Demand()
Catch s As SecurityException
Console.WriteLine(s.Message)
End Try
xurl.Credentials = New System.Net.NetworkCredential("gael", "adminqua")
'xurl.DownloadFile("http://172.16.0.252/goautodial-admin/admin.php", "c:\users\v.jhurry\admin.htm")
xurl.DownloadFile(zurl, saveas)
'xurl.DownloadFile("http://172.16.0.252/goautodial-admin/admin.php?ADD=311&list_id=10110
'file:///C:/Users/v.jhurry/list_download.php?list_id=10110
'xurl.DownloadFile(zurl, saveas)' Catch ex As Exception
'Console.WriteLine("sfkjvh", ex.ToString)
Return (True)'End Try
End FunctionI GET blank textfile as mentioned above.
Now I try to re-use the URLdownloadtofile function and I get
A call to PInvoke function 'WindowsApplication1!WindowsApplication1.ListsManagement::URLDownloadToFile' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
This is my problem, either help me with webclient to get the file, or how to solve the Pinvoke unbalanced stack problem. I have read the notes concerning this problem and I cannot figure out how to correct this.
Regards.
All Replies
-
Friday, April 06, 2012 4:43 PMModerator
Hi,
I would suggest to ask such a question inside a Visual Basic forum. This forum covers the developer documentation and MS Help System only.
I was unable to understand fully the main problem you described first but the problem at the end simply means, that you added a dllimport for a function but your function signature is wrong. Maybe you can give us the DLLImport and how you are using so we can check it.
A good resource for pinvoke signatures is http://pinvoke.net - but when I checked your function I saw, that the page does not contain the VB definition so far:
http://pinvoke.net/default.aspx/urlmon/URLDownloadToFile.htmlBut I am sure, that the people inside a VB forum will be able to translate the C# signature to a VB signature.
With kind regards,
Konrad
-
Tuesday, April 10, 2012 8:22 AM
I must tell you that your response is excellent and I have been able to be routed to the real solutions.
I thank you very much and sorry for pouring this item into a wrong forum.
Best regards,
Vikram Jhurry -
Tuesday, April 10, 2012 8:56 AMModerator
Hi Vikram,
I am happy that I was able to help you and you are really welcome inside the MSDN and Partner forums. Finding the best forum can always be a task that could be quite hard at first and the main concern is not, that we get annoyed or so. In other forums your question is simply read by more specialists so yout chance get an answer quickly is much higher.
I see, that you are also a Microsoft Partner. So maybe you also want to have a look at the Microsoft Partner Forums. You can find the partner forums for Developers at http://social.microsoft.com/Forums/en-US/category/partnerdev.
With kind regards,
Konrad
-
Friday, June 01, 2012 6:36 AM
Finally IT WORKED:
Steps: 1. Make Class DownloadFileThread where DLL is imported
Steps: 2. Call the Function from anywhere
CLASS:
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.IOPublic Class DownloadFileThread
<DllImport("urlmon.dll", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Unicode, EntryPoint:="URLDownloadToFileW", ExactSpelling:=True, SetLastError:=True)> _
Public Shared Function urldownloadtofile(ByVal pCaller As Integer, ByVal szurl As String, ByVal szfilename As String, ByVal reserved As Integer, ByVal callback As Integer) As IntegerEnd Function
End Class
CALL FUNCTION: DownloadFileThread.urldownloadtofile(0, urlStr1, fileToStoreStr, 0, 0)
DownloadFileThread.urldownloadtofile(0, urlStr2, fileToStoreStr, 0, 0)In C# it works very fine as well.
- Marked As Answer by vikramjhurry Monday, June 04, 2012 9:14 AM

