正文

用VB6.0编写自我升级的程序(二)2005-09-25 07:50:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/5257.html

分享到:

用VB6.0编写自我升级的程序(二)

            二、 projNewMain.vbp工程:

            说明:这个是新的主程序,即升级后的主程序,由于我目前没有服务器,故将该程序放在projUpdate.vbp工程的资源文件中。在projUpdate.vbp中模拟网络升级。
            用记事本打开frmNewMain.frm文件,copy以下内容到其中:
            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 = "升级"
            Height = 525
            Left = 1410
            TabIndex = 0
            Top = 660
            Width = 1245
            End
            Begin VB.Label lblCap
            Caption = "告诉你,我已经升级了,没看出和以前不一样了吗?"
            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

            ' ------------------------------------------
            ' 升级程序的例子
            ' 作者: 谢家峰
            ' 日期: 2003/12/19
            ' 这里是升级后的主程序
            ' ------------------------------------------
            Private Sub Command1_Click()
            Command1.Enabled = False
            ' 运行更新程序
            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
            ' 记录运行状态
            WritePrivateProfileString "Main", "Active", "-1", UpdateIniPath
            Me.Caption = App.EXEName
            End Sub

            Private Sub Form_Unload(Cancel As Integer)
            ' 记录运行状态
            WritePrivateProfileString "Main", "Active", "0", UpdateIniPath
            End Sub

            三、 projUpdate.vbp工程:
            说明:这是升级程序,含有一个资源文件。
            用记事本打开frmUpdate.frm文件,copy以下内容到其中:
            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 = "关闭"
            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

            ' ------------------------------------------
            ' 升级程序的例子
            ' 作者: 谢家峰
            ' 日期: 2003/12/19

            ' 这里是升级程序

            ' ------------------------------------------

            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
            ' 获取旧主程序名称
            OldExeFile = CStr(ReadIniFile(UpdateIniPath, "Main", "Name", "主程序"))
            NewExeFile = OldExeFile
            OldExeFile = App.Path & "\" & OldExeFile & ".exe"
            tmpFile = App.Path & "\tmp.tmp"
            ' 改写升级次数
            i = CInt(ReadIniFile(UpdateIniPath, "Update", "Num", "0"))
            i = i + 1
            ChangeLabelPos Me, lblCap, "这是您第" & i & "次升级!"
            Sleep 1500
            ' 摸拟从网站下载新的更新程序。
            ChangeLabelPos Me, lblCap, "正在 摸拟从网站下载新的升级程序 ..."
            Sleep 1000
            On Error Resume Next
            Kill tmpFile
            ' 此处可以修改为将临时文件放在临时文件夹内
            SaveFileFromRes 101, "CUSTOM", tmpFile
            Sleep 1000
            On Error GoTo 0
            ' 检查主程序是否开启,若开启则关闭旧程序
            MainState = CBool(CInt(ReadIniFile(UpdateIniPath, "Main", "Active",
            "0")))
            If MainState Then
            ' 关闭旧程序
            ChangeLabelPos Me, lblCap, "正在关闭旧程序 ... "
            Sleep 300
            ' 这里强行关闭旧程序
            ' 你也可以发送消息,让旧程序自己关闭
            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

            ' 生成新主程序名称

            '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
            ' 删除临时程序
            Kill tmpFile
            ' 记录更新次数
            WritePrivateProfileString "Update", "Num", CStr(i), UpdateIniPath
            ' 检查旧程序的状态
            If MainState Then
            ChangeLabelPos Me, lblCap, "正在启动更新后的主程序 ... "
            Sleep 1000
            Shell NewExeFile, vbNormalNoFocus
            End If
            ' 关闭更新程序
            Command1.Visible = True
            ChangeLabelPos Me, lblCap, "更新完成,请等待 3 秒后将自动关闭更新程序 ..."
            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

            完成了三个工程的代码拷贝,在代码的必要之处我都进行了详细注释,这里我就不再介绍代码了,现在进行最后一步,模拟从网站下载升级后的程序。

            a. 打开projMain.vbp,编译该工程,并且命名为“主程序.exe”;

            b. 打开projNewMain.vbp,编译该工程,并且命名为“projNewMain.exe”;

            c.
            打开projUpdate.vbp,打开工具栏上的“vb资源编辑器”(若没找到,读者需要点击菜单“外接程序|外接程序管理器”,在打开的对话框中选择“vb6资源编辑器”,在加载行为中选择“加载/卸载”复选框),在资源编辑器中选择“添加自定义资源
            …”,在打开的对话框中定位到Update文件夹,选择projNewMain.exe文件,保存该资源文件,最后编译该工程并且命名为“Update.exe”。

            d. 将可执行文件“主程序.exe”、“Update.exe”拷贝到同一个文件夹中,运行任一个程序,相信读者会看到效果。


            以上代码比较简单,仅供读者分析思路用。在实际工程中的代码编写时也根本没必要把projNewMain.exe放在资源文件中,读者只需将它放在服务器的某个位置,然后在projUpdate.vbp中的相应处写下载代码即可。

            Ok,文章至此编写完成,希望对各位读者能有所帮助。

阅读(1565) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册