說(shuō)明:所有升級(jí)任務(wù)都由升級(jí)程序完成。
1.啟動(dòng)升級(jí)程序,升級(jí)程序連接到網(wǎng)站,下載新的主程序(當(dāng)然還包括支持的庫(kù)文件等)到臨時(shí)文件夾;
2.升級(jí)程序檢測(cè)舊的主程序是否活動(dòng),若活動(dòng)則關(guān)閉舊的主程序(同時(shí)記下主程序的狀態(tài));
3.刪除舊的主程序,拷貝臨時(shí)文件夾中的文件到相應(yīng)的位置,同時(shí)注冊(cè)相應(yīng)的文件;
4.檢查主程序的狀態(tài),若狀態(tài)為活動(dòng)的,則啟動(dòng)新的主程序;
5.關(guān)閉升級(jí)程序?! ?
6.祝賀你,升級(jí)完成?! ?
由于網(wǎng)友提出了關(guān)于升級(jí)這個(gè)升級(jí)程序的問(wèn)題,以下是對(duì)其思路進(jìn)行的一點(diǎn)補(bǔ)充,但原文仍然是升級(jí)主程序的,具體代碼需讀者自己添加:
7.主程序升級(jí)完畢;
8.升級(jí)程序繼續(xù)檢查所下載的臨時(shí)文件中是否含有NewUpdate.exe(新的升級(jí)程序)和rename.exe(是一個(gè)可以更改文件名的程序);
9.若存在以上兩個(gè)文件,表示要更新Update.exe文件;啟動(dòng)rename.exe程序,同時(shí)update.exe關(guān)掉自己;
10.rename.exe程序檢測(cè)update.exe是否已被關(guān)掉,若已關(guān)掉,刪除該update.exe。移動(dòng)臨時(shí)文件夾中的NewUpdate.exe文件到主程序的目錄下,同時(shí)更名為update.exe;
11.rename.exe關(guān)掉自己。
12.OK,至此升級(jí)程序也被升級(jí)了?!?
下面進(jìn)行具體的程序編寫(xiě),需建立三個(gè)工程,然后把它們編輯成一個(gè)組,三個(gè)工程需共用一個(gè)模塊?! ?
建立工程步驟:
1. 建立工程proMain:打開(kāi)vb,“新建工程”,選擇“標(biāo)準(zhǔn)EXE”, 再給工程中添加模塊,并且命名為modCommon,修改窗體名為frmMain,同時(shí)修改工程名為projMain,然后保存到某個(gè)文件夾(譬如在桌面建立個(gè)文件夾Update),窗體、模塊和工程分別保存為frmMain.frm、modCommon.bas、projMain.vbp;
2. 建立工程projNewMain:點(diǎn)擊菜單“文件 新建工程” ,選擇“標(biāo)準(zhǔn)EXE”,點(diǎn)擊菜單“工程 添加模塊”,在彈出的對(duì)話(huà)框中選擇“現(xiàn)存”標(biāo)簽,定位到Update文件夾,選中modCommon.bas模塊。修改窗體名為frmNewMain,同時(shí)修改工程名為projNewMain,然后保存到Update文件夾,窗體和工程分別保存為frmNewMain.frm、projNewMain.vbp;
3.建立工程projUpdate:點(diǎn)擊菜單“文件 新建工程” ,選擇“標(biāo)準(zhǔn)EXE”,點(diǎn)擊菜單“工程 添加模塊”,在彈出的對(duì)話(huà)框中選擇“現(xiàn)存”標(biāo)簽,定位到Update文件夾,選中modCommon.bas模塊。修改窗體名為frmUpdate,同時(shí)修改工程名為projUpdate,然后保存到Update文件夾,窗體和工程分別保存為frmUpdate.frm、projUpdate.vbp;
4.建立組:在工程projUpdate中,點(diǎn)擊菜單“文件 添加工程…”在彈出的對(duì)話(huà)框中選擇“現(xiàn)存”標(biāo)簽,定位到Update文件夾,選擇projMain.vbp;重復(fù)該動(dòng)作,選擇projNewMain.vbp;保存該組即可;
5.關(guān)閉工程,定位到Update文件夾,然后執(zhí)行下面的
各個(gè)工程文件中的文件源碼:
一、 projMain.vbp工程:
說(shuō)明:這個(gè)是舊的主程序,從來(lái)沒(méi)有進(jìn)行過(guò)升級(jí)前的程序。
用記事本打開(kāi)frmMain.frm文件,copy以下內(nèi)容到其中:
VERSION 5.00
Begin VB.Form frmMain
Caption = "請(qǐng)點(diǎn)擊升級(jí)進(jìn)行程序"
ClientHeight = 1140
ClientLeft = 60
ClientTop = 345
ClientWidth = 4500
LinkTopic = "Form1"
ScaleHeight = 1140
ScaleWidth = 4500
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "升級(jí)"
Height = 525
Left = 1380
TabIndex = 0
Top = 570
Width = 1245
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
' 升級(jí)程序的例子
' 作者: 謝家峰
' 日期: 2003/12/19
' 這里是沒(méi)有升級(jí)時(shí)的主程序
Private Sub Command1_Click()
Command1.Enabled = False
' 運(yùn)行更新程序
Shell App.Path & "\update.exe", vbNormalFocus
End Sub
Private Sub Form_Load()
If App.PrevInstance Then End
UpdateIniPath = App.Path & "\Update.ini"
' 記錄主程序的名字
WritePrivateProfileString "Main", "Name", App.EXEName, UpdateIniPath
' 記錄運(yùn)行狀態(tài)
WritePrivateProfileString "Main", "Active", "-1", UpdateIniPath
' 記錄更新次數(shù)
WritePrivateProfileString "Update", "Num", "0", UpdateIniPath
Me.Caption = App.EXEName
End Sub
Private Sub Form_Unload(Cancel As Integer)
' 記錄運(yùn)行狀態(tài)
WritePrivateProfileString "Main", "Active", "0", UpdateIniPath
End Sub
用記事本打開(kāi)modCommon.bas文件,copy以下內(nèi)容到其中:
Attribute VB_Name = "modCommon"
Option Explicit
' 升級(jí)程序的例子
' 作者: 謝家峰
' 日期: 2003/12/19
' 這里是通用模塊,放置API函數(shù)以及公用函數(shù)
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal lSize As Long, ByVal lpFilename As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As Any, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lplFilename As String) As Long
Public Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFilename As String) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_Close = &H10
Private Const gintMAX_SIZE% = 255 'Maximum buffer size Public UpdateIniPath As String
' 從資源文件中提取文件
Public Function SaveFileFromRes(vntResourceID As Variant, sType As String, sFileName As String) As Boolean
Dim bytImage() As Byte
Dim iFileNum As Integer
On Error GoTo SaveFileFromRes_Err
SaveFileFromRes = True
bytImage = LoadResData(vntResourceID, sType)
iFileNum = FreeFile
Open sFileName For Binary As iFileNum
Put #iFileNum, , bytImage
Close iFileNum
Exit Function
SaveFileFromRes_Err:
SaveFileFromRes = False: Exit Function
End Function
' 從緩沖區(qū)中讀取字符串
Private Function StringFromBuffer(Buffer As String) As String
Dim nPos As Long
nPos = InStr(Buffer, vbNullChar)
If nPos > 0 Then
StringFromBuffer = Left$(Buffer, nPos - 1)
Else
StringFromBuffer = Buffer
End If
End Function
' 讀Ini文件
Public Function ReadIniFile(ByVal strIniFile As String, ByVal strSection As String, ByVal strKey As String, Optional ByVal strKeyDefault As String = vbNullString) As String
Dim strBuffer As String
strBuffer = Space$(gintMAX_SIZE)
If GetPrivateProfileString(strSection, strKey, strKeyDefault, strBuffer, gintMAX_SIZE, strIniFile) Then
ReadIniFile = StringFromBuffer(strBuffer)
End If
End Function
' 檢查文件是否存在
Function FileExists(filename As String) As Boolean
On Error Resume Next
FileExists = (Dir$(filename) <> "")
End Function
' 改變標(biāo)簽的文本及位置
Public Function ChangeLabelPos(frm As Form, lbl As Label, msg As String)
With lbl
.Caption = msg
.Left = (frm.ScaleWidth - .Width) / 2
.Top = .Height / 2
End With
End Function
'關(guān)閉窗體
Function CloseValidForm(Ret As String) As Boolean
Dim WinWnd As Long
'搜尋該窗口的句柄
WinWnd = FindWindow(vbNullString, Ret)
If WinWnd <> 0 Then
SendMessage WinWnd, WM_Close, 0&, 0&
End If
CloseValidForm = True
End Function
二、 projNewMain.vbp工程:
說(shuō)明:這個(gè)是新的主程序,即升級(jí)后的主程序,由于我目前沒(méi)有服務(wù)器,故將該程序放在projUpdate.vbp工程的資源文件中。在projUpdate.vbp中模擬網(wǎng)絡(luò)升級(jí)?! ?
用記事本打開(kāi)frmNewMain.frm文件,copy以下內(nèi)容到其中:
VERSION 5.00
Begin VB.Form frmNewMain
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "Form1"
ClientHeight = 1365
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 1365
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "升級(jí)"
Height = 525
Left = 1410
TabIndex = 0
Top = 660
Width = 1245
End
Begin VB.Label lblCap
Caption = "告訴你,我已經(jīng)升級(jí)了,沒(méi)看出和以前不一樣了嗎?"
Height = 255
Left = 120
TabIndex = 1
Top = 60
Width = 4275
End
End
Attribute VB_Name = "frmNewMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
' 升級(jí)程序的例子
' 作者: 謝家峰
' 日期: 2003/12/19
' 這里是升級(jí)后的主程序
Private Sub Command1_Click()
Command1.Enabled = False
' 運(yùn)行更新程序
Shell App.Path & "\update.exe", vbNormalFocus
End Sub
Private Sub Form_Load()
Dim i As Integer
If App.PrevInstance Then End
UpdateIniPath = App.Path & "\Update.ini"
' 記錄主程序的名字
WritePrivateProfileString "Main", "Name", App.EXEName, UpdateIniPath
' 記錄運(yùn)行狀態(tài)
WritePrivateProfileString "Main", "Active", "-1", UpdateIniPath
Me.Caption = App.EXEName
End Sub
Private Sub Form_Unload(Cancel As Integer)
' 記錄運(yùn)行狀態(tài)
WritePrivateProfileString "Main", "Active", "0", UpdateIniPath
End Sub
三、 projUpdate.vbp工程:
說(shuō)明:這是升級(jí)程序,含有一個(gè)資源文件。
用記事本打開(kāi)frmUpdate.frm文件,copy以下內(nèi)容到其中:
VERSION 5.00
Begin VB.Form frmUpdate
BackColor = &H80000007&
BorderStyle = 0 'None
Caption = "Form1"
ClientHeight = 1185
ClientLeft = 0
ClientTop = 0
ClientWidth = 5400
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 1185
ScaleWidth = 5400
ShowInTaskbar = 0 'False
StartUpPosition = 2 '屏幕中心
Begin VB.CommandButton Command1
Appearance = 0 'Flat
Caption = "關(guān)閉"
Height = 435
Left = 2130
TabIndex = 0
Top = 630
Visible = 0 'False
Width = 1245
End
Begin VB.Label lblCap
AutoSize = -1 'True
BackStyle = 0 'Transparent
BeginProperty Font
Name = "宋體"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 240
Left = 330
TabIndex = 1
Top = 150
Width = 120
End
Begin VB.Shape Shape1
BackColor = &H00C0FFFF&
BackStyle = 1 'Opaque
BorderColor = &H0000FF00&
Height = 1065
Left = 60
Top = 60
Width = 5265
End
End
Attribute VB_Name = "frmUpdate"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
' 升級(jí)程序的例子
' 作者: 謝家峰
' 日期: 2003/12/19
' 這里是升級(jí)程序
Dim State As Boolean
Private Sub Command1_Click()
Unload Me
End Sub
Private Sub Form_Activate()
Dim i As Integer
Dim j As String
Dim OldExeFile As String
Dim NewExeFile As String
Dim tmpFile As String
Dim MainState As Boolean
DoEvents
If State Then
' 獲取舊主程序名稱(chēng)
OldExeFile = CStr(ReadIniFile(UpdateIniPath, "Main", "Name", "主程序"))
NewExeFile = OldExeFile
OldExeFile = App.Path & "\" & OldExeFile & ".exe"
tmpFile = App.Path & "\tmp.tmp"
' 改寫(xiě)升級(jí)次數(shù)
i = CInt(ReadIniFile(UpdateIniPath, "Update", "Num", "0"))
i = i + 1
ChangeLabelPos Me, lblCap, "這是您第" & i & "次升級(jí)!"
Sleep 1500
' 摸擬從網(wǎng)站下載新的更新程序。
ChangeLabelPos Me, lblCap, "正在 摸擬從網(wǎng)站下載新的升級(jí)程序 ..."
Sleep 1000
On Error Resume Next
Kill tmpFile
' 此處可以修改為將臨時(shí)文件放在臨時(shí)文件夾內(nèi)
SaveFileFromRes 101, "CUSTOM", tmpFile
Sleep 1000
On Error GoTo 0
' 檢查主程序是否開(kāi)啟,若開(kāi)啟則關(guān)閉舊程序
MainState = CBool(CInt(ReadIniFile(UpdateIniPath, "Main", "Active", "0")))
If MainState Then
' 關(guān)閉舊程序
ChangeLabelPos Me, lblCap, "正在關(guān)閉舊程序 ... "
Sleep 300
' 這里強(qiáng)行關(guān)閉舊程序
' 你也可以發(fā)送消息,讓舊程序自己關(guān)閉
Do While -1
DoEvents
If CloseValidForm(NewExeFile) Then
Exit Do
End If
Loop
Sleep 200
End If
' 刪除舊程序
On Error Resume Next
ChangeLabelPos Me, lblCap, "正在刪除舊程序 ... "
Sleep 1000
Kill OldExeFile
Sleep 1000
' 生成新主程序名稱(chēng)
'NewExeFile = CStr(ReadIniFile(UpdateIniPath, "Main", "Name", "主程序"))
j = Right(NewExeFile, 1)
If IsNumeric(j) Then
j = i
NewExeFile = Left(NewExeFile, Len(NewExeFile) - 1)
Else
j = 1
End If
' 記錄主程序的名字
NewExeFile = NewExeFile & j
WritePrivateProfileString "Main", "Name", NewExeFile, UpdateIniPath
NewExeFile = App.Path & "\" & NewExeFile & ".exe"
' 拷貝新程序
ChangeLabelPos Me, lblCap, "正在更新程序 ... "
Sleep 1000
FileCopy tmpFile, NewExeFile
Sleep 1000
' 刪除臨時(shí)程序
Kill tmpFile
' 記錄更新次數(shù)
WritePrivateProfileString "Update", "Num", CStr(i), UpdateIniPath
' 檢查舊程序的狀態(tài)
If MainState Then
ChangeLabelPos Me, lblCap, "正在啟動(dòng)更新后的主程序 ... "
Sleep 1000
Shell NewExeFile, vbNormalNoFocus
End If
' 關(guān)閉更新程序
Command1.Visible = True
ChangeLabelPos Me, lblCap, "更新完成,請(qǐng)等待 3 秒后將自動(dòng)關(guān)閉更新程序 ..."
Sleep 3000
lblCap.Refresh
Unload Me
End If
End Sub
Private Sub Form_Load()
If App.PrevInstance Then End
UpdateIniPath = App.Path & "\Update.ini"
State = True
End Sub
完成了三個(gè)工程的代碼拷貝,在代碼的必要之處我都進(jìn)行了詳細(xì)注釋?zhuān)@里我就不再介紹代碼了,現(xiàn)在進(jìn)行最后一步,模擬從網(wǎng)站下載升級(jí)后的程序。
a.打開(kāi)projMain.vbp,編譯該工程,并且命名為“主程序.exe”;
b.打開(kāi)projNewMain.vbp,編譯該工程,并且命名為“projNewMain.exe”; c. 打開(kāi)projUpdate.vbp,打開(kāi)工具欄上的“vb資源編輯器”(若沒(méi)找到,讀者需要點(diǎn)擊菜單“外接程序 外接程序管理器”,在打開(kāi)的對(duì)話(huà)框中選擇“vb6資源編輯器”,在加載行為中選擇“加載/卸載”復(fù)選框),在資源編輯器中選擇“添加自定義資源 …”,在打開(kāi)的對(duì)話(huà)框中定位到Update文件夾,選擇projNewMain.exe文件,保存該資源文件,最后編譯該工程并且命名為“Update.exe”。
d.將可執(zhí)行文件“主程序.exe”、“Update.exe”拷貝到同一個(gè)文件夾中,運(yùn)行任一個(gè)程序,相信讀者會(huì)看到效果。
以上代碼比較簡(jiǎn)單,僅供讀者分析思路用。在實(shí)際工程中的代碼編寫(xiě)時(shí)也根本沒(méi)必要把projNewMain.exe放在資源文件中,讀者只需將它放在服務(wù)器的某個(gè)位置,然后在projUpdate.vbp中的相應(yīng)處寫(xiě)下載代碼即可。