Use VB6.0 to compile a self-Upgrade Program

Source: Internet
Author: User
Tags read ini file

My specific ideas are as follows:

Write two programs, one is the main program, and the other is the Upgrade Program (the Upgrade Program is placed on the server );

Note: All upgrade tasks are completed by the upgrade procedure.

1. Start the Upgrade Program, connect the Upgrade Program to the website, and download the new main program (including the supported library files) to the Temporary Folder;

2. the Upgrade Program checks whether the old master program is active. if the activity is active, the old master program is closed (and the status of the master program is recorded );

3. Delete the old main program, copy the files in the Temporary Folder to the corresponding location, and register the corresponding files;

4. Check the status of the main program. If the status is active, start the new main program;

5. Close the Upgrade Program.

6. Congratulations! The upgrade is complete.

As a netizen raised a question about upgrading the Upgrade Program, the following is a supplement to his ideas. However, the original article still updates the main program. The specific code should be added by the reader:

7. The main program has been upgraded;

8.The update procedure is used to check that the downloaded file contains newupdate.exe(the new update procedure and rename.exe (a program that can change the file name );

9.if two files exist, you must update the update.exefile and start the rename.exeprogram. At the same time, update.exe will disable itself;

10.rename.exeprogram detects that update.exe.zip has been shut down. If it has been shut down, delete the update.exe. Move the newupdate.exefile In the hosts folder to the directory of the main program, and change its name to update.exe at the same time;

11.rename.exe close yourself.

12. OK. Now the Upgrade Program has been upgraded.

In the following code, we need to create three projects and edit them into a group. The three projects need to share one module.

Steps for creating a project:

1. create Project proMain: Open vb, "Create Project", select "Standard EXE", add a module to the project, name it modCommon, modify the form name to frmMain, and modify the project name to projMain, save it to a folder (for example, create a folder Update on the desktop), and save the form, module, and project as frmMain. frm, modCommon. bas, projMain. vbp;

2. create Project projNewMain: click "file new project", select "Standard EXE", click "project add module", and select the "existing" tab in the displayed dialog box, go to the Update folder and select modCommon. bas module. Modify the form name to frmNewMain, modify the project name to projNewMain, and save it to the Update folder. Save the form and project as frmNewMain. frm and projNewMain. vbp respectively;

3. create Project projUpdate: click "file new Project" in the menu, select "Standard EXE", click "project add module", and select the "existing" tab in the displayed dialog box, go to the Update folder and select modCommon. bas module. Modify the form name to frmUpdate, modify the project name to projUpdate, and save it to the Update folder. Save the form and project as frmUpdate. frm and projUpdate. vbp respectively;

4. Create a group: In project projUpdate, click "add project file..." in the menu ..." In the displayed dialog box, select the "existing" tag, locate the Update folder, and select projMain. vbp. Repeat this action and select projNewMain. vbp. Save the group;

5. Close the project, locate the Update folder, and then perform the following steps.

Source code of files in various engineering files:

1. projMain. vbp Project:

Note: This is the old main program and has never been performed before the upgrade.

Open the frmMain. frm file in notepad and copy the following content to it:

VERSION 5.00:
Begin VB. Form frmMain
Caption = "Please click to upgrade the program"
ClientHeight = 1, 1140
ClientLeft = 60
ClientTop = 345
ClientWidth = 4500
LinkTopic = "Form1"
ScaleHeight = 1140
ScaleWidth = 4500
StartUpPosition = 3'' window default
Begin VB. CommandButton Command1
Caption = "Upgrade"
Height = 525
Left = 1, 1380
TabIndex = 0
Maximum = 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
''Example of upgrading a program
''By Xie jiafeng
'''Date: 2003/12/19
''There is no main program during upgrade.
Private Sub commandementclick ()
Command1.Enabled = False
''Run the update program
Shell App. Path & "/update.exe", vbNormalFocus
End Sub
Private Sub Form_Load ()
If App. PrevInstance Then End
UpdateIniPath = App. Path & "/Update. ini"
''Records the name of the main program
WritePrivateProfileString "Main", "Name", App. EXEName, UpdateIniPath
''Records the running status
WritePrivateProfileString "Main", "Active", "-1", UpdateIniPath
''Records the number of updates
WritePrivateProfileString "Update", "Num", "0", UpdateIniPath
Me. Caption = App. EXEName
End Sub
Private Sub Form_Unload (Cancel As Integer)
''Records the running status
WritePrivateProfileString "Main", "Active", "0", UpdateIniPath
End Sub
Open the modCommon. bas file in notepad and copy the following content to it:
Attribute VB_Name = "modCommon"
Option Explicit
''Example of upgrading a program
''By Xie jiafeng
'''Date: 2003/12/19
''Here is a general module where API functions and public functions are placed
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "inline" (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 "find0000wa" (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
''Extract files from resource files
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
''Read strings from the buffer
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
''Read INI File
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
''Check whether the file exists
Function FileExists (filename As String) As Boolean
On Error Resume Next
FileExists = (Dir $ (filename) <> "")
End Function
''Change the text and position of a tag.
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
End Function
''Close the form
Function CloseValidForm (Ret As String) As Boolean
Dim WinWnd As Long
''Search for the handle of the window
WinWnd = FindWindow (vbNullString, Ret)
If WinWnd <> 0 Then
SendMessage WinWnd, WM_Close, 0 &, 0 &
End If
CloseValidForm = True
End Function
Ii. projNewMain. vbp Project:

Note: This is a new main program, that is, the upgraded main program. Because I do not have a server, this program is stored in the resource file of the projUpdate. vbp Project. Simulate network upgrade in projUpdate. vbp.

Open the frmNewMain. frm file in notepad and copy the following content to it:
VERSION 5.00:
Begin VB. Form frmNewMain
Appearance = 0'' Flat
BackColor = & H80000005 &
Caption = "Form1"
ClientHeight = 1, 1365
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 1365
ScaleWidth = 4680
StartUpPosition = 3'' window default
Begin VB. CommandButton Command1
Caption = "Upgrade"
Height = 525
Left = 1, 1410
TabIndex = 0
Maximum = 660
Width = 1245
End
Begin VB. Label lblCap
Caption = "tell you, I have upgraded. Didn't you see it is different from before? "
Height = 255
Left = 1, 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
''Example of upgrading a program
''By Xie jiafeng
'''Date: 2003/12/19
''Here is the upgraded main program
Private Sub commandementclick ()
Command1.Enabled = False
''Run the update program
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"
''Records the name of the main program
WritePrivateProfileString "Main", "Name", App. EXEName, UpdateIniPath
''Records the running status
WritePrivateProfileString "Main", "Active", "-1", UpdateIniPath
Me. Caption = App. EXEName
End Sub
Private Sub Form_Unload (Cancel As Integer)
''Records the running status
WritePrivateProfileString "Main", "Active", "0", UpdateIniPath
End Sub

Iii. projUpdate. vbp Project:

Note: This is an upgrade program that contains a resource file.

Open the frmUpdate. frm file in notepad and copy the following content to it:

VERSION 5.00:
Begin VB. Form frmUpdate
BackColor = & H80000007 &
BorderStyle = 0'' None
Caption = "Form1"
ClientHeight = 1, 1185
ClientLeft = 0
ClientTop = 0
ClientWidth = 5400
LinkTopic = "Form1"
MaxButton = 0'' False
MinButton = 0'' False
ScaleHeight = 1185
ScaleWidth = 5400
ShowInTaskbar = 0'' False
StartUpPosition = 2' screen center
Begin VB. CommandButton Command1
Appearance = 0'' Flat
Caption = "disabled"
Height = 435
Left = 1, 2130
TabIndex = 0
Maximum = 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 = 1, 330
TabIndex = 1
Maximum = 150
Width = 120
End
Begin VB. Shape Shape1
BackColor = & H00C0FFFF &
BackStyle = 1' Opaque
BorderColor = & hsf-ff00 &
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
''Example of upgrading a program
''By Xie jiafeng
'''Date: 2003/12/19
''Here is the Upgrade Program
Dim State As Boolean
Private Sub commandementclick ()
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
''Get the name of the old master program
OldExeFile = CStr (ReadIniFile (UpdateIniPath, "Main", "Name", "Main program "))
NewExeFile = OldExeFile
OldExeFile = App. Path & "/" & OldExeFile & ". exe"
TmpFile = App. Path & "/tmp. tmp"
''Number of rewrites and upgrades
I = CInt (ReadIniFile (UpdateIniPath, "Update", "Num", "0 "))
I = I + 1
ChangeLabelPos Me, lblCap, "this is your" & I & "upgrade! "
Sleep 1500
''To download a new update program from the website.
ChangeLabelPos Me, lblCap, "I'm trying to download a new upgrade program from the website ..."
Sleep 1000
On Error Resume Next
Kill tmpFile
''Here, you can modify it to put the temporary file in the Temporary Folder.
SaveFileFromRes 101, "CUSTOM", tmpFile
Sleep 1000
On Error GoTo 0
''Check whether the main program is enabled. If yes, close the old program.
MainState = CBool (CInt (ReadIniFile (UpdateIniPath, "Main", "Active", "0 ")))
If MainState Then
''Close the old program
ChangeLabelPos Me, lblCap, "closing old program ..."
Sleep 300
''Here, the old program is forcibly closed.
''You can also send a message to close the old program.
Do While-1
DoEvents
If CloseValidForm (NewExeFile) Then
Exit Do
End If
Loop
Sleep 200
End If
''Delete the old program
On Error Resume Next
ChangeLabelPos Me, lblCap, "deleting old program ..."
Sleep 1000
Kill OldExeFile
Sleep 1000
''Generate the name of the new Master Program
''Newexefile = CStr (ReadIniFile (UpdateIniPath, "Main", "Name", "Main program "))
J = Right (NewExeFile, 1)
If IsNumeric (j) Then
J = I
NewExeFile = Left (NewExeFile, Len (NewExeFile)-1)
Else
J = 1
End If
''Records the name of the main program
NewExeFile = NewExeFile & j
WritePrivateProfileString "Main", "Name", NewExeFile, UpdateIniPath
NewExeFile = App. Path & "/" & NewExeFile & ". exe"
''Copy new program
ChangeLabelPos Me, lblCap, "Updating program ..."
Sleep 1000
FileCopy tmpFile, NewExeFile
Sleep 1000
''Delete the temporary program
Kill tmpFile
''Records the number of updates
WritePrivateProfileString "Update", "Num", CStr (I), UpdateIniPath
''Check the status of the old program
If MainState Then
ChangeLabelPos Me, lblCap, "the updated main program is being started ..."
Sleep 1000
Shell NewExeFile, vbNormalNoFocus
End If
''Close the update program
Command1.Visible = True
ChangeLabelPos Me, lblCap, "the update is complete. Please wait 3 seconds until the update program is automatically closed ..."
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
I copied the code of the three projects and commented out the code in detail when necessary. I will not introduce the code here. Now I will proceed to the last step, download the upgraded program from the website.
A.open projmain.vbp, compile the project, and name it the main program .exe ";
B .open projnewmain.vbp, compile the project, and name it "projnewmain.exe"; c. open projUpdate. vbp: Open the "vb resource editor" on the toolbar. (if not, you need to click "external program manager" and select "vb6 resource editor" in the displayed dialog box ", select the "Attach/detach" check box in the Action column. In the resource editor, select "add custom resource ...", ".

D.copy the executable file main program .exe000000000000update.exe to the same folder and run any program. I believe the reader will see the effect.

The above code is relatively simple and is only used by readers for analysis. In the coding process of the pipeline project, it is not necessary to put projnewmain.exe in the resource file. The reader only needs to place it somewhere on the server, and then write and download the code in the corresponding part of projUpdate. vbp.

OK. Now the article has been written. I hope it will be helpful to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.