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

来源:互联网 发布:程序员必读书单 1.0 编辑:程序博客网 时间:2024/04/28 17:20
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

 

曾经有位网友和我讨论怎样编写一个通过服务器对客户机上的程序进行自我升级的问题,由于我的项目也涉及到自我升级,当时我把我的解决方案提了出来,那就是做一个类似于瑞星的升级程序。在这里我也发现了很多的网友对这个问题很困惑,故我愿意把我的设计方案写出来与大家共享,大家也可以通过这个思路用其他语言进行改写。

 

以下是我的具体思路:

写两个程序,一个是主程序;一个是升级程序(升级程序放在服务器上);

说明:所有升级任务都由升级程序完成。

 

1.启动升级程序,升级程序连接到网站,下载新的主程序(当然还包括支持的库文件等)到临时文件夹;

2.升级程序检测旧的主程序是否活动,若活动则关闭旧的主程序(同时记下主程序的状态);

3.删除旧的主程序,拷贝临时文件夹中的文件到相应的位置,同时注册相应的文件;

4.检查主程序的状态,若状态为活动的,则启动新的主程序

5.关闭升级程序

6.祝贺你,升级完成。

由于网友提出了关于升级这个升级程序的问题,以下是对其思路进行的一点补充,但原文仍然是升级主程序的,具体代码需读者自己添加:

7.主程序升级完毕;
8.升级程序继续检查所下载的临时文件中是否含有NewUpdate.exe(新的升级程序)和rename.exe(是一个可以更改文件名的程序);
9.若存在以上两个文件,表示要更新Update.exe文件;启动rename.exe程序,同时update.exe关掉自己;
10.rename.exe程序检测update.exe是否已被关掉,若已关掉,删除该update.exe。移动临时文件夹中的NewUpdate.exe文件到主程序的目录下,同时更名为update.exe;
11.rename.exe关掉自己。
12.OK,至此升级程序也被升级了。

 

 

下面进行具体的程序编写,需建立三个工程,然后把它们编辑成一个组,三个工程需共用一个模块。

 

建立工程步骤:

1.        建立工程proMain:打开VB,“新建工程”,选择“标准EXE, 再给工程中添加模块,并且命名为modCommon,修改窗体名为frmMain,同时修改工程名为projMain,然后保存到某个文件夹(譬如在桌面建立个文件夹Update),窗体、模块和工程分别保存为frmMain.frmmodCommon.basprojMain.VBp

 

2.        建立工程projNewMain:点击菜单“文件|新建工程” ,选择“标准EXE”,点击菜单“工程|添加模块”,在弹出的对话框中选择“现存”标签,定位到Update文件夹,选中modCommon.bas模块。修改窗体名为frmNewMain,同时修改工程名为projNewMain,然后保存到Update文件夹,窗体和工程分别保存为frmNewMain.frmprojNewMain.VBp

 

 

3.        建立工程projUpdate:点击菜单“文件|新建工程” ,选择“标准EXE”,点击菜单“工程|添加模块”,在弹出的对话框中选择“现存”标签,定位到Update文件夹,选中modCommon.bas模块。修改窗体名为frmUpdate,同时修改工程名为projUpdate,然后保存到Update文件夹,窗体和工程分别保存为frmUpdate.frmprojUpdate.VBp

 

4.        建立组:在工程projUpdate中,点击菜单“文件|添加工程”在弹出的对话框中选择“现存”标签,定位到Update文件夹,选择projMain.VBp;重复该动作,选择projNewMain.VBp;保存该组即可;

 

5.        关闭工程,定位到Update文件夹,然后执行下面的步骤。

 

 

各个工程文件中的文件源码:

一、 projMain.VBp工程:

说明:这个是旧的主程序,从来没有进行过升级前的程序

 

用记事本打开frmMain.frm文件,copy以下内容到其中

 

VERSION 5.00

Begin VB.Form frmMain

   Caption         =   "请点击升级进行程序"

   ClientHeight    =   1140

   ClientLeft      =   60

   ClientTop       =   345

   ClientWidth     =   4500

   LinkTopic       =   "Form1"

   ScaleHeight     =   1140

   ScaleWidth      =   4500

   StartUpPosition =   3  '窗口缺省

   Begin VB.CommandButton Command1

      Caption         =   "升级"

      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

 

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

' 升级程序的例子

' 作者:   谢家峰

' 日期:   2003/12/19

'

' 这里是没有升级时的主程序

'

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

 

Private Sub Command1_Click()

  Command1.Enabled = False

   

  ' 运行更新程序

  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

  ' 记录运行状态

  WritePrivateProfileString "Main", "Active", "-1", UpdateIniPath

  ' 记录更新次数

  WritePrivateProfileString "Update", "Num", "0", UpdateIniPath

 

  Me.Caption = App.EXEName

End Sub

 

Private Sub Form_Unload(Cancel As Integer)

  ' 记录运行状态

  WritePrivateProfileString "Main", "Active", "0", UpdateIniPath

End Sub

 

 

用记事本打开modCommon.bas文件,copy以下内容到其中

 

Attribute VB_Name = "modCommon"

Option Explicit

 

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

' 升级程序的例子

' 作者:   谢家峰

' 日期:   2003/12/19

'

' 这里是通用模块,放置API函数以及公用函数

'

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

 

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

 

 

' ===============================================

' 从缓冲区中读取字符串

'

' ===============================================

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

 

' 改变标签的文本及位置

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

 

'关闭窗体

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

 

 

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击