Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

UrlDownloadToFile Progress


  • Please log in to reply
57 replies to this topic
Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

But I found the culprit. Using DllCall( "ole32\CoInitialize", UInt,0 ) does the trick, again
urlmon\URLDownloadToFile() also works now, with CoInitialize() added.

Good. BTW, why don't you move to AHK_L where it's done automatically?

In case I may forget in the future to adjust in the posted codes, I'd like to mention that my codes are all free of CoInitialize/OleInitialize and CoUninitialize/OleUninitialize. Another crucial one is LoadLibrary, in conjunction with DllCall, as once loaded dlls will never be automatically unloaded in my custom build.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

why don't you move to AHK_L


AHK_L is excellent, eventually I might have to move.. but not right now, as I believe Mr.Chris will update.
Some of us has to cater to noobs, I guess I will stay as one as long as I can.

.. and I forgot to say this: You are my Hero :D, Thanks.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
BTW, I suggest changing the DllCall to:
DllCall("urlmon\URLDownloadToFile" [color=red](A_IsUnicode ? "" : "A"),[/color] "Uint", 0, "str", sUrl, "str", sFile, "Uint", 0, "UintP", &vt)
in order for script to work with AHKLx32 too. It would be also good to fix loop offsets for x64 AHKL version.

but not right now, as I believe Mr.Chris will update.

Then don't switch, use both versions. The same code works in both with minimal effort.
BTW, even if Mr. Chris updates it will never be comparable to AHKL in the level of goodness (based on empirical observations). It would be the best for Mr Chris to unite forces with AHKL and create the best thing for everybody.
Posted Image

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

in order for script to work with AHKLx32 too.

Obviously it won't work for 64bit build unless Ptr is used. Anyway, being able to omit W/A suffix for any API's is another benefit of AHK_L, although it always requires a caution for the case like lstrcpy/lstrcpyA/lstrcpyW.

Guest_
  • Guests
  • Last active:
  • Joined: --

Dear Sean,

I have two questions about urlmon\URLDownloadToFile progress method

1) It returns the file from cache when available.. Can we force the download?
2) It does not download webpages
for eg: http://www.autohotke...pic.php?t=19475 (this topic)
or http://msdn.microsof...ibrary/ms775133
and returns 0x800401E4

Please help!


"1) It returns the file from cache when available.. Can we force the download?"
I'm not sure... ==> Wininet.dll/DeleteUrlCacheEntry(URL)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Wininet.dll/DeleteUrlCacheEntry(URL)


I had tried that.. unreliable.

sbc
  • Members
  • 321 posts
  • Last active: Jun 07 2011 10:24 AM
  • Joined: 25 Aug 2009
I'm trying to make it work with the AHK_L 64bit bulid and with the code below, if a huge file is specified, it stops downloading at 3472 bytes. But if it is run in Xp with the Ansi 32bit build of AHK it works fine. Could somebody take a look at it?
sUrl  := [color=#666666]"http://www.autohotkey.com/download/AutoHotkeyInstall.exe"[/color]									;this works
sUrl := [color=#666666]"https://ahknet.autohotkey.com/~Lexikos/AutoHotkey_L/AutoHotkey_L_Install.exe"[/color]					;this works
sUrl := [color=#666666]"http://mirror.as29550.net/releases.ubuntu.com/maverick/ubuntu-10.10-desktop-amd64.iso"[/color]		;this doesn't work
sFile := [color=brown]A_Temp[/color] . [color=#666666]"\AutoHotkeyInstall_L.exe"[/color]

[color=#107095]Progress[/color], % [color=#666666]"M W"[/color] . [color=brown]A_ScreenWidth[/color]//2, 0, 0 of 0
[color=#107095]VarSetCapacity[/color](vt, 4*11*([color=brown]A_IsUnicode[/color] ? 2 : 1)), nParam = 31132253353
[color=#107095]Loop[/color], Parse, nParam
   [color=#107095]NumPut[/color]([color=#107095]RegisterCallback[/color]([color=#666666]"DownloadProgress"[/color], [color=#666666]"Fast"[/color], [color=brown]A_LoopField[/color], [color=brown]A_Index[/color]-1), vt, 4*([color=brown]A_Index[/color]-1)*([color=brown]A_IsUnicode[/color] ? 2 : 1))
[color=#107095]DllCall[/color]([color=#666666]"urlmon\URLDownloadToFile"[/color] . ([color=brown]A_IsUnicode[/color] ? [color=#666666]""[/color] : [color=#666666]"A"[/color]), ([color=brown]A_PtrSize=8[/color] ? [color=#666666]"Ptr"[/color] : [color=#666666]"Uint"[/color]) , 0, [color=#666666]"Str"[/color], sUrl, [color=#666666]"Str"[/color], sFile, [color=#666666]"Uint"[/color], 0, [color=#666666]"UintP"[/color], &vt)	

[color=#107095]msgbox[/color] done

DownloadProgress(pthis, nProgress = 0, nProgressMax = 0, nStatusCode = 0, pStatusText = 0)
{
	[color=#107095]If[/color] [color=brown]A_EventInfo[/color] = 6
	[color=#107095]Progress[/color], % p := 100 * nProgress//nProgressMax, %p%, % nProgress . [color=#666666]" of "[/color] . nProgressMax
	[color=#107095]SetTimer[/color], RemoveProgressBar, -3000
	[color=#107095]Return[/color] 0
	RemoveProgressBar:
		[color=#107095]Progress[/color], Off
	[color=#107095]Return[/color]
}

Edit: Never mind, the previously created downloaded file with the same name must have been deleted.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

VarSetCapacity(vt, [color=darkred]4[/color]*11[color=red]*(A_IsUnicode ? 2 : 1)[/color]), nParam = 31132253353
Loop, Parse, nParam
   NumPut(RegisterCallback("DownloadProgress", "Fast", A_LoopField, A_Index-1), vt, [color=darkred]4[/color]*(A_Index-1)[color=red]*(A_IsUnicode ? 2 : 1)[/color])
These are not strings, so char size is irrelevant. Replace 4 with A_PtrSize or (A_PtrSize=8 ? 8:4) for compatibility. The end result is the same on Unicode x64 builds, but it should cause 32-bit Unicode builds to fail, at least in theory.

DllCall("urlmon\URLDownloadToFile" . (A_IsUnicode ? "" : "A"), (A_PtrSize=8 ? "Ptr" : "Uint") , 0, "Str", sUrl, "Str", sFile, "Uint", 0, "[color=red]Uint[/color]P", &vt)
The final parameter is a pointer to a pointer, so PtrP or UPtrP.

sbc
  • Members
  • 321 posts
  • Last active: Jun 07 2011 10:24 AM
  • Joined: 25 Aug 2009
That's so kind of you. Thanks Lexikos. :)

troyt
  • Guests
  • Last active:
  • Joined: --
DetectHiddenWindows, On

url := "http://fuller.zen.co.uk/test/100MB_nonzero.bin"
splitpath,url,ofn
name := ofn
download( url, name )

Return

Download( url, file ) {
  static _init
  global _cu
  Splitpath,file, _dFile
  if ! init
{ RegRead,SysNot,HKCU,AppEvents\Schemes\Apps\.Default\SystemNotification\.Current
  Transform, SysNot, deref, %sysnot%
  SysGet, m, MonitorWorkArea, 1
  y:=(mBottom-52-2),x:=(mRight-330-2),init:=1,VarSetCapacity(vt,4*11),nPar:="31132253353"
  Loop, Parse, nPar
     NumPut(RegisterCallback("DL_Progress","Fast",A_LoopField,A_Index-1),vt,4*(A_Index-1))
} VarSetCapacity(_cu,255),DllCall("shlwapi\PathCompactPathExA",Str,_cu,Str,url,UInt,50)
msgbox %_cu%
Progress,Hide CWE6E3E4 CT000020 CBF73D00 x%x% y%y% w330 h52 B1 FS8 WM700 WS400 FM8 ZH8 ZY3 C11,,%_cu%,AutoHotkeyProgress,Tahoma

  WinSet,Transparent,210,AutoHotkeyProgress
  SoundPlay, %SysNot%
  re:=DllCall("urlmon\URLDownloadToFileA", Uint,0, Str,url,Str,file,Uint,0,UintP,&vt)
  SoundPlay, %SysNot%
  Progress, Off
Return re=0 ? 1 : 0
}
DL_Progress( pthis, nP=0, nPMax=0, nSC=0, pST=0 ) {
  global _cu
  If (A_EventInfo=6)
{ Progress, Show
  Progress, % (P:=100*nP//nPMax),% "Downloading:     " Round(np/1024,1) " Kb / "
            .  Round(npmax/1024) " Kb    [ " p "`% ]",%_cu%
} Return 0
}

This seems to work OK, but is there anyway to get the file size of the file I'm downloading and add it to the progress window ?

troyt
  • Guests
  • Last active:
  • Joined: --
Ahh.. just found it's downloading the file from the cache !
How do I stop that ?

troyt
  • Guests
  • Last active:
  • Joined: --

Ahh.. just found it's downloading the file from the cache !
How do I stop that ?


Anyway to bypass / not read from the cache ?

troyt
  • Guests
  • Last active:
  • Joined: --

DetectHiddenWindows, On

url := "http://fuller.zen.co.uk/test/100MB_nonzero.bin"
splitpath,url,ofn
name := ofn
download( url, name )

Return

Download( url, file ) {
  static _init
  global _cu
  Splitpath,file, _dFile
  if ! init
{ RegRead,SysNot,HKCU,AppEvents\Schemes\Apps\.Default\SystemNotification\.Current
  Transform, SysNot, deref, %sysnot%
  SysGet, m, MonitorWorkArea, 1
  y:=(mBottom-52-2),x:=(mRight-330-2),init:=1,VarSetCapacity(vt,4*11),nPar:="31132253353"
  Loop, Parse, nPar
     NumPut(RegisterCallback("DL_Progress","Fast",A_LoopField,A_Index-1),vt,4*(A_Index-1))
} VarSetCapacity(_cu,255),DllCall("shlwapi\PathCompactPathExA",Str,_cu,Str,url,UInt,50)
msgbox %_cu%
Progress,Hide CWE6E3E4 CT000020 CBF73D00 x%x% y%y% w330 h52 B1 FS8 WM700 WS400 FM8 ZH8 ZY3 C11,,%_cu%,AutoHotkeyProgress,Tahoma

  WinSet,Transparent,210,AutoHotkeyProgress
  SoundPlay, %SysNot%
  re:=DllCall("urlmon\URLDownloadToFileA", Uint,0, Str,url,Str,file,Uint,0,UintP,&vt)
  SoundPlay, %SysNot%
  Progress, Off
Return re=0 ? 1 : 0
}
DL_Progress( pthis, nP=0, nPMax=0, nSC=0, pST=0 ) {
  global _cu
  If (A_EventInfo=6)
{ Progress, Show
  Progress, % (P:=100*nP//nPMax),% "Downloading:     " Round(np/1024,1) " Kb / "
            .  Round(npmax/1024) " Kb    [ " p "`% ]",%_cu%
} Return 0
}


I'm using this code and it seems to work, but I notice CPU Usage hits 50% and stays there..

Any idea what using the CPU and why it's not releasing it ?

Thanks :)

Got around the caching issue, I'll post my code, once this bit has been resolved.

troyt
  • Guests
  • Last active:
  • Joined: --
Anyone know why this is using 50% CPU on XP ?

Thanks

troyt
  • Guests
  • Last active:
  • Joined: --
sorry to ask again
but any idea why it pushes upto 50% and stays there ??

Are there any alternatives to this script, that still use ahk_basic ?

Thanks