WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Web UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

HTTP Download UDFs


UDFLIB.PROGRESSBAR.WBT

(Progress Bar UDF Library (required))
;---------------------------------------------------------------------------------------------
;  udflib.progressbar.wbt
;---------------------------------------------------------------------------------------------
;  by GUI Guy (http://guiguy.wminds.com)
;
;  based on code found on the Winbatch support site long ago - sorry, original credit is unknown

;---------------------------------------------------------------------------------------------
#DefineFunction udfProgressBarCreate(bartype,startx,starty,width,height,step,windowtitle)
;---------------------------------------------------------------------------------------------
;  Creates a progress bar
;  Returns the handle of the progress bar or -1 on failure
;
;   bartype = (4 = vertical, 3 = horizontal )
;   startx = x coordinate of upper left corner of progress bar
;   starty = y coordinate of upper left corner of progress bar
;   width = width of progress bar
;   height = height of progress bar
;   step = how many units to increment on each call to udfProgressBarTick
;   windowtitle = title of window in which to place the progress bar (use "" for main WinBatch window)

dll=strcat(dirwindows(1),"user32.dll")
func="CreateWindowExA"
winclass="msctls_progress32"
hwnd=dllhwnd(windowtitle)
hinst=dllhinst(windowtitle)

smooth= 1
vis= 268435456
child= 1073741824
hProgBar=dllcall(dll,long:func,long:0,lpstr:winclass,lpstr:"",long:bartype | smooth | vis | child,long:startx,long:starty,long:width,long:height,long:hwnd, long:0,long:hinst,long:0)
if !hProgBar then return(-1)
IntControl (22,hProgBar,1028,step,0)
return(hProgBar)
#EndFunction 
;---------------------------------------------------------------------------------------------
#DefineFunction udfProgressBarTick(hProgBar)
;---------------------------------------------------------------------------------------------
;  Increments a progress bar (by amount specified when it was created)
;  Returns 1
;
;   handle = handle of progress bar to increment

IntControl (22,hProgBar,1029,0,0)
return(1)
#EndFunction

UDFLIB.HTTPDOWNLOAD.WBT

;---------------------------------------------------------------------------------------------
;  udflib.httpdownload.wbt
;---------------------------------------------------------------------------------------------
;  by GUI Guy (http://guiguy.wminds.com)
;
;  Based largely on code by GUIDO - found on the WinBatch support site.

#Include "E:\Scripts\MyGUIGuy script source\Common\udflib.progressbar.wbt"

;---------------------------------------------------------------------------------------------
#DefineFunction udfHttpDownload(url,localfile,timeout,titlebar,coordx,coordy)
;---------------------------------------------------------------------------------------------
;  Downloads "url" into "localfile" via http and displays a nice dialog with progress bar.
;
;   url = remote file location in form host.domain/path/filename.ext  (NO "HTTP://" at the beginning!!!)
;   localfile = fully qualified path / filename to save url to - if exists, will be overwritten
;   timeout = timeout value in seconds
;   titlebar = caption to display in titlebar
;   coordx = x coordinate of upper left corner (use -1 for centered horizontally)
;   coordy = y coordinate of upper left corner (use -1 for centered vertically)

username=""  ;maybe export these 3 variables to the function too if http connect requires them:
password=""  ; ^
referer=""   ; ^

host=ItemExtract(1,url,"/")
object=ItemRemove(1,url,"/")

dlgHttpDownloadFormat=`WWWDLGED,6.1`
dlgHttpDownloadCaption=titlebar
dlgHttpDownloadX=coordx
dlgHttpDownloadY=coordy
dlgHttpDownloadWidth=218
dlgHttpDownloadHeight=078
dlgHttpDownloadNumControls=005
dlgHttpDownloadProcedure=`procHttpDownload`
dlgHttpDownloadFont=`DEFAULT`
dlgHttpDownloadTextColor=`DEFAULT`
dlgHttpDownloadBackground=`DEFAULT,DEFAULT`
dlgHttpDownloadConfig=0
dlgHttpDownload001=`177,061,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,"Microsoft Sans Serif|5632|40|34","0|0|0",DEFAULT`
dlgHttpDownload002=`001,055,212,004,GROUPBOX,DEFAULT,DEFAULT,DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
dlgHttpDownload003=`003,003,046,012,STATICTEXT,DEFAULT,"Downloading:",DEFAULT,4,DEFAULT,"Microsoft Sans Serif|5632|70|34","0|0|0",DEFAULT`
dlgHttpDownload004=StrCat('003,041,208,012,STATICTEXT,DEFAULT,"',"http://",url,'",DEFAULT,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT')
dlgHttpDownload005=`001,061,174,012,EDITBOX,edtStatus,"",DEFAULT,6,8,DEFAULT,DEFAULT,DEFAULT`
;ButtonPushed=
Return Dialog("dlgHttpDownload")
#EndFunction

;---------------------------------------------------------------------------------------------
#DefineSubroutine procHttpDownload(DialogHandle_HttpDownload, EventCode_HttpDownload, ControlNum_HttpDownload, Res4, Res5)
;---------------------------------------------------------------------------------------------
;This function is called from above - only the function above should be called directly.
; (Dialog callback routine)

DIALOG_INIT =       0
DIALOG_TIMERTICK =  1
DIALOG_PUSH =       2

switch(EventCode_HttpDownload)

   case DIALOG_INIT
		doonce=@TRUE
		x=0
   	DialogProcOptions(DialogHandle_HttpDownload, DIALOG_TIMERTICK, 1)
   	DialogProcOptions(DialogHandle_HttpDownload, DIALOG_PUSH, 1)
	 	break

   case DIALOG_PUSH
      if  ControlNum_HttpDownload == 1 ;CANCEL
			gosub FinishDownload
			if FileExist(localfile) then FileDelete(localfile)
			return 99
		endif
		break

	case DIALOG_TIMERTICK
		x=x+1
		if isdefined(doonce) then
			hProgBar=udfProgressBarCreate(3,20,50,380,10,1,titlebar)
			percentdone=0
			gosub StartDownload
			drop(doonce)
		endif
		StartTime=TimeYmdHms()
		sofar=percentdone
		while TimeYmdHms()==StartTime
		    r=dllcall(wininet,long:"InternetReadFile",long:openreqhandle,lpbinary:databuf,long:btoread,lpbinary:breadbuf) 
			 bread=BinaryPeek4(breadbuf,0)
		    totalread=totalread+bread
			 DialogControlSet(DialogHandle_HttpDownload, 5, 3, StrCat(totalread," of ",size," bytes"))
		    dllcall(kernel32,long:"WriteFile",long:hfile,lpbinary:databuf,long:bread,lpbinary:bwrittenbuf,lpnull)
			 if bread==0 then break 
		endwhile
		percentdone=int(%totalread%.00/size*100)
		if percentdone>sofar
			for x = (sofar+1) to percentdone
				udfProgressBarTick(hProgBar)
			next x
		endif
		if bread==0
			gosub FinishDownload
			return 1 ;success (file complete)
		endif
		break
endswitch
return -2

:StartDownload
;dll handles
wininet=dllload(StrCat(DirWindows(1),"wininet.dll"))
kernel32=dllload(StrCat(DirWindows(1),"kernel32.dll"))

;init connection
tophandle=dllcall(wininet,long:"InternetOpenA",lpstr:"Microsoft Internet Explorer",long:1,lpnull,lpnull,long:0) ;INTERNET_OPEN_TYPE_DIRECT
connecthandle=dllcall(wininet,long:"InternetConnectA",long:tophandle,lpstr:host,long:80,lpstr:username,lpstr:password,long:3,long:0,long:0) ;INTERNET_DEFAULT_HTTP_PORT  INTERNET_SERVICE_HTTP
openreqhandle=dllcall(wininet,long:"HttpOpenRequestA",long:connecthandle,lpstr:"GET",lpstr:object,lpnull,lpstr:referer,lpnull,long:67108864,long:0) ;INTERNET_FLAG_NO_CACHE_WRITE

;set recieve timeout
toutbuf=binaryalloc(4)
binarypoke4(toutbuf,0,timeout*1000)
dllcall(wininet,long:"InternetSetOptionA",long:openreqhandle,long:6,lpbinary:toutbuf,long:4) ;INTERNET_OPTION_RECEIVE_TIMEOUT
binaryfree(toutbuf)

sendreq=dllcall(wininet,long:"HttpSendRequestA",long:openreqhandle,lpstr:"",long:0,lpstr:"",long:0)
;internet error
if sendreq==0 
  dllcall(wininet,long:"InternetCloseHandle",long:tophandle)
  dllfree(wininet)
  dllfree(kernel32)
  return 0 ;-3
endif
  
;query status code
indexbuf=binaryalloc(4)
infobuf=binaryalloc(4)
infobufsize=binaryalloc(4)
binarypoke4(infobufsize,0,4)
dllcall(wininet,long:"HttpQueryInfoA",long:openreqhandle,long:19|536870912,lpbinary:infobuf,lpbinary:infobufsize,lpbinary:indexbuf) ;HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER
stcode=binarypeek4(infobuf,0)

;query size
binarypoke4(indexbuf,0,0)
binarypoke4(infobuf,0,0)
dllcall(wininet,long:"HttpQueryInfoA",long:openreqhandle,long:5|536870912,lpbinary:infobuf,lpbinary:infobufsize,lpbinary:indexbuf) ;HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER
size=binarypeek4(infobuf,0)
if size==0 then size="Unknown"
binaryfree(infobuf)
binaryfree(indexbuf)
binaryfree(infobufsize)
 
if stcode==200 ;OK
  
  ;create localfile
  hfile=dllcall(kernel32,long:"CreateFileA",lpstr:localfile,long:1073741824,long:0,lpnull,long:2,long:128,lpnull) ;GENERIC_WRITE  CREATE_ALWAYS  FILE_ATTRIBUTE_NORMAL
  ;file error
  if hfile==-1
    dllcall(wininet,long:"InternetCloseHandle",long:tophandle)
    dllfree(wininet)
    dllfree(kernel32) 
 	 return 0 ;hfile
  endif

;  boxtitle(udfNiceName(object))
    
  ;download
  totalread=0
  bread=""
  btoread=10000
  databuf=binaryalloc(btoread)
  breadbuf=binaryalloc(4)
  bwrittenbuf=binaryalloc(4)
else
  dllcall(wininet,long:"InternetCloseHandle",long:tophandle)
  dllfree(wininet)
  dllfree(kernel32) 
  return 0 ;stcode ;failure (no download attempt <>200)
endif
return

:FinishDownload
  ;close file
  dllcall(kernel32,long:"_lclose",long:hfile)
  ;free mem
  binaryfree(databuf)
  binaryfree(breadbuf)
  binaryfree(bwrittenbuf)
  ;close ihandles
  dllcall(wininet,long:"InternetCloseHandle",long:tophandle)
  ;free dlls
  dllfree(wininet)
  dllfree(kernel32)

  if r==0 then return 0 ;-2 ;timeout error
  ;else return stcode ;success
return

#EndSubroutine

Article ID:   W16231
File Created: 2004:03:30:15:43:30
Last Updated: 2004:03:30:15:43:30