一、STRUCTURE定义
1、s_filetime
global type s_filetime from structure
long dwlowdatetime
long dwhighdatetime
end type
2、s_win32_find_data
global type s_win32_find_data from structure
long dwfileattributes
s_filetime ftcreationtime
s_filetime ftlastaccesstime
s_filetime ftlastwritetime
long nfilesizehigh
long nfilesizelow
long dwreserved0
long dwreserved1
character cfilename[255]
character calternatefilename[14]
end type
3、s_netresource
global type s_netresource from structure
long dwScope
long dwType
long dwDisplayType
long dwUsage
string lpLocalName
string lpRemoteName
string lpComment
string lpProvider
end type
二、API说明及定义
一、WNetAddConnection2A
function ulong WNetAddConnection2A(ref s_netresource lpNetResource, String lpPassword, String lpUserName, long dwFlags) library "mpr.dll" Alias for "WNetAddConnection2A"
说明:
VB声明
Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
说明
创建同一个网络资源的连接
返回值
Long,零表示成功。>0表示失败
参数表
参数 类型及说明
lpNetResource 在这个结构中设置了下述字段,对要连接的网络资源进行了定义:dwType, lpLocalName (可为 vbNullString), lpRemoteName, lpProvider (设为 vbNullString 表示用默认提供者)。该结构的其他所有变量都会被忽略
lpPassword String,可选的一个密码。如为vbNullString,表示采用当前用户的默认密码。如为一个空字串,则不用任何密码
lpUserName String,用于连接的用户名。如为vbNullString,表示使用当前用户
dwFlags Long,设为零;或指定常数CONNECT_UPDATE_PROFILE,表示创建永久性连接
2、WNetGetConnectionA
function ulong WNetGetConnectionA(String lpszLocalName, String lpszRemoteName, long cbRemoteName) library "mpr.dll"
说明:
VB声明
Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
说明
获取本地或已连接的一个资源的网络名称
返回值
Long,零表示成功。>0表示失败。
参数表
参数 类型及说明
lpszLocalName String,本地设备的名字
lpszRemoteName String,指定一个字串缓冲区,用于装载设备的资源名称
cbRemoteName Long,lpszRemoteName缓冲区的字符数量。如缓冲区不够大,则设为需要的缓冲区长度
3、FindFirstFileA
function ulong FindFirstFileA(string lpFileName, REF s_WIN32_FIND_DATA lpFindFileData) Library "kernel32.dll"
VB声明
Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
说明
根据文件名查找文件
返回值
Long,如执行成功,返回一个搜索句柄。如果出错,返回一个INVALID_HANDLE_VALUE常数,一旦不再需要,应该用FindClose函数关闭这个句柄
参数表
参数 类型及说明
lpFileName String,欲搜索的文件名。可包含通配符,并可包含一个路径或相对路径名
lpFindFileData WIN32_FIND_DATA,这个结构用于装载与找到的文件有关的信息。该结构可用于后续的搜索
注解
由这个函数返回的句柄可以作为一个参数用于FindNextFile函数。这样一来,就可以方便的枚举出与lpFileName参数指定的文件名相符的所有文件
4、FindClose
function ulong FindClose(ulong hFindFile) Library "kernel32.dll"
VB声明
Declare Function FindClose Lib "kernel32" Alias "FindClose" (ByVal hFindFile As Long) As Long
说明
关闭由FindFirstFile函数创建的一个搜索句柄
返回值
Long,非零表示成功,零表示失败。会设置GetLastError
参数表
参数 类型及说明
hFindFile Long,由FindFirstFile函数提供的搜索句柄
5、CompareFileTime
function ulong CompareFileTime( REF s_filetime lpFileTime1, REF s_filetime lpFileTime2) library "kernel32.dll" Alias for "CompareFileTime"
VB声明Declare Function CompareFileTime Lib "kernel32" Alias "CompareFileTime" (lpFileTime1 As FILETIME, lpFileTime2 As FILETIME) As Long说明根据FILETIME结构的信息,对比两个文件的时间返回值Long,如两个时间相等,就返回零;如lpFileTime1小于lpFileTime2,返回-1;如lpFileTime2小于lpFileTime1,返回1参数表参数类型及说明lpFileTime1FILETIME,参考FILETIMElpFileTime26、CopyFileA
function ulong CopyFileA(string lpExistingFileName, string lpNewFileName, long bFailIfExists) library "kernel32.dll" Alias for "CopyFileA"
VB声明Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long说明复制文件。与vb的filecopy命令相似返回值Long,非零表示成功,零表示失败。会设置GetLastError参数表参数类型及说明lpExistingFileNameString,源文件名lpNewFileNameString,目标文件名bFailIfExistsLong,如果设为TRUE(非零),那么一旦目标文件已经存在,则函数调用会失败。否则目标文件被改写
三、连接函数F_CONNECT_COPYSERVER
返回路径
global type f_connect_copyserver from function_object
end type
forward prototypes
global function string f_connect_copyserver ()
end prototypes
global function string f_connect_copyserver ();
STRING GS_COPYSERVER1
STRING GS_COPYSERVER2
STRING GS_COPYSERVER3
STRING GS_COPYLOGINUSER1
STRING GS_COPYLOGINUSER2
STRING GS_COPYLOGINUSER3
STRING GS_COPYLOGINPASS1
STRING GS_COPYLOGINPASS2
STRING GS_COPYLOGINPASS3
GS_COPYSERVER1 = ProfileString("autodown.ini","downpath","COPYSERVER1","")
GS_COPYSERVER2 = ProfileString("autodown.ini","downpath","COPYSERVER2","")
GS_COPYSERVER3 = ProfileString("autodown.ini","downpath","COPYSERVER3","")
GS_COPYLOGINUSER1 = ProfileString("autodown.ini","downpath","COPYLOGINUSER1","")
GS_COPYLOGINUSER2 = ProfileString("autodown.ini","downpath","COPYLOGINUSER2","")
GS_COPYLOGINUSER3 = ProfileString("autodown.ini","downpath","COPYLOGINUSER3","")
GS_COPYLOGINPASS1 = ProfileString("autodown.ini","downpath","COPYLOGINPASS1","")
GS_COPYLOGINPASS2 = ProfileString("autodown.ini","downpath","COPYLOGINPASS2","")
GS_COPYLOGINPASS3 = ProfileString("autodown.ini","downpath","COPYLOGINPASS3","")
s_netresource ss_net
ss_net.dwScope = 0
ss_net.dwType = 0//
ss_net.dwDisplayType = 0
ss_net.dwUsage = 0
ss_net.lpLocalName = \'\'//
ss_net.lpRemoteName = GS_COPYSERVER1//
ss_net.lpComment = \'\'
ss_net.lpProvider = \'\'//
long ll_ret
string ls_path
ls_path = \'\'
ll_ret = WNetGetConnectionA(\'\',GS_COPYSERVER1,255)
if ll_ret <> 0 then
ll_ret = WNetGetConnectionA(\'\',GS_COPYSERVER2,255)
if ll_ret <> 0 then
ll_ret = WNetGetConnectionA(\'\',GS_COPYSERVER3,255)
if ll_ret = 0 then
return GS_COPYSERVER3
end if
else
return GS_COPYSERVER2
end if
else
return GS_COPYSERVER1
end if
//gl_handle,
ll_ret = WNetAddConnection2A(ss_net,GS_COPYLOGINPASS1,GS_COPYLOGINUSER1,1)
if ll_ret <> 0 then
ss_net.dwScope = 0
ss_net.dwType = 0//
ss_net.dwDisplayType = 0
ss_net.dwUsage = 0
ss_net.lpLocalName = \'\'//
ss_net.lpRemoteName = GS_COPYSERVER2//
ss_net.lpComment = \'\'
ss_net.lpProvider = \'\'//
ll_ret = WNetAddConnection2A(ss_net,GS_COPYLOGINPASS2,GS_COPYLOGINUSER2,1)
if ll_ret <> 0 then
ss_net.dwScope = 0
ss_net.dwType = 0//
ss_net.dwDisplayType = 0
ss_net.dwUsage = 0
ss_net.lpLocalName = \'\'//
ss_net.lpRemoteName = GS_COPYSERVER3//
ss_net.lpComment = \'\'
ss_net.lpProvider = \'\'//
ll_ret = WNetAddConnection2A(ss_net,GS_COPYLOGINPASS3,GS_COPYLOGINUSER3,1)
if ll_ret <> 0 then
//messagebox(\'错误!\',\'连接服务器错误!\',stopsign!)
return ls_path
end if
ls_path = GS_COPYSERVER3
else
ls_path = GS_COPYSERVER2
end if
else
ls_path = GS_COPYSERVER1
end if
if isnull(ls_path) then ls_path = \'\'
//messagebox(\'\',ls_path)
return ls_path
end function
四、统计复制文件数量函数F_CONNECT_COPYCOUNT
需要控件:LISTBOX(3个)
返回值:需要下载的文件个数。
global type f_connect_copycount from function_object
end type
forward prototypes
global function integer f_connect_copycount (string as_path, listbox lb_filecopy1, listbox lb_filecopy2, listbox lb_filecopy3)
end prototypes
global function integer f_connect_copycount (string as_path, listbox lb_filecopy1, listbox lb_filecopy2, listbox lb_filecopy3);
string ls_directorya1,ls_directorya2
string ls_filename,ls_file
ls_directorya1 = as_path
ls_directorya2 = gs_application_path
string ls_directoryb1,ls_directoryb2
string ls_directoryc1,ls_directoryc2
string ls_filename1,ls_filename2
long ll_filecounta1,ll_filecounta2
long ll_filecountb1,ll_filecountb2
long ll_filecountc1,ll_filecountc2
long ll_cnt1
long ll_cnt2
long ll_cnt3
s_filetime sf_createtime1,sf_createtime2
s_filetime sf_accesstime1,sf_accesstime2
s_filetime sf_writetime1,sf_writetime2
s_WIN32_FIND_DATA ss_file1,ss_file2
long ll_filehandle1,ll_filehandle2
long ll_k
ls_file = ls_directorya1 + \'*.*\'
If Not lb_filecopy1.DirList(ls_file, 0+1+2+16+32) Then
Return -1
End If
ll_filecounta1 = lb_filecopy1.TotalItems()
ll_k = 0
For ll_cnt1 = 1 To ll_filecounta1
ls_File = lb_filecopy1.Text(ll_cnt1)
if left(ls_file,1) = \'[\' then
ls_filename = right(ls_file,len(ls_file) - 1)
ls_filename = left(ls_filename,len(ls_filename) - 1)
if ls_filename = \'..\' then continue
ls_directoryb1 = ls_directorya1 + \'\' + ls_filename
ls_directoryb2 = ls_directorya2 + \'\' + ls_filename
if directoryexists(ls_directoryb2) = false then
ll_k++
end if
ls_file = ls_directoryb1 + \'*.*\'
lb_filecopy2.DirList(ls_file, 0+1+2+16)
ll_filecountb1 = lb_filecopy2.TotalItems()
for ll_cnt2 = 1 to ll_filecountb1
ls_File = lb_filecopy2.Text(ll_cnt2)
if left(ls_file,1) = \'[\' then
ls_filename = right(ls_file,len(ls_file) - 1)
ls_filename = left(ls_filename,len(ls_filename) - 1)
if ls_filename = \'..\' then continue
ls_directoryc1 = ls_directoryb1 + \'\' + ls_filename
ls_directoryc2 = ls_directoryb2 + \'\' + ls_filename
if directoryexists(ls_directoryc2) = false then
ll_k++
end if
ls_file = ls_directoryc1 + \'*.*\'
lb_filecopy3.DirList(ls_file, 0+1+2+16)
ll_filecountc1 = lb_filecopy3.TotalItems()
for ll_cnt3 = 1 to ll_filecountc1
ls_File = lb_filecopy3.Text(ll_cnt3)
if ls_file = \'[..]\' then continue
//判断文件,比较和下载
ls_filename1 = ls_directoryc1 + \'\' + ls_file
ls_filename2 = ls_directoryc2 + \'\' + ls_file
if fileexists(ls_filename2) = false then
ll_k++
else
ll_filehandle1 = FindFirstFileA(ls_filename1,ss_file1)
FindClose(ll_filehandle1)
ll_filehandle2 = FindFirstFileA(ls_filename2,ss_file2)
FindClose(ll_filehandle2)
choose case CompareFileTime(ss_file1.ftlastwritetime,ss_file2.ftlastwritetime)
case 0
//两个时间相等,就返回零
case 1
//如lpFileTime2小于lpFileTime1,返回1
//下载
ll_k++
case -1
//如lpFileTime1小于lpFileTime2,返回-1
end choose
end if
next
else
//判断文件,比较和下载
ls_filename1 = ls_directoryb1 + \'\' + ls_file
ls_filename2 = ls_directoryb2 + \'\' + ls_file
if fileexists(ls_filename2) = false then
ll_k++
else
ll_filehandle1 = FindFirstFileA(ls_filename1,ss_file1)
FindClose(ll_filehandle1)
ll_filehandle2 = FindFirstFileA(ls_filename2,ss_file2)
FindClose(ll_filehandle2)
choose case CompareFileTime(ss_file1.ftlastwritetime,ss_file2.ftlastwritetime)
case 0
//两个时间相等,就返回零
case 1
//如lpFileTime2小于lpFileTime1,返回1
//下载
ll_k++
case -1
//如lpFileTime1小于lpFileTime2,返回-1
end choose
end if
end if
next
else
//判断文件,比较和下载
ls_filename1 = ls_directorya1 + \'\' + ls_file
ls_filename2 = ls_directorya2 + \'\' + ls_file
if fileexists(ls_filename2) = false then
ll_k++
else
ll_filehandle1 = FindFirstFileA(ls_filename1,ss_file1)
FindClose(ll_filehandle1)
ll_filehandle2 = FindFirstFileA(ls_filename2,ss_file2)
FindClose(ll_filehandle2)
choose case CompareFileTime(ss_file1.ftlastwritetime,ss_file2.ftlastwritetime)
case 0
//两个时间相等,就返回零
case 1
//如lpFileTime2小于lpFileTime1,返回1
//下载
choose case lower(ls_file)
case \'hbky_autodown.exe\' //下载程序
case \'hbky_autodown.pbd\'
//case \'autodown.ini\'
case else
ll_k++
end choose
case -1
//如lpFileTime1小于lpFileTime2,返回-1
end choose
end if
end if
Next
return ll_k
end function
五、复制函数F_CONNECT_COPYFILE
需要控件:LISTBOX(3个)
需要INI文件:AUTODOWN.INI
global type f_connect_copyfile from function_object
end type
forward prototypes
global function integer f_connect_copyfile (string as_path, hprogressbar hpb_1, long al_copycount, listbox lb_filecopy1, listbox lb_filecopy2, listbox lb_filecopy3)
end prototypes
global function integer f_connect_copyfile (string as_path, hprogressbar hpb_1, long al_copycount, listbox lb_filecopy1, listbox lb_filecopy2, listbox lb_filecopy3);
string ls_user
string ls_pass
string ls_directorya1,ls_directorya2
string ls_directoryb1,ls_directoryb2
string ls_directoryc1,ls_directoryc2
ls_directorya1 = as_path
ls_directorya2 = gs_application_path
string ls_filename,ls_file
string ls_filename1,ls_filename2
long ll_filecounta1,ll_filecounta2
long ll_filecountb1,ll_filecountb2
long ll_filecountc1,ll_filecountc2
long ll_cnt1
long ll_cnt2
long ll_cnt3
long ll_m
long ll_ret = 1
s_WIN32_FIND_DATA ss_file1,ss_file2
long ll_filehandle1,ll_filehandle2
ls_file = ls_directorya1 + \'*.*\'
If Not lb_filecopy1.DirList(ls_file, 0+1+2+16) Then
Return 0
End If
hpb_1.position = 0
ll_filecounta1 = lb_filecopy1.TotalItems()
ll_m = 0
if al_copycount = 0 then al_copycount = 1
For ll_cnt1 = 1 To ll_filecounta1
ls_File = lb_filecopy1.Text(ll_cnt1)
w_autodown_test.st_2.text = ls_file
Yield()
if left(ls_file,1) = \'[\' then
ls_filename = right(ls_file,len(ls_file) - 1)
ls_filename = left(ls_filename,len(ls_filename) - 1)
if ls_filename = \'..\' then continue
ls_directoryb1 = ls_directorya1 + \'\' + ls_filename
ls_directoryb2 = ls_directorya2 + \'\' + ls_filename
if directoryexists(ls_directoryb2) = false then
CreateDirectory(ls_directoryb2)
ll_m++
hpb_1.position = ll_m / al_copycount * 100
end if
ls_file = ls_directoryb1 + \'*.*\'
lb_filecopy2.DirList(ls_file, 0+1+2+16)
ll_filecountb1 = lb_filecopy2.TotalItems()
for ll_cnt2 = 1 to ll_filecountb1
ls_File = lb_filecopy2.Text(ll_cnt2)
w_autodown_test.st_2.text = ls_file
Yield()
if left(ls_file,1) = \'[\' then
ls_filename = right(ls_file,len(ls_file) - 1)
ls_filename = left(ls_filename,len(ls_filename) - 1)
if ls_filename = \'..\' then continue
ls_directoryc1 = ls_directoryb1 + \'\' + ls_filename
ls_directoryc2 = ls_directoryb2 + \'\' + ls_filename
if directoryexists(ls_directoryc2) = false then
CreateDirectory(ls_directoryc2)
ll_m++
hpb_1.position = ll_m / al_copycount * 100
end if
ls_file = ls_directoryc1 + \'*.*\'
lb_filecopy3.DirList(ls_file, 0+1+2+16)
ll_filecountc1 = lb_filecopy3.TotalItems()
for ll_cnt3 = 1 to ll_filecountc1
ls_File = lb_filecopy3.Text(ll_cnt3)
w_autodown_test.st_2.text = ls_file
if ls_file = \'[..]\' then continue
//判断文件,比较和下载
ls_filename1 = ls_directoryc1 + \'\' + ls_file
ls_filename2 = ls_directoryc2 + \'\' + ls_file
Yield()
if fileexists(ls_filename2) = false then
CopyFileA(ls_filename1,ls_filename2,0)
ll_m++
hpb_1.position = ll_m / al_copycount * 100
else
ll_filehandle1 = FindFirstFileA(ls_filename1,ss_file1)
FindClose(ll_filehandle1)
ll_filehandle2 = FindFirstFileA(ls_filename2,ss_file2)
FindClose(ll_filehandle2)
choose case CompareFileTime(ss_file1.ftlastwritetime,ss_file2.ftlastwritetime)
case 0
//两个时间相等,就返回零
case 1
//如lpFileTime2小于lpFileTime1,返回1
//下载
CopyFileA(ls_filename1,ls_filename2,0)
ll_m++
hpb_1.position = ll_m / al_copycount * 100
case -1
//如lpFileTime1小于lpFileTime2,返回-1
end choose
end if
next
else
//判断文件,比较和下载
ls_filename1 = ls_directoryb1 + \'\' + ls_file
ls_filename2 = ls_directoryb2 + \'\' + ls_file
if fileexists(ls_filename2) = false then
CopyFileA(ls_filename1,ls_filename2,0)
ll_m++
hpb_1.position = ll_m / al_copycount * 100
else
ll_filehandle1 = FindFirstFileA(ls_filename1,ss_file1)
FindClose(ll_filehandle1)
ll_filehandle2 = FindFirstFileA(ls_filename2,ss_file2)
FindClose(ll_filehandle2)
choose case CompareFileTime(ss_file1.ftlastwritetime,ss_file2.ftlastwritetime)
case 0
//两个时间相等,就返回零
case 1
//如lpFileTime2小于lpFileTime1,返回1
//下载
CopyFileA(ls_filename1,ls_filename2,0)
ll_m++
hpb_1.position = ll_m / al_copycount * 100
case -1
//如lpFileTime1小于lpFileTime2,返回-1
end choose
end if
end if
next
else
//判断文件,比较和下载
ls_filename1 = ls_directorya1 + \'\' + ls_file
ls_filename2 = ls_directorya2 + \'\' + ls_file
if fileexists(ls_filename2) = false then
CopyFileA(ls_filename1,ls_filename2,0)
ll_m++
hpb_1.position = ll_m / al_copycount * 100
else
ll_filehandle1 = FindFirstFileA(ls_filename1,ss_file1)
FindClose(ll_filehandle1)
ll_filehandle2 = FindFirstFileA(ls_filename2,ss_file2)
FindClose(ll_filehandle2)
choose case CompareFileTime(ss_file1.ftlastwritetime,ss_file2.ftlastwritetime)
case 0
//两个时间相等,就返回零
case 1
//如lpFileTime2小于lpFileTime1,返回1
//下载
choose case lower(ls_file)
case \'hbky_autodown.exe\' //下载程序
//filedelete(ls_filename2)
case \'hbky_autodown.pbd\'
//case \'autodown.ini\'
case else
CopyFileA(ls_filename1,ls_filename2,0)
ll_m++
hpb_1.position = ll_m / al_copycount * 100
end choose
case -1
//如lpFileTime1小于lpFileTime2,返回-1
end choose
end if
end if
Next
hpb_1.position = 0
return ll_ret
end function
六、AUTODOWN.INI文件
[downpath]
COPYSERVER1=\\dl-serverhbky_update
COPYSERVER2=\\dl-serverhbky_update
COPYSERVER3=
COPYLOGINUSER1=administrator
COPYLOGINUSER2=hbkyautodown
COPYLOGINUSER3=
COPYLOGINPASS1=gygsglk
COPYLOGINPASS2=hbkyautodown
COPYLOGINPASS3=
七、下载代码
STRING GS_COPYSERVER1
STRING GS_COPYSERVER2
STRING GS_COPYSERVER3
string ls_path
string ls_mypath
long ll_count = 0
GS_COPYSERVER1 = ProfileString("autodown.ini","downpath","COPYSERVER1","")
GS_COPYSERVER2 = ProfileString("autodown.ini","downpath","COPYSERVER2","")
GS_COPYSERVER3 = ProfileString("autodown.ini","downpath","COPYSERVER3","")
st_ts.text = \'正在检查需要更新软件的数量...\'
ls_path = GS_COPYSERVER1
ll_count = f_connect_copycount(ls_path,lb_files1,lb_files2,lb_files3)
if ll_count = -1 then
ls_path = GS_COPYSERVER2
ll_count = f_connect_copycount(ls_path,lb_files1,lb_files2,lb_files3)
if ll_count = -1 then
ls_path = GS_COPYSERVER3
ll_count = f_connect_copycount(ls_path,lb_files1,lb_files2,lb_files3)
if ll_count = -1 then
st_ts.text = \'正在检查是否需要更新软件版本...\'
ls_path = f_connect_copyserver()
if ls_path = \'\' then
messagebox(\'连接错误!\',\'无法连接服务器!软件不能运行!\',stopsign!)
//close(this)
return
end if
end if
end if
end if
//messagebox(\'\',ls_path)ls_path<> \'\'
if ll_count = -1 then
st_ts.text = \'正在检查需要更新软件的数量...\'
ll_count = f_connect_copycount(ls_path,lb_files1,lb_files2,lb_files3)
end if
if ll_count = -1 then
messagebox(\'连接错误!\',\'无法连接服务器!软件不能运行!\',stopsign!)
close(this)
return
end if
if ll_count = 0 then
st_ts.text = \'正在启动主程序...\'
else
st_ts.text = \'正在更新软件版本...\'
choose case f_connect_copyfile(ls_path,hpb_1,ll_count,lb_files1,lb_files2,lb_files3)
case 1
st_ts.text = \'正在启动主程序...\'
case 2 //启动参数不同,以便赋值更新程序
st_ts.text = \'正在启动主程序...\'
end choose
end if
string ls_file
ls_file = gs_application_path + \'xxxxx.exe qqaazzwwss\'
run(ls_file)
//close(this)