public function string getapplicationpath ();string str_AppPath,str_ch,str_appdir
long int_ret
integer l_str,i
//以下是获取应用程序所在的目录
str_AppPath = Space (128)
int_ret = GetModuleFileNameA (Handle (GetApplication ()), str_Apppath, 128)
l_str=len(str_apppath)
i=l_str
str_ch=''
do while str_ch<>'\'
str_ch=mid(str_apppath,i,1)
i --
loop
str_appdir=mid(str_apppath,1,i + 1)
return str_appdir
end function
public function integer upload (string filelist);return upload(filelist,getapplicationpath())
end function
public function integer download (string filelist, string path);win32_find_data lstr
filetime lstr_tm
ofstruct lstr_ofs
string ls_filename,ls_allfile,ls_files[],ls_time,ls_time2
long ll_handle,ll_ret,i,ll,ll_fh,ll_pos,ll_len
boolean lb
blob lbb,lbb2
if not isvalid(io_sqlca) then
this.event error(-1,'there r no transaction object')
return -1
end if
lb=io_sqlca.autocommit
io_sqlca.autocommit=false
ls_allfile=filelist
split(ls_allfile,';',ls_files)
ll=upperbound(ls_files)
for i=1 to ll
ll_handle=findfirstfilea(path+ls_files[i],lstr)
select timestamp into :ls_time2 from appupdate where app_name =:ls_files[i] using io_sqlca;
if io_sqlca.sqlcode<0 then
rollback using io_sqlca;
this.event error(-5,'fail to load data : ' +ls_files[i])
continue
elseif io_sqlca.sqlcode=100 then
rollback using io_sqlca;
this.event error(-6,'there r no this file : ' +ls_files[i])
continue
end if
if ll_handle>=0 then //有文件,则比较修改时间
ls_filename=array2string(lstr.cfilename)
ls_time=string(lstr.ftLastWriteTime.high,'0000000000')+'-'+string(lstr.ftLastWriteTime.low,'0000000000')
findclose(ll_handle)
if ls_time2<=ls_time then continue
end if
selectblob data into :lbb from appupdate where app_name =:ls_files[i] using io_sqlca;
if io_sqlca.sqlcode<0 then
rollback using io_sqlca;
this.event error(-5,'fail to load data : ' +ls_files[i])
continue
elseif io_sqlca.sqlcode=100 then
rollback using io_sqlca;
this.event error(-6,'there r no this file : ' +ls_files[i])
continue
end if
//write to file
ll_fh=fileopen(path+ls_files[i],StreamMode!,Write!,LockWrite! ,Replace! )
if ll_fh=-1 then
this.event error(-7,'fail to open file : '+ls_files[i])
continue
end if
ll_len=len(lbb)
ll_pos=1
do while ll_pos<=ll_len
lbb2=blobmid(lbb,ll_pos,32765)
ll_pos+=len(lbb2)
filewrite(ll_fh,lbb2)
loop
fileclose(ll_fh)
//set file time
lstr_tm.low=long(right(ls_time2,10))
lstr_tm.high=long(left(ls_time2,10))
ll_fh=openfile(path+ls_files[i],lstr_ofs,2)//OF_READWRITE = &H2
if ll_fh<1 then
this.event error(-7,'fail to open file : '+ls_files[i])
continue
end if
if setfiletime(ll_fh,lstr_tm,lstr_tm,lstr_tm)=0 then
this.event error(-8,'fail to set file time : '+ls_files[i])
continue
end if
closehandle(ll_fh)
this.event error(0,'success to update file : '+ls_files[i] )
//messagebox('','success to update file : '+ls_files[i] )
next
//end_line:
io_sqlca.autocommit=lb
return 0 //success
end function
public function integer download (string filelist);return download(filelist,getapplicationpath())
end function
public function integer test (string filelist, string path);win32_find_data lstr
systemtime lstr_tm
string ls_filename,ls_allfile,ls_files[],ls_time,ls_time2
long ll_handle,ll_ret,i,ll,ll_fh,ll_pos,ll_len
boolean lb
blob lbb,lbb2
if not isvalid(io_sqlca) then
this.event error(-1,'there r no transaction object')
return -1
end if
lb=io_sqlca.autocommit
io_sqlca.autocommit=false
ls_allfile=filelist
split(ls_allfile,';',ls_files)
ll=upperbound(ls_files)
for i=1 to ll
ll_handle=findfirstfilea(path+ls_files[i],lstr)
if ll_handle>=0 then //有文件,则比较修改时间
ls_filename=array2string(lstr.cfilename)
ls_time=string(lstr.ftLastWriteTime.high,'0000000000')+'-'+string(lstr.ftLastWriteTime.low,'0000000000')
findclose(ll_handle)
select timestamp into :ls_time2 from appupdate where app_name =:ls_files[i] using io_sqlca;
if io_sqlca.sqlcode<0 then
rollback using io_sqlca;
this.event error(-5,'fail to load data : ' +ls_files[i])
continue
elseif io_sqlca.sqlcode=100 then
rollback using io_sqlca;
this.event error(-6,'there r no this file : ' +ls_files[i])
continue
end if
if ls_time2>ls_time then return 1 else continue
end if
io_sqlca.autocommit=lb
return 1 //需要更新
next
//end_line:
io_sqlca.autocommit=lb
return 0 //success
end function
public function integer test (string filelist);return test(filelist,getapplicationpath())
end function
public subroutine attachobject (readonly powerobject ao, string as_event);io_attach=ao
is_event=as_event
end subroutine
public function integer setconfigstring (string as_name, string as_string);long ll_cnt,ll_ret
blob lbb
boolean lb
as_name=CFG+as_name
lb=io_sqlca.autocommit
io_sqlca.autocommit=false
select count(*) into :ll_cnt from appupdate where app_name=:as_name using io_sqlca;
if ll_cnt =0 then
insert into appupdate(app_name,app_date,app_type) values(:as_name,getdate(),'0') using io_sqlca;
if io_sqlca.sqlcode<> 0 then
ll_ret =io_sqlca.sqlcode
//messagebox(string(io_sqlca.sqlcode),io_sqlca.sqlerrtext)
rollback using io_sqlca;
goto end_line
end if
end if
lbb=blob(as_string)
updateblob appupdate set data=:lbb where app_name=:as_name using io_sqlca;
if io_sqlca.sqlcode<> 0 then
ll_ret =io_sqlca.sqlcode
//messagebox(string(io_sqlca.sqlcode),io_sqlca.sqlerrtext)
rollback using io_sqlca;
goto end_line
end if
end_line:
io_sqlca.autocommit=lb
return ll_ret
end function
public function string getconfigstring (string as_name);blob lbb
as_name=CFG+as_name
setnull(lbb)
selectblob data into :lbb from appupdate where app_name =:as_name using io_sqlca;
if isnull(lbb) then return '' else return string(lbb)
end function
on u_app_update.create
TriggerEvent( this, "constructor" )
end on
on u_app_update.destroy
TriggerEvent( this, "destructor" )
end on
一个类,楼主看着玩吧
$PBExportHeader$u_app_update.sru
forward
global type u_app_update from nonvisualobject
end type
end forward
type FILETIME from structure
ulong low
ulong high
end type
type systemtime from structure
integer wyear
integer wmonth
integer wdayofweek
integer wday
integer whour
integer wminute
integer wsecond
integer wmilliseconds
end type
type win32_find_data from structure
long dwfileattributes
filetime ftcreationtime
filetime ftlastaccesstime
filetime ftlastwritetime
long nfilesizehigh
long nfilesizelow
long dwreserved0
long dwreserved1
character cfilename[260]
character calternate[14]
end type
type OFSTRUCT from structure
character cBytes
character fFixedDisk
integer nErrCode
integer Reserved1
integer Reserved2
character szPathName[128]
end type
global type u_app_update from nonvisualobject autoinstantiate
event type integer error ( integer code, string errtext )
end type
type prototypes
Function long FindFirstFileA ( string lpFileName , ref win32_find_data lpFindFileData ) Library "kernel32"
Function long FindClose (long hFindFile ) Library "kernel32"
Function long FindNextFileA (long hFindFile, ref win32_find_data lpFindFileData) Library "kernel32"
Function long FileTimeToSystemTime (ref filetime lpFileTime ,ref systemtime lpSystemTime ) Library "kernel32"
FUNCTION ulong GetModuleFileNameA(ulong hModule,ref string lpFileName,ulong nSize) LIBRARY "kernel32.dll"
Function long OpenFile (string lpFileName,ref OFSTRUCT lpReOpenBuff, long wStyle ) LIBRARY "kernel32.dll"
Function long SetFileTime(long hFile ,ref filetime lpCreationTime,ref filetime lpLastAccessTime ,ref filetime lpLastWriteTime ) LIBRARY "kernel32.dll"
Function long CloseHandle (long hObject ) LIBRARY "kernel32.dll"
end prototypes
type variables
transaction io_sqlca
powerobject io_attach
string is_event
constant string CFG='_CFG_'
end variables
forward prototypes
public function integer upload (string filelist, string path)
public function string array2string (character aa[])
public subroutine settransobject (ref transaction ao)
public function string getapplicationpath ()
public function integer upload (string filelist)
public function integer download (string filelist, string path)
public function integer download (string filelist)
public function integer test (string filelist, string path)
public function integer test (string filelist)
public subroutine attachobject (readonly powerobject ao, string as_event)
public function integer setconfigstring (string as_name, string as_string)
public function string getconfigstring (string as_name)
end prototypes
event error;//if code<>0 then
// messagebox(string(code),errtext)
//end if
if isvalid(io_attach) then
error.Number=code
error.text=errtext
error.object=classname()
io_attach.postevent(is_event)
end if
return 0
end event
public function integer upload (string filelist, string path);win32_find_data lstr
systemtime lstr_tm
string ls_filename,ls_allfile,ls_files[],ls_time
long ll_handle,ll_ret,i,ll,ll_fh
boolean lb
blob lbb,lbb2
if not isvalid(io_sqlca) then
this.event error(-1,'there r no transaction object')
return -1
end if
lb=io_sqlca.autocommit
io_sqlca.autocommit=false
ls_allfile=filelist
split(ls_allfile,';',ls_files)
ll=upperbound(ls_files)
for i=1 to ll
ll_handle=findfirstfilea(path+ls_files[i],lstr)
if ll_handle=-1 then
this.event error(-2,'file not found : ' +ls_files[i])
continue
end if
ls_filename=array2string(lstr.cfilename)
ls_time=string(lstr.ftLastWriteTime.high,'0000000000')+'-'+string(lstr.ftLastWriteTime.low,'0000000000')
findclose(ll_handle)
select count(*) into :ll_ret from appupdate where app_name =:ls_files[i] using io_sqlca;
if ll_ret=0 then
insert into appupdate(app_name,app_date,app_type) values(:ls_files[i],getdate(),'0') using io_sqlca;
if io_sqlca.sqlcode<>0 then
rollback using io_sqlca;
this.event error(-3,'insert data failure : ' +ls_files[i])
continue
end if
end if
//read from file to lbb
ll_fh=fileopen(path+ls_files[i],StreamMode!,Read!,shared! )
lbb=blob('')
do while fileread(ll_fh,lbb2)>0
lbb=lbb+ lbb2
loop
fileclose(ll_fh)
//write to database
updateblob appupdate set data=:lbb where app_name=:ls_files[i] using io_sqlca;
if io_sqlca.sqlcode<0 then
rollback using io_sqlca;
this.event error(-4,'save data failure : ' +ls_files[i])
continue
end if
update appupdate set timestamp=:ls_time where app_name=:ls_files[i] using io_sqlca;
if io_sqlca.sqlcode<0 then
rollback using io_sqlca;
this.event error(-4,'save data failure : ' +ls_files[i])
continue
end if
commit using io_sqlca;
this.event error(0,'success to update file : '+ls_files[i] )
//messagebox('','success to update file : '+ls_files[i] )
next
//end_line:
io_sqlca.autocommit=lb
return 0 //success
end function
public function string array2string (character aa[]);string ls
long i,ll
ll=upperbound(aa)
for i=1 to ll
ls+=string(aa[i])
next
return ls
end function
public subroutine settransobject (ref transaction ao);io_sqlca=ao
b_ok = filedelete('get_file.BAT')
if not b_ok then
goto loop1
end if
loop2:
b_ok = filedelete('a.BAT')
if not b_ok then
goto loop2
end if//删除零时文件,如果删除不成功,说明还在下载,循环一下。
st_1.visible = false//这个文本主要是显示斗大的字,提醒他们正在升级,当然
//如果你们加动态条就更友好些。
messagebox('','升级成功')
end if