Download Demo

Download a file from the internet to your hard drive. Updated January 28, 2006

See issue #136 and issue #141 of the Liberty BASIC Newsletter for articles on the usage.

'Download html from given
'URL to file on disk.
'Minimum availability Internet Explorer 3.0
'Minimum operating systems Windows NT 4.0, Windows 95

'check for valid URL for HTML page:
u$ = MultiByteToWideChar$("")

if u$ = "" then
    print "Unable to convert URL to unicode string."
end if

'if result = 0, URL is valid
result = ValidURL(u$)

if result = 0 then
    'download html from
    downloadresult = DownloadToFile("", DefaultDir$ + "\test.htm")
    if downloadresult <> 0 then print "Error downloading HTML file."

    open DefaultDir$ + "\test.htm" for input as #f
    test$ = input$(#f, LOF(#f))
    close #f

    print test$ 'print HTML from file into mainwin
    print : print
    print "The text above was downloaded from"
    print "Invalid URL:"; u$
end if

'now attempt to download the banner:
result = DownloadToFile("", DefaultDir$ + "\banner.jpg")
if result <> 0 then
    print "Error downloading Liberty BASIC banner."
    run "mspaint.exe " + chr$(34) + DefaultDir$ + "\banner.jpg" + chr$(34)
    print : print : print "Done"
end if


Function DownloadToFile(urlfile$, localfile$)
    open "URLmon" for dll as #url
    calldll #url, "URLDownloadToFileA",_
    0 as long,_         'null
    urlfile$ as ptr,_   'url to download
    localfile$ as ptr,_ 'save file name
    0 as long,_         'reserved, must be 0
    0 as long,_         'callback address, can be 0
    DownloadToFile as ulong  '0=success
    close #url
end function

Function ValidURL(urlfile$)
    open "URLmon" for dll as #url
    calldll #url, "IsValidURL",_
    0 as long,_         'ignored, must be 0
    urlfile$ as ptr,_   'urlfile to check
    0 as ulong,_        'ignored, must be 0
    ValidURL as long
    close #url
end function

function MultiByteToWideChar$(String$)
    'converts any string into unicode
    CodePage = 0  :  dwFlags = 0
    cchMultiByte = -1
    lpMultiByteStr$ = String$
    cchWideChar = len(String$) * 3
    lpWideCharStr$ = space$(cchWideChar)

    calldll #kernel32, "MultiByteToWideChar", _
    CodePage as ulong, _    'CP_ACP=0, ansi code page
    dwFlags as ulong, _     'use 0, flags for character translation
    lpMultiByteStr$ as ptr,_'the ascii string to convert
    cchMultiByte as long, _ 'len of string, -1 for null-terminated string
    lpWideCharStr$ as ptr, _'buffer for returned ansi string
    cchWideChar as long, _  'size in wide characters of string buffer
    result as long          'returns number of wide characters written to buffer

    if result = 0 then
        MultiByteToWideChar$ = ""
        MultiByteToWideChar$ = left$(lpWideCharStr$, result * 2)
    end if
    end function


Source Code





Media Demos



API Resources


LB 4 Companion

Mastering LB 3

LB Workshop

Game Workshop