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
This is a preliminary/oversimplified version which will show a progress percentage of downloading of a web file.
It's written on request of our dear friend SKAN.

It was become possible due to the ingenious work by JGR, callback.dll, which is absolutely needed in the script.
Many thanks to him.

PS. I used DebugView here to display the percentage.
And, please keep in mind that this is only a test version, so there may be problems. Then, please inform me.

RegisterCallback version:
sUrl  := "http://www.autohotkey.com/download/AutoHotkeyInstall.exe"
sFile := A_Temp . "\AutoHotkeyInstall.exe"

Progress, % "M W" . A_ScreenWidth//2, 0, 0 of 0
VarSetCapacity(vt, 4*11), nParam = 31132253353
Loop, Parse, nParam
	NumPut(RegisterCallback("DownloadProgress", "Fast", A_LoopField, A_Index-1), vt, 4*(A_Index-1))
DllCall("urlmon\URLDownloadToFileA", "Uint", 0, "str", sUrl, "str", sFile, "Uint", 0, "UintP", &vt)

DownloadProgress(pthis, nProgress = 0, nProgressMax = 0, nStatusCode = 0, pStatusText = 0)
{
	If A_EventInfo = 6
	Progress, % p := 100 * nProgress//nProgressMax, %p%, % nProgress . " of " . nProgressMax
	Return 0
}
callback.dll version:
sUrl  := "http://download.sysinternals.com/Files/DebugView.zip"
sFile := "C:\DebugView.zip"

DetectHiddenWindows, On
Process, Exist
hAHK := WinExist("ahk_pid " . ErrorLevel)

OnMessage(WM_AHK_Callback := DllCall("RegisterWindowMessage", "str", "WM_AHK_Callback"), "WM_AHK_Callback")

VarSetCapacity(vt, 4*11), hModule := DllCall("LoadLibrary", "str", "callback"), nParams = 31132253353
Loop, Parse, nParams
	EncodeInteger(&vt + 4*(A_Index-1), hCallback%A_Index% := DllCall("callback\callbackit", "Uint", A_LoopField, "Uint", hAHK, "Uint", WM_AHK_Callback, "Uint", A_Index-1))

MsgBox, % DllCall("urlmon\URLDownloadToFileA", "Uint", 0, "str", sUrl, "str", sFile, "Uint", 0x10, "UintP", &vt) . "|" . ErrorLevel . "|" . A_LastError

Loop, 11
	DllCall("GlobalFree", "Uint", hCallback%A_Index%)
DllCall("FreeLibrary", "Uint", hModule)
ExitApp


WM_AHK_Callback(wParam, lParam)
{
	OutputDebug, % wParam <> 6 ? wParam : DecodeInteger(lParam+4)/DecodeInteger(lParam+8) * 100
	Return 0
}

DecodeInteger(ref, nSize = 4)
{
	DllCall("RtlMoveMemory", "int64P", val, "Uint", ref, "Uint", nSize)
	Return val
}

EncodeInteger(ref, val = 0, nSize = 4)
{
	DllCall("RtlMoveMemory", "Uint", ref, "int64P", val, "Uint", nSize)
}


BoBo¨
  • Guests
  • Last active:
  • Joined: --
I really like to see to be surrounded by proffessionals Posted Image.
Thx Sean & to all of you out there. Keep up your good work.
Much appreciated. Posted Image

:D Posted Image Have fun :D

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

I expected this, but not so fast.. :O
I am overwhelmed!.. Thank you

This looks like a dream. I will reply as soon as I wake-up.

:)

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
Thanks, BoBo and Skan. :D
I simplified the script a little.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Aah, the power of copy/paste...
I prefer this form:
sUrl  = http://www.autohotkey.com/download/AutoHotkeyInstall.exe
sFile = C:\AutoHotkeyInstall.exe

DetectHiddenWindows, On
Process, Exist
hAHK := WinExist("ahk_pid " . ErrorLevel)
Progress M W400 R1-100, -, Downloading, %A_ScriptName%

WM_AHK_Callback := DllCall("RegisterWindowMessage", "str", "WM_AHK_Callback")
OnMessage(WM_AHK_Callback, "WM_AHK_Callback")

hModule    := DllCall("LoadLibrary", "str", "callback")
vals = 31132253353
Loop 11
{
	i := A_Index - 1
	v := SubStr(vals, A_Index, 1)
	hCallback%i% := DllCall("callback\callbackit", "Uint", v, "Uint", hAHK, "Uint", WM_AHK_Callback, "Uint", i)
}

VarSetCapacity(vt, 4 *11)
Loop 11
{
	i := A_Index - 1
	EncodeInteger(&vt+ 4 * i, hCallback%i%)
}

MsgBox, % DllCall("urlmon\URLDownloadToFileA", "Uint", 0, "str", sUrl, "str", sFile, "Uint", 0x10, "UintP", &vt) . "|" . ErrorLevel "|" A_LastError

Loop 11
{
	i := A_Index - 1
	DllCall("GlobalFree", "Uint", hCallback%i%)
}
DllCall("FreeLibrary","Uint", hModule)
ExitApp


WM_AHK_Callback(wParam, lParam)
{
   p := wParam <> 6 ? wParam : DecodeInteger(lParam+4)/DecodeInteger(lParam+8) * 100
   Progress %p%, % Floor(p)
   Return 0
}
I added a progress bar, I first thought it didn't worked, as I expected to see the IE download progress bar...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
And this a another simplified mod of a part of PhiLhos script. *Not tested* Since I do not have callback.dll installed yet
vals = 31132253353

Loop, Parse, vals

   hCallback%A_Index% := DllCall("callback\callbackit", "Uint", A_LoopField, "Uint", hAHK, "Uint", WM_AHK_Callback, "Uint", A_Index - 1) 

VarSetCapacity(vt, 4 *11) 

Loop 11 

   EncodeInteger(&vt+ 4 * (A_Index - 1), hCallback%A_Index%) 

MsgBox, % DllCall("urlmon\URLDownloadToFileA", "Uint", 0, "str", sUrl, "str", sFile, "Uint", 0x10, "UintP", &vt) . "|" . ErrorLevel "|" A_LastError 

Loop 11 

   DllCall("GlobalFree", "Uint", hCallback%A_Index%) 


Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

new_noobie
  • Guests
  • Last active:
  • Joined: --
@PhiLho
Thanks for the working example. I have been following the "callback"
discussions here, and have not been able to get any of the examples
to work on Win98.

Your posted code (after I added XXXInteger subroutines) worked
very well :D

This forum would be alot less useful without your presence.
(and some other gurus also :) )

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Sean, :)

I tested your code and it does not work for me in Win 2k SP4 / Win XP SP2
I get the MsgBox with ErrorLevel:0xc0000005 and LastError:126

---------------------------
URL.ahk
---------------------------
|0xc0000005|126
---------------------------
OK
---------------------------
Ditto with PhiLho's code.

:roll:

JGR
  • Members
  • 59 posts
  • Last active: Feb 07 2012 08:49 PM
  • Joined: 15 Jun 2006
Put a pause command just before the DllCall, and post the contents of your variables.
Also check that you actually have the callback.dll and urlmon.dll

JGR

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

Check that you actually have the callback.dll


That was it! Long awaited, and glad to have this facility.
Thanks to you and Sean

:D

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

And this a another simplified mod of a part of PhiLhos script.

I like it. I updated the script.

AbsolutePressure
  • Members
  • 31 posts
  • Last active: Sep 25 2008 11:17 PM
  • Joined: 28 May 2006
I get an error on line10 :?:

This line does not contain a recognized action.



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

I get an error on line10 :?:

What build of AHK are you using currently?
I just noticed Chris updated AHK to 1.0.46.16 today, it can be a good time to update if you're using some older build.
And, don't forget to download callback.dll too, which is linked at the top post.

Helpy
  • Guests
  • Last active:
  • Joined: --
This usually means your version of AutoHotkey is too old. Sean uses some new facilities, like assignment as expression and multiple expression on one line.

AbsolutePressure
  • Members
  • 31 posts
  • Last active: Sep 25 2008 11:17 PM
  • Joined: 28 May 2006
Yes, I looked and I am using an old version. Sorry. I have 1.0.46.10, but I will update today. I am certainly not blaming anyone but myself for the mistake, but I would like to make a forum suggestion. The suggestion would be in having the scripts labeled in the version they were created. This might help ignorant post such as mine from having the wrong version.

Sorry Sean for posting the error off my own mistake, and thanks for sharing this script.