The length of ls_file returned by each calling of InternetReadFile is
not matching lul_bytesRead (also returned by this function). I am doing
something wrong when using the function InternetReadFile of
WININET.DLL. At the end of execution lul_bytesReadTotal is equal to the
size of file available for download. The size of file downloaded is wrong.
Any help will be appreciated.
Thanks
Declaring local external functions:
FUNCTION ulong InternetOpenA (string lpszAgent, ulong dwAcessType,
string lpszProxyName, string lpszProxyBypass, ulong dwFlags) LIBRARY
"Wininet.dll"
FUNCTION ulong InternetOpenUrlA (ulong hInternetSession, string lpszUrl,
string lpszHeaders, ulong dwHeadersLength, ulong dwFlags, ulong
dwContext) LIBRARY "Wininet.dll"
FUNCTION boolean InternetReadFile (ulong hFile, REF string lpBuffer,
ulong dwNumberOfBytesToRead, ref ulong lpdwNumberOfBytesRead) Library
"WININET.DLL"
FUNCTION boolean InternetCloseHandle (ulong hInternet) LIBRARY "Wininet.dll"
Listing 1: Function code for of_filetransfer
CONSTANT ulong INTERNET_OPEN_TYPE_PRECONFIG = 0
CONSTANT ulong INTERNET_FLAG_RELOAD = 2147483648 //0x80000000
CONSTANT ulong INTERNET_CACHE_NO_CACHE_WRITE = 67108864 //0x04000000
CONSTANT ulong BUFFER_LEN = 10000
// This function downloads file from a given Internet URL
// Parameters are
// as_appname - a string representing application name of calling app
// as_url - a string holding fully qualified URL
// as_filename -a string holding path where the
// downloaded file will be saved.
// RETURN - The function returns an error message & or the string "success"
ulong lul_hInternetSession, lul_hUrlFile, lul_bytesRead, lul_bytesReadTotal
string ls_file, ls_work, ls_return = ""
int li_ret, li_fh, li_write_len
boolean lb_ret
lul_bytesReadTotal = 0
// Initialize the buffer
ls_file = Space(BUFFER_LEN)
DO WHILE TRUE
//Establish Internet connection
lul_hInternetSession = InternetOpenA (as_appname,
INTERNET_OPEN_TYPE_PRECONFIG, "", "", 0)
IF isNull(lul_hInternetSession) THEN
ls_return = "Internet Open Failed"
EXIT
END IF
lul_hUrlFile = InternetOpenUrlA(lul_hInternetSession, as_url, "",
0, INTERNET_FLAG_RELOAD, 0)
IF isNull(lul_hUrlFile) THEN
ls_return = "Url Open Failed"
EXIT
END IF
li_fh = FileOpen(as_filename, StreamMode!, Write!, Lockwrite!,
Replace!)
IF li_fh < 0 THEN
ls_return = "File open error"
EXIT
END IF
// Now read the file
DO WHILE TRUE
lb_ret = InternetReadFile(lul_hUrlFile, ls_file, BUFFER_LEN,
lul_bytesRead)
lul_bytesReadTotal = lul_bytesReadTotal + lul_bytesRead
IF lul_bytesread = 0 and lb_ret THEN
EXIT
END IF
IF lul_bytesread < BUFFER_LEN THEN
ls_work = Mid(ls_file, 1, lul_bytesread)
li_write_len = FileWrite(li_fh, ls_work)
ELSE
li_write_len = FileWrite(li_fh, ls_file)
END IF
ls_file = Space(BUFFER_LEN)
LOOP
EXIT
LOOP
IF li_fh > 0 THEN
FileClose(li_fh)
END IF
IF NOT isNull(lul_hUrlFile) THEN
InternetCloseHandle(lul_hUrlFile)
END IF
IF NOT isNull(lul_hInternetSession) THEN
InternetCloseHandle(lul_hInternetSession)
END IF
Return "Success~n" + String(lul_bytesReadTotal) + " Bytes~n" +
String(Round(lul_bytesReadTotal/1024,0)) + " KB"
Listing 2: Calling the function of_transfer_file.
n_cst_wininet luo_wininet
string ls_msg
SetPointer(HourGlass!)
luo_wininet = CREATE n_cst_wininet
ls_msg = luo_wininet.of_transfer_file(&
"Investment TRacking System", &
"http://www.mindspring.com/~~madhav/mktdelim.txt", &
"c:\mspr32\download\mktdelim.txt")
DESTROY luo_wininet
MessageBox("Internet Download Status",ls_msg)