 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
bmcclure
Joined: 24 Nov 2007 Posts: 774
|
Posted: Sun Dec 23, 2007 5:19 am Post subject: |
|
|
This function is a slightly modified version of Sean's original RegisterCallback version. The problem is the function never ends and just hands indefinitely.
The file downloads successfully, the download window closes successfully, but something never ends.
| Code: | SW_Download(pUrl,pFile,pTitle="",pGuiNum="") {
global
If pGuiNum {
GuiNum = %pGuiNum%:
Gui, %GuiNum%Default
}
If Not pTitle
pTitle := SubStr(pFile,InStr(pFile,"\","-1")+1)
thisDownloadSW := SW_Create("Downloading " . pTitle,300,125,pGuiNum) ; Create the window
;Gui, Add, Text, x8 y27 w280 h60 +backgroundtrans, Downloading %pTitle%
Gui, Add, Progress, w280 h30 x8 y27 background424242 c282828 vSWDLprogress
Gui, Add, Text, xp y+10 h30 +backgroundtrans vSWDLtext1, %pTitle% -
Gui, Add, Text, x+5 yp h30 +backgroundtrans w150 vSWDLtext2,
SW_Show()
Gui, -Resize
VarSetCapacity(vt, 4*11), nParam = 31132253353
Loop, Parse, nParam
NumPut(RegisterCallback("SW_DownloadProgress", "Fast", A_LoopField, A_Index-1), vt, 4*(A_Index-1))
DllCall("urlmon\URLDownloadToFileA", "Uint", 0, "str", pUrl, "str", pFile, "Uint", 0, "UintP", &vt)
WinWaitClose,% "ahk_id " . SteamWin%thisDownloadSW%WinID
Return 1
}
SW_DownloadProgress(pthis, nProgress = 0, nProgressMax = 0, nStatusCode = 0, pStatusText = 0)
{
global
If (A_EventInfo = 6) {
GuiControl,,SWDLprogress,% p := 100 * nProgress//nProgressMax
GuiControl,,SWDLtext2,%nProgress% of %nProgressMax%
If (p = 100)
WinClose,% "ahk_id " . SteamWin%thisDownloadSW%WinID
}
Return 0
} |
Update: OK, actually, it's the WinWaitClose that never ends. But I don't understand it... the download window is successfully closed with WinClose, and WinWaitClose uses the exact same ahk_id for the title. Oh well, I guess the issue is unrelated to the download--thanks for the cool script!
Update 2: Odd, not even this works:
| Code: | Loop
IfWinNotExist,% "ahk_id " . SteamWin%thisDownloadSW%WinID
Break
Return 1 |
Something else in this download script must be stopping this from working. If I remove the code that's supposed to wait for the window, the function completes, but it completes immediately without waiting. If I try and wait for the window, it waits indefinitely even after the window is closed. Hmm...
I even tried setting a global variable in the DownloadProgress function when the download completes, and waiting for that variable in the main script. But it never stops waiting, even after the variable should be set. This is odd behavior. Could the callback or DllCall be somehow responsible?
Without waiting for the window to close, or setting a variable upon completion, I don't know how to know when the download is complete. Not to mention, is that going to mean I can't wait for any more windows in the remainder of my script? This is confusing. _________________ Ben
My Trac projects
My Wiki
[Broken] - My music |
|
| Back to top |
|
 |
Murp|e
Joined: 12 Jan 2007 Posts: 531 Location: Norway
|
Posted: Sun Dec 23, 2007 6:09 am Post subject: |
|
|
| Would winkill or Process, close, PID-or-Name do any good? |
|
| Back to top |
|
 |
bmcclure
Joined: 24 Nov 2007 Posts: 774
|
Posted: Sun Dec 23, 2007 6:14 am Post subject: |
|
|
Using WinKill doesn't seem to correct the issue, it's still indefinitely waiting for the already-killed window for some reason. It's like whatever changes I make from within the callback function aren't seen afterwards by the rest of the script. But I could be completely wrong about that. WinWaitClose shouldn't care how the window was closed as long as it stops existing. _________________ Ben
My Trac projects
My Wiki
[Broken] - My music |
|
| Back to top |
|
 |
Murp|e
Joined: 12 Jan 2007 Posts: 531 Location: Norway
|
Posted: Sun Dec 23, 2007 6:25 am Post subject: |
|
|
What about: | Code: | WinGetText, OutputVar, ahk_pid %myPID%
if errorlevel
break |
|
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Sun Dec 23, 2007 8:05 am Post subject: |
|
|
| You don't need to wait, the DllCall() will not return until the download is completed. |
|
| Back to top |
|
 |
bmcclure
Joined: 24 Nov 2007 Posts: 774
|
Posted: Sun Dec 23, 2007 8:08 am Post subject: |
|
|
D'oh!
Thanks Sean, I didn't realize that. So I can potentially close the window immediately after the DLL call then I take it. Nice. _________________ Ben
My Trac projects
My Wiki
[Broken] - My music |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Thu Mar 13, 2008 9:58 pm Post subject: |
|
|
Thanks again, Sean. This is a great convenience.
I slightly modified your code to create unobtrusive tray-tip like progress:
| Code: | DetectHiddenWindows, On
url:="http://www.pinellascounty.org/fbg/images/wildlife/Tree_Frog_lg.jpg"
splitpath,url,ofn
file := A_Temp "\" ofn
MsgBox, % file "`n" url
If Download( url, file )
Runwait, Rundll32.exe %A_Windir%\system32\shimgvw.dll`,ImageView_Fullscreen %file%
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)
Progress,Hide CWFAFAF7 CT000020 CB445566 x%x% y%y% w330 h52 B1 FS8 WM700 WS700 FM8 ZH12 ZY3 C11,,%_cu%,AutoHotkeyProgress,Tahoma
WinSet,Transparent,180,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
} |
Edit:
Snapshot of TNA -

Last edited by SKAN on Fri Mar 14, 2008 9:44 am; edited 1 time in total |
|
| Back to top |
|
 |
trik
Joined: 15 Jul 2007 Posts: 1320
|
Posted: Fri Mar 14, 2008 3:18 am Post subject: |
|
|
| Sean, when I first saw this, I sort of brushed it off thinking it was just another boring function I could never hope to understand. Now I have learned to completely read the title before passing through. I greatly appreciate you making this. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Thu Oct 07, 2010 1:01 am Post subject: |
|
|
| SKAN wrote: | | 1) It returns the file from cache when available.. Can we force the download? | I don't think making URLDownloadToFile do so will be simple. Use instead URLDownloadToCacheFile with BINDF_GETNEWESTVERSION flag.
| Code: | sUrl := "http://www.autohotkey.com/download/AutoHotkeyInstall.exe"
sFile := A_Temp . "\AutoHotkeyInstall.exe"
VarSetCapacity(sCache,260*2) ; assuming UNICODE build
DllCall("urlmon\URLDownloadToCacheFileW", "Ptr", 0, "Str", sUrl, "Str", sCache, "UInt", VarSetCapacity(sCache)//2, "UInt", BINDF_GETNEWESTVERSION:=0x10, "Ptr*", &vt)
FileCopy, %sCache%, %sFile%
|
It's Invalid Syntax error. What did you use as URL?
| Code: | | MK_E_SYNTAX = 0x800401E4 ; Invalid Syntax |
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Thu Oct 07, 2010 1:40 am Post subject: |
|
|
| Sean wrote: | | I don't think making URLDownloadToFile do so will be simple. Use instead URLDownloadToCacheFile with BINDF_GETNEWESTVERSION flag. |
er.. Seeing the parameters, I am unable to guess how to pass that flag to the function.
Guess, I am stuck with UrlDownLoadToFile... I could workaround calling UrlDownloadToFile twice, first to find and delete the cache file and then do the download.
| Sean wrote: | It's Invalid Syntax error. What did you use as URL?
| Code: | | MK_E_SYNTAX = 0x800401E4 ; Invalid Syntax |
|
I am able to reproduce it with your code. The only alteration I have in my code is the return type for urlmon\UrlDownloadToFile
| Code: | sUrl := "http://www.autohotkey.com/forum/viewtopic.php?t=19475"
sFile := A_ScriptDir "\post.htm"
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))
Res := DllCall("urlmon\URLDownloadToFileA", "Uint", 0, "str", sUrl, "str", sFile, "Uint", 0, "UintP", &vt, UInt )
Progress, Off
SetFormat, Integer, hex
MsgBox, % Res+0
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
} |
|
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Thu Oct 07, 2010 2:09 am Post subject: |
|
|
It worked fine with AHK_L UNICODE 64bit build. The code with the official AHK will be:
| Code: | sUrl := "http://www.autohotkey.com/forum/viewtopic.php?t=19475"
sFile := A_ScriptDir "\post.htm"
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))
hr := DllCall("urlmon\URLDownloadToCacheFileA", "UInt", 0, "Str", sUrl, "Str", sCache, "UInt", VarSetCapacity(sCache,260), "UInt", BINDF_GETNEWESTVERSION:=0x10, "UInt*", &vt, "UInt")
If hr = 0
FileMove, %sCache%, %sFile%, 1
MsgBox % hr "|" sCache
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
}
|
|
|
| Back to top |
|
 |
Uberi
Joined: 23 Aug 2010 Posts: 694 Location: Ontario, Canada
|
Posted: Thu Oct 07, 2010 2:56 am Post subject: |
|
|
| Sean wrote: | It worked fine with AHK_L UNICODE 64bit build. The code with the official AHK will be:
| Code: | sUrl := "http://www.autohotkey.com/forum/viewtopic.php?t=19475"
sFile := A_ScriptDir "\post.htm"
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))
hr := DllCall("urlmon\URLDownloadToCacheFileA", "UInt", 0, "Str", sUrl, "Str", sCache, "UInt", VarSetCapacity(sCache,260), "UInt", BINDF_GETNEWESTVERSION:=0x10, "UInt*", &vt, "UInt")
If hr = 0
FileMove, %sCache%, %sFile%, 1
MsgBox % hr "|" sCache
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
}
|
|
I believe the line:
| Code: | | VarSetCapacity(vt, 4*11), nParam = 31132253353 |
Should be:
| Code: | | VarSetCapacity(vt, 4*11), nParam := 31132253353 |
Unless I'm mistaken? Nice script, by the way. _________________ AutoHotkey.net | GitHub
AHK_L v1.1.05.06
The original BSD license is my default license unless otherwise stated. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Thu Oct 07, 2010 3:10 am Post subject: |
|
|
Again: URLDownloadToCacheFileA() did not download webpages for me in IE 6.0.2900 in XP SP3
But I found the culprit. Using DllCall( "ole32\CoInitialize", UInt,0 ) does the trick, again
urlmon\URLDownloadToFile() also works now, with CoInitialize() added.
Many Many Thanks!... For the quick reply and especially for URLDownloadToCacheFileA()
 |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Thu Oct 07, 2010 10:36 am Post subject: |
|
|
| Uberi wrote: | I believe the line: | Code: | | VarSetCapacity(vt, 4*11), nParam = 31132253353 | Should be: | Code: | | VarSetCapacity(vt, 4*11), nParam := 31132253353 |
|
| Help wrote: | | In v1.0.46.01+, when a comma is followed immediately by a variable and an equal sign, that equal sign is automatically treated as an assignment (:=). For example, all of the following are assignments: x:=1, y=2, a=b=c |
Anyway, I don't recommend this practice, IMO, one of the most confusing syntaxes in AHK. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|