Author Topic: Progressbar for URLDownloadToFile  (Read 5310 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Progressbar for URLDownloadToFile
« on: July 08, 2021, 12:01:04 am »
As per this page
https://www.qb64.org/forum/index.php?topic=3910.0

I been busy, but been tinkering with this, got it to work. But been doing research on the last var lpfnCB
I cant seem to find documentation on how to make this display a progress bar - instead of just a blankscreen.
At least nothing for the BASIC language.

Anyone help on this?

code in question:
Code: QB64: [Select]
  1.  Function URLDownloadToFileA%& (ByVal pCaller As Long, szURL As String, szFileName As String, Byval dwReserved As Long, Byval lpfnCB As Long)
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Progressbar for URLDownloadToFile
« Reply #1 on: July 08, 2021, 12:12:24 am »
For this particular function, you'd need to use "interfaces" and a callback function. That still wouldn't display a progress bar. You'd have to code that yourself or use a library like InForm or my Task Dialog library. I have yet to successfully use interfaces but I have made code for downloading files that does return progress if you'd like that.
Shuwatch!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Progressbar for URLDownloadToFile
« Reply #2 on: July 08, 2021, 08:41:27 am »
sure I can take a look.
what I am looking for is something to show that there is activity vs screen locked (which its not) :-)
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Progressbar for URLDownloadToFile
« Reply #3 on: July 08, 2021, 10:02:04 am »
o and fyi, I like pain LOL - I mean progress bar in dos and I am still working on a command line downloader
 :-)

is there a way to make something run in the background
example I can just whip up an animated scroll bar to show motion, but it would need to run in the backghroug

psuedo:

run fancy scrollbar
run download action

then scrollbar stops when download is finished.

something asynchronous
« Last Edit: July 08, 2021, 10:03:41 am by xra7en »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Progressbar for URLDownloadToFile
« Reply #4 on: July 08, 2021, 10:22:51 am »
Yes, but not with that function. I'm on vacation right now but if I'm able to get that code to you I will
Shuwatch!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Progressbar for URLDownloadToFile
« Reply #5 on: July 08, 2021, 03:36:07 pm »
o no rush.. is there something in qb64 that allows two functions to run at the same time?
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

FellippeHeitor

  • Guest
Re: Progressbar for URLDownloadToFile
« Reply #6 on: July 08, 2021, 04:21:29 pm »
ON TIMER can be used to trigger multiple procedures simultaneously. In the following example, if instead of GOSUB Timer.Trap you had a SUB Timer.Trap, it'd run even if mySub was already being run too:

Code: QB64: [Select]
  1. ON TIMER(t1, 1) GOSUB Timer.Trap 'the code following the Timer.Trap label will be run every 1 second
  2.  
  3. ON TIMER(t2, .5) mySub 'QB64 can also trigger a SUB procedure with TIMER;
  4. '                       in this case mySUB will be triggered every 500 milliseconds
  5.  
  6. 'activate timers:
  7. TIMER(t1) ON
  8. TIMER(t2) ON
  9.  
  10. DO 'go into an infinite loop until the window is closed
  11.     _LIMIT 1 'run the main loop at 1 cycle per second, to show how timers are independent from main program flow
  12.  
  13. Timer.Trap:
  14. PRINT "1s; ";
  15.  
  16. SUB mySub
  17.     PRINT "500ms; ";

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Progressbar for URLDownloadToFile
« Reply #7 on: July 08, 2021, 04:36:21 pm »
Right. I don't think it would allow us to capture how much has been downloaded yet with that function, though
Shuwatch!

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Progressbar for URLDownloadToFile
« Reply #8 on: July 08, 2021, 09:17:56 pm »
Using ON TIMER gives the appearance of running two things at the same time,
but actually the timed event pauses main execution, as the following code shows.
If it was simultaneous execution like running on another core, the delay in the sub
would not effect the count.

Code: QB64: [Select]
  1. Common Shared showcount, xdelay!
  2. On Timer(t1, 1) mySub
  3. Timer(t1) On
  4.  
  5. Do: _Limit 100
  6.     count = count + 1
  7.     If showcount Then
  8.         Print Using "#.##"; xdelay!;
  9.         Print count
  10.         count = 0
  11.         showcount = 0
  12.     End If
  13.  
  14. Sub mySub
  15.     xdelay! = Rnd + .1
  16.     _Delay xdelay
  17.     showcount = 1
  18.  
« Last Edit: July 08, 2021, 09:20:35 pm by Richard Frost »
It works better if you plug it in.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Progressbar for URLDownloadToFile
« Reply #9 on: July 08, 2021, 11:04:45 pm »
OK, took a shot at Richard's method.. It would not go past the download.

 
Code: QB64: [Select]
  1.        Dim t1
  2.         t1 = _FreeTimer
  3.         On Timer(t1, 1) progressBar
  4.         Timer(t1) On
  5.  
  6.         appTxt = FileDownload(thisAppLink, "dl/" + filename)
  7.         If appTxt <> 0 Then
  8.             Print
  9.             Color 4
  10.             Print "Problem retreiving " + filename + " from " + thisAppLink
  11.             System
  12.         End If
  13.  

or did I put that incorrect?

thanks guys for checking this out
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Progressbar for URLDownloadToFile
« Reply #10 on: July 08, 2021, 11:37:34 pm »
Might check out spriggs libs.
I would be satisfied with a windows progress bar dialog.
I found this: https://www.vbforums.com/showthread.php?800993-RESOLVED-Need-Code-For-Downloading-with-Progress-Bar-!
looks promising, but more up spriggs line of programing :-)
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Progressbar for URLDownloadToFile
« Reply #11 on: July 09, 2021, 07:52:34 am »
I'm not able to access my home PC right now. Looks like it is offline. As soon as I can get into it then I will give it to ya.
Shuwatch!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Progressbar for URLDownloadToFile
« Reply #12 on: July 09, 2021, 08:03:40 am »
I'm not able to access my home PC right now. Looks like it is offline. As soon as I can get into it then I will give it to ya.

hey yer on VAY-K, no peekin' on qb :P
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Progressbar for URLDownloadToFile
« Reply #13 on: July 10, 2021, 02:11:40 pm »
Here is the whole "scratch book" drawing I am workiing on.
I tried all this with wget, curl and powershell and currently working with a download script from spriggy. Since powershell works well, I found there was a problem with windows 7 fresh install, because you need I believe PS5+. The command that allows you to download doesn't work on earlier versions, and I wanted this to work on fresh installs. I could have used the curl/wget but each had their own problems (mostly security and SSL issues) and besides, I wanted a self contained version, thus spriggy idea. But that was problematic. Example I did another win7 fresh install, and the download no longer works. Not sure whats missing and why it worked on the other win7 but not my fresh one.

Still looking for a progress bar -

I did see another site appget.net that has  a progress bar and looks identical to what I was trying to do. Just saw it 2 days ago and tried it out. Perfect.  LOL. puts mine to shame /sigh
here is their source code: https://github.com/appget/appget/tree/master/src
havent looked at it,as I do not use or even looked at c-sharp.

So now just using it as a learning too. I still want to see if i can get this to do downloads without having to resort to 3rd party and use all QB64 natively

Take a look and see what you think
you can use the database here https://appgetmgr.com/db/app.txt
save it as app.db


Code: QB64: [Select]
  1.  
  2.     Function URLDownloadToFileA%& (ByVal pCaller As Long, szURL As String, szFileName As String, Byval dwReserved As Long, Byval lpfnCB As Long)
  3.  
  4. Const REL = "210709.0519"
  5. Const DATABASE = "app.db"
  6. Const DOWNLOAD_FOLDER = "dl\"
  7. Const REMOTE_DB = ""
  8. Const APP_NAME = "AppGet Manager"
  9. Const APP_URL = ""
  10. Const TRUE = 1
  11. Const FALSE = 0
  12.  
  13. Dim Shared thisAppLink As String
  14. Dim Shared FF As Integer 'free file
  15. Dim Shared matches(100) As String ' should not be more than 100 matches.
  16. Dim Shared filename As String
  17. Dim appTxt As _Offset
  18.  
  19.  
  20. 'create a download dir if not exists
  21. If Not _DirExists(DOWNLOAD_FOLDER) Then MkDir (DOWNLOAD_FOLDER)
  22.  
  23.  
  24.  
  25. ' act on param sent
  26.     Case "D", "-D":
  27.  
  28.         thisAppLink = findApp(Command$(2))
  29.  
  30.  
  31.         Cls
  32.         appGetHeader
  33.  
  34.         '*** D O W N L O A D I N G  F I L E
  35.         '*** Strip the filename off the end
  36.         '*** "file" is appended at the back of mediafire files.
  37.         If Right$(thisAppLink, 4) = "file" Then
  38.             Print thisAppLink
  39.             thisAppLink = Mid$(thisAppLink, 1, Len(thisAppLink) - 5)
  40.             filename = _Trim$(Mid$(thisAppLink, _InStrRev(thisAppLink, "/") + 1))
  41.  
  42.         Else
  43.             filename = _Trim$(Mid$(thisAppLink, _InStrRev(thisAppLink, "/") + 1))
  44.         End If
  45.  
  46.         LWRITELN "`2Downloading  ... [ `0" + filename + " ]"
  47.         LWRITELN "`#URL: `%" + thisAppLink
  48.  
  49.  
  50.         '*** download it...
  51.         'Shell "cd " + DOWNLOAD_FOLDER + " && curl -LO -k " + thisAppLink + " && cd .."
  52.         'Shell "cd " + DOWNLOAD_FOLDER + " && Powershell -Command Invoke-WebRequest -Uri " + thisAppLink + " -Outfile " + filename + " && cd .."
  53.  
  54.         Print
  55.         LWRITELN "`6Downloading now, one moment . . ."
  56.  
  57.  
  58.         appTxt = FileDownload(thisAppLink, "dl/" + filename)
  59.         If appTxt <> 0 Then
  60.             Print
  61.             Color 4
  62.             Print "Problem retreiving " + filename + " from " + thisAppLink
  63.             System
  64.         End If
  65.  
  66.         LWRITELN "`0Success! `6Your file has been saved to `%.\DL\ "
  67.         LWRITELN "`7If you do not see " + filename + "installing, check the taskbar below"
  68.  
  69.  
  70.         'check to see if it is an archive 7z,zip,rar,arj.exe
  71.         Select Case LCase$(Right$(filename, 3))
  72.             Case ".7z", "arj", "rar", "zip": Shell _Hide _DontWait "cmd /c 7zFM .\" + DOWNLOAD_FOLDER + filename
  73.             Case "exe", "msi": Shell _Hide _DontWait "cmd /c .\" + DOWNLOAD_FOLDER + filename
  74.         End Select
  75.  
  76.         '*** originall was going to clean up, but save downloaded files
  77.         'Kill DOWNLOAD_FOLDER +  filename
  78.  
  79.         System
  80.  
  81.  
  82.     Case "-V", "V": Print: Print "Current Release: "; REL: Print
  83.     Case Else:
  84.         appGetHeader
  85.         LWRITELN "`!Usage: appget command [`%appname...]"
  86.         LWRITELN "`3Available commands:"
  87.         LWRITELN "     `%d - `7download appname (or partial search)"
  88.         Print
  89.         LWRITELN "`7Example:"
  90.         LWRITELN "`2appget `$d `0notepad`7"
  91.         Print
  92.         System
  93.  
  94.  
  95.  
  96.  
  97. ' $include: 'utils.bi'
  98.  
  99.  
  100. Sub progressBar
  101.     Print ".";
  102.  
  103. Function FileDownload%& (URL As String, File As String)
  104.     FileDownload = URLDownloadToFileA(0, URL + Chr$(0), File + Chr$(0), 0, 0)
  105.  
  106.  
  107.  
  108. Sub downloadDB
  109.     '*** see page for variable types
  110.     '*** in order to work must be defined as offset
  111.     '*** https://www.qb64.org/wiki/Variable_Types
  112.     Dim appTxt As _Offset
  113.     Dim x As Integer
  114.  
  115.  
  116.     'If _FileExists("app.db") Then Kill "app.db"
  117.     Print
  118.     LWRITE "`8Reading remote database...."
  119.  
  120.     '*** download it...
  121.     'check to see if there is an existing one
  122.     If _FileExists("app.db") Then Exit Sub
  123.  
  124.  
  125.     appTxt = FileDownload(REMOTE_DB, "app.db") ' download the db and save it to "APP.DB"
  126.     If appTxt <> 0 Then
  127.         Print
  128.         Print "AppTxt Val = "; appTxt
  129.         Print
  130.         LWRITELN "`4*** `2ISSUE RET RIEVING DATABASE `4***`7"
  131.         LWRITELN "`8Location: " + REMOTE_DB + "`7"
  132.         System
  133.     End If
  134.  
  135.     'Shell _Hide "rename app.txt app.db"
  136.  
  137.     LWRITELN "`%Done!"
  138.  
  139.     Print
  140.  
  141.  
  142.  
  143. Sub openDB
  144.     downloadDB
  145.     FF = FreeFile
  146.  
  147.     Open DATABASE For Input As #FF
  148.  
  149.  
  150. Function findApp$ (appname As String)
  151.  
  152.     Dim appSelectStr As String
  153.  
  154.     Dim aline As String
  155.     Dim recNum As Integer ' record count
  156.     'If Not _FileExists(DATABASE) Then findApp = "": Exit Function
  157.  
  158.     recNum = 1
  159.     Dim recCount As Integer: recCount = 0
  160.     appGetHeader
  161.  
  162.     openDB
  163.     Print "Results"
  164.     While Not EOF(FF)
  165.         recCount = recCount + 1
  166.         Line Input #FF, aline
  167.         'Remove the trailing "," if there is one.
  168.         'Sometimes I use a spread sheet to organize and when exporting it
  169.         'adds aother ","
  170.         If Right$(aline, 1) = "," Then aline = Left$(aline, Len(aline) - 1)
  171.  
  172.         ReDim d(5) As String
  173.         EXPLODE aline, ",", d()
  174.  
  175.         If InStr(LCase$(d(1)), LCase$(appname)) And aline > "" Then
  176.  
  177.             LWRITE "`8[`%"
  178.             Print recNum;
  179.             LWRITE "`8] `3"
  180.             Print d(1);
  181.             LWRITE " `8ver. `7"
  182.             Print d(3)
  183.             matches(recNum) = d(2)
  184.             recNum = recNum + 1
  185.         End If
  186.     Wend
  187.     Close
  188.     Kill "app.db" ' Make sure each db is downloaded fresh
  189.  
  190.     If recNum - 1 = 0 Then
  191.         Print "Scanned "; recCount; " files..."
  192.         Print appname; " not found. Put in a request"
  193.         System
  194.     Else
  195.  
  196.         LWRITELN "`8[ `@0 `8] `4Cancel": Print
  197.         Color 10
  198.         Line Input "Select id of app to install: "; appSelectStr: recNum = Val(appSelectStr)
  199.         If recNum = 0 Then System
  200.         findApp$ = matches(recNum)
  201.     End If
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. Sub appGetHeader
  209.     Cls
  210.     LWRITELN "`3- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
  211.     LWRITELN "`!" + APP_NAME + " x64 (Windows)`3 rel: `#" + REL
  212.     LWRITELN "`3- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
  213.  
  214.  





Required for pretty colored text :-)
Code: QB64: [Select]
  1. Sub LWRITE (TXT As String)
  2.     Dim i As Integer
  3.     '======================================================
  4.     '--- THIS IS A POPULAR COLOR ROUTINE BASED ON THE
  5.     '--- SETH ABLE L.O.R.D. BBS GAME COLOR FORMATTING
  6.     '=====================================================
  7.     Color 7 ' DEFAULT COLOR
  8.     For i = 1 To Len(TXT$)
  9.         ' CHECK FOR A COLOR CHANGE
  10.         If Mid$(TXT$, i, 1) = "`" Then
  11.             i = i + 1
  12.             Select Case Mid$(TXT, i, 1)
  13.                 Case "0": Color 10
  14.                 Case "1": Color 1
  15.                 Case "2": Color 2
  16.                 Case "3": Color 3
  17.                 Case "4": Color 4
  18.                 Case "5": Color 5
  19.                 Case "6": Color 6
  20.                 Case "7": Color 7
  21.                 Case "8": Color 8
  22.                 Case "9": Color 9
  23.                 Case "!": Color 11
  24.                 Case "@": Color 12
  25.                 Case "#": Color 13
  26.                 Case "$": Color 14
  27.                 Case "%": Color 15
  28.                 Case "n": Print '           new line
  29.                 Case "t": Print , ; '       TAB
  30.             End Select
  31.             i = i + 1
  32.  
  33.         End If
  34.         Print Mid$(TXT$, i, 1);
  35.     Next
  36.     Print ;
  37.  
  38. Sub LWRITELN (TXT As String)
  39.     LWRITE (TXT$)
  40.     Print
  41.  
  42.  
« Last Edit: July 10, 2021, 02:17:39 pm by xra7en »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Progressbar for URLDownloadToFile
« Reply #14 on: July 12, 2021, 03:29:55 pm »
@xra7en

Code: QB64: [Select]
  1.  
  2. Const INTERNET_OPEN_TYPE_DIRECT = 1
  3.  
  4. Const INTERNET_DEFAULT_HTTP_PORT = 80
  5. Const INTERNET_DEFAULT_HTTPS_PORT = 443
  6.  
  7. Const INTERNET_SERVICE_HTTP = 3
  8.  
  9. 'Flags
  10. Const INTERNET_FLAG_SECURE = &H00800000
  11. Const INTERNET_FLAG_RELOAD = &H80000000
  12.  
  13. Const HTTP_QUERY_CONTENT_LENGTH = 5
  14.  
  15. Const TRUE = 1
  16. 'CONST FALSE = 0
  17.  
  18.     Function InternetOpen%& Alias "InternetOpenA" (ByVal lpszAgent As _Offset, Byval dwAccessType As Long, Byval lpszProxy As _Offset, Byval lpszProxyBypass As _Offset, Byval dwFlags As Long)
  19.     Function InternetConnect%& Alias "InternetConnectA" (ByVal hInternet As _Offset, Byval lpszServerName As _Offset, Byval nServerPort As Integer, Byval lpszUserName As _Offset, Byval lpszPassword As _Offset, Byval dwService As Long, Byval dwFlags As Long, Byval dwContext As _Offset)
  20.     Function HTTPOpenRequest%& Alias "HttpOpenRequestA" (ByVal hConnect As _Offset, Byval lpszVerb As _Offset, Byval lpszObjectName As _Offset, Byval lpszVersion As _Offset, Byval lpszReferrer As _Offset, Byval lpszAcceptTypes As _Offset, Byval dwFlags As Long, Byval dwContext As _Offset)
  21.     Function HTTPSendRequest%% Alias "HttpSendRequestA" (ByVal hRequest As _Offset, Byval lpszHeaders As _Offset, Byval dwHeadersLength As Long, Byval lpOptional As _Offset, Byval dwOptionalLength As Long)
  22.     Sub InternetCloseHandle (ByVal hInternet As _Offset)
  23.     Function InternetReadFile%% (ByVal hFile As _Offset, Byval lpBuffer As _Offset, Byval dwNumberOfBytesToRead As Long, Byval lpdwNumberOfBytesRead As _Offset)
  24.     Function HTTPQueryInfo%% Alias "HttpQueryInfoA" (ByVal hRequest As _Offset, Byval dwInfoLevel As Long, Byval lpBuffer As _Offset, Byval lpdwBufferLength As _Offset, Byval lpdwIndex As _Offset)
  25.  
  26.     Function GetLastError& ()
  27.     Sub SetLastError (ByVal dwErrCode As Long)
  28.     Function FormatMessage& Alias "FormatMessageA" (ByVal dwFlags As Long, Byval lpSource As Long, Byval dwMessageId As Long, Byval dwLanguageId As Long, Byval lpBuffer As _Offset, Byval nSize As Long, Byval Arguments As _Offset)
  29.  
  30. Declare CustomType Library ".\offsettostring"
  31.     Function offset_to_string$ Alias "offset_to_offset" (ByVal offset As _Offset)
  32.  
  33.     Function MAKELANGID& (ByVal p As Long, Byval s As Long)
  34.  
  35.  
  36. Screen _NewImage(480, 80, 32)
  37.  
  38. _Title "URL Downloader"
  39. _ConsoleTitle "Enter Link"
  40. Dim filename As String
  41.  
  42.     Cls
  43.     Line Input "Link: ", link
  44.     Line Input "File Name : ", filename
  45. Loop Until link <> "" And filename <> ""
  46. _Title _Title$ + " - " + Mid$(filename, _InStrRev(filename, "\") + 1)
  47.  
  48.  
  49. DownloadLink link, filename
  50.  
  51. Sub DownloadLink (URL As String, File As String)
  52.     Dim As String URLFile
  53.     URLFile = URL
  54.     Dim As _Offset hsession
  55.     hsession = InternetOpen(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
  56.     If hsession = 0 Then
  57.         Cls
  58.         Print "Error : InternetOpen", ErrorMessage(GetLastError)
  59.         InternetCloseHandle hsession
  60.         Exit Sub
  61.     End If
  62.  
  63.     Dim As _Offset httpsession
  64.     URL = Mid$(URL, InStr(URL, "/") + 2)
  65.     URL = Mid$(URL, 1, InStr(URL, "/") - 1)
  66.  
  67.     httpsession = InternetConnect(hsession, _Offset(URL), INTERNET_DEFAULT_HTTPS_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0)
  68.     If httpsession = 0 Then
  69.         Cls
  70.         Print "Error : Internet Connect", ErrorMessage(GetLastError)
  71.         InternetCloseHandle hsession
  72.         Exit Sub
  73.     End If
  74.  
  75.     Dim As _Offset httpRequest
  76.     Dim As String sessiontype, location, accepttypes
  77.     sessiontype = "GET" + Chr$(0)
  78.     location = Mid$(URLFile, InStr(URLFile, URL) + Len(URL)) + Chr$(0)
  79.     accepttypes = "*/*" + Chr$(0)
  80.     httpRequest = HTTPOpenRequest(httpsession, _Offset(sessiontype), _Offset(location), 0, 0, _Offset(accepttypes), INTERNET_FLAG_RELOAD Or INTERNET_FLAG_SECURE, 0)
  81.     If httpRequest = 0 Then
  82.         Cls
  83.         Print "Error : HTTPOpenRequest", ErrorMessage(GetLastError)
  84.         InternetCloseHandle hsession
  85.         Exit Sub
  86.     End If
  87.  
  88.     Dim As Long sendrequest
  89.     Dim As String headers
  90.     headers = ""
  91.     sendrequest = HTTPSendRequest(httpRequest, 0, 0, 0, 0)
  92.     If sendrequest <> TRUE Then
  93.         Cls
  94.         Print "Error : HTTPSendRequest", ErrorMessage(GetLastError)
  95.         InternetCloseHandle hsession
  96.         Exit Sub
  97.     End If
  98.  
  99.  
  100.     Dim As _Byte query
  101.     Dim As String queryinfo
  102.     queryinfo = Space$(1024)
  103.     Dim As Long querylen
  104.     querylen = Len(queryinfo) - 1
  105.  
  106.     query = HTTPQueryInfo(httpRequest, HTTP_QUERY_CONTENT_LENGTH, _Offset(queryinfo), _Offset(querylen), 0)
  107.     If query <> TRUE Then
  108.         Cls
  109.         Print "Error : HTTPQueryInfo", ErrorMessage(GetLastError)
  110.         InternetCloseHandle hsession
  111.     End If
  112.  
  113.     Dim As _Unsigned _Integer64 bytesToRead
  114.     bytesToRead = Val(queryinfo)
  115.  
  116.     Dim As String szBuffer
  117.     szBuffer = Space$(4097)
  118.     Dim As _Unsigned _Integer64 dwRead, bytesRead
  119.     If _FileExists(File) Then
  120.         Kill File
  121.     End If
  122.     Open File For Binary As #1
  123.     Dim As _Byte a
  124.     Dim As String filedownload
  125.     Dim As Long errr, bytesForRate
  126.     Dim x!
  127.     Dim y!
  128.     Dim Rate!
  129.     Dim As Single ratetime
  130.     Do
  131.         x! = Timer
  132.         a = InternetReadFile(httpRequest, _Offset(szBuffer), Len(szBuffer) - 1, _Offset(dwRead))
  133.         errr = GetLastError
  134.         If dwRead > 0 Then
  135.             filedownload = Mid$(szBuffer, 1, dwRead)
  136.             Put #1, , filedownload
  137.             bytesRead = bytesRead + dwRead
  138.             bytesForRate = bytesForRate + dwRead
  139.             ratetime = timeElapsedSince(x!)
  140.             If _Round(ratetime) >= 1 Then
  141.                 Rate! = (bytesForRate / ratetime) / 1024
  142.                 bytesForRate = 0
  143.             End If
  144.             Cls
  145.             Print "Downloading to " + File
  146.             If bytesToRead <> 0 Then
  147.                 Select Case bytesRead
  148.                     Case Is < 1024
  149.                         Print Using "#### B downloaded of "; bytesRead;
  150.                     Case Is < (1024 ^ 2) And bytesRead >= 1024
  151.                         Print Using "####.## KB downloaded of "; (bytesRead / 1024);
  152.                     Case Is < (1024 ^ 3) And bytesRead >= (1024 ^ 2)
  153.                         Print Using "####.## MB downloaded of "; (bytesRead / (1024 ^ 2));
  154.                     Case Is < (1024 ^ 4) And bytesRead >= (1024 ^ 3)
  155.                         Print Using "####.## GB downloaded of "; (bytesRead / (1024 ^ 3));
  156.                 End Select
  157.                 Select Case bytesToRead
  158.                     Case Is < 1024
  159.                         Print Using "#### B"; bytesToRead
  160.                     Case Is < (1024 ^ 2) And bytesToRead >= 1024
  161.                         Print Using "####.## KB"; (bytesToRead / 1024)
  162.                     Case Is < (1024 ^ 3) And bytesToRead >= (1024 ^ 2)
  163.                         Print Using "####.## MB"; (bytesToRead / (1024 ^ 2))
  164.                     Case Is < (1024 ^ 4) And bytesToRead >= (1024 ^ 3)
  165.                         Print Using "####.## GB"; (bytesToRead / (1024 ^ 3))
  166.                 End Select
  167.                 Print Using "###.##%"; bytesRead / bytesToRead * 100
  168.             Else
  169.                 Select Case bytesRead
  170.                     Case Is < 1024
  171.                         Print Using "   ####  B downloaded"; bytesRead
  172.                     Case Is < (1024 ^ 2) And bytesRead >= 1024
  173.                         Print Using "####.## KB downloaded"; (bytesRead / 1024)
  174.                     Case Is < (1024 ^ 3) And bytesRead >= (1024 ^ 2)
  175.                         Print Using "####.## MB downloaded"; (bytesRead / (1024 ^ 2))
  176.                     Case Is < (1024 ^ 4) And bytesRead >= (1024 ^ 3)
  177.                         Print Using "####.## GB downloaded"; (bytesRead / (1024 ^ 3))
  178.                 End Select
  179.             End If
  180.             Select Case Rate!
  181.                 Case Is < 1024
  182.                     Print Using "Rate: #### Bps"; Rate!
  183.                 Case Is < (1024 ^ 2) And Rate! >= 1024
  184.                     Print Using "Rate: ####.## KBps"; Rate! / 1024
  185.                 Case Is < (1024 ^ 3) And Rate! >= (1024 ^ 2)
  186.                     Print Using "Rate: ####.## MBps"; Rate! / (1024 ^ 2)
  187.                 Case Is < (1024 ^ 4) And Rate! >= (1024 ^ 3)
  188.                     Print Using "Rate: ####.## GBps"; Rate! / (1024 ^ 3)
  189.             End Select
  190.             'Print "Rate="; _Round(Rate!); "KBps"
  191.             _Display
  192.         End If
  193.     Loop Until bytesRead = bytesToRead Or errr <> 0
  194.     If errr Then
  195.         Print "Error downloading file:"; errr
  196.         Close #1
  197.         InternetCloseHandle hsession
  198.         Kill File
  199.         Exit Sub
  200.     Else
  201.     End If
  202.     Close #1
  203.     InternetCloseHandle hsession
  204.     Cls
  205.     Select Case bytesRead
  206.         Case Is < 1024
  207.             Print Using "#### B downloaded of "; bytesRead;
  208.         Case Is < (1024 ^ 2) And bytesRead >= 1024
  209.             Print Using "####.## KB downloaded of "; (bytesRead / 1024);
  210.         Case Is < (1024 ^ 3) And bytesRead >= (1024 ^ 2)
  211.             Print Using "####.## MB downloaded of "; (bytesRead / (1024 ^ 2));
  212.         Case Is < (1024 ^ 4) And bytesRead >= (1024 ^ 3)
  213.             Print Using "####.## GB downloaded of "; (bytesRead / (1024 ^ 3));
  214.     End Select
  215.     Select Case bytesToRead
  216.         Case Is < 1024
  217.             Print Using "#### B"; bytesToRead
  218.         Case Is < (1024 ^ 2) And bytesToRead >= 1024
  219.             Print Using "####.## KB"; (bytesToRead / 1024)
  220.         Case Is < (1024 ^ 3) And bytesToRead >= (1024 ^ 2)
  221.             Print Using "####.## MB"; (bytesToRead / (1024 ^ 2))
  222.         Case Is < (1024 ^ 4) And bytesToRead >= (1024 ^ 3)
  223.             Print Using "####.## GB"; (bytesToRead / (1024 ^ 3))
  224.     End Select
  225.     Print Using "###.##%"; bytesRead / bytesToRead * 100
  226.     Print "Downloaded to " + File
  227.  
  228. Function timeElapsedSince! (startTime!)
  229.     If startTime! > Timer Then startTime! = startTime! - 86400
  230.     timeElapsedSince! = Timer - startTime!
  231.  
  232. Function ErrorMessage$ (errCode As Long)
  233.     Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H00000100
  234.     Const FORMAT_MESSAGE_FROM_SYSTEM = &H00001000
  235.     Const FORMAT_MESSAGE_IGNORE_INSERTS = &H00000200
  236.  
  237.     Const LANG_NEUTRAL = &H00
  238.     Const SUBLANG_DEFAULT = &H01
  239.  
  240.     Dim As _Offset lpMsgBuf
  241.     Dim As Long msg
  242.  
  243. msg = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER Or _
  244.                     FORMAT_MESSAGE_FROM_SYSTEM Or _
  245.                     FORMAT_MESSAGE_IGNORE_INSERTS, _
  246.                    0, _
  247.                     errCode, _
  248.                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), _
  249.                     _Offset(lpMsgBuf), _
  250.                    0, 0)
  251.  
  252.  
  253.     ErrorMessage = offset_to_string(lpMsgBuf)
  254.  
Shuwatch!