Sub UninstallMain()
Dim inst_uninst, inst_str, output, ExitCode, ProductCode
inst_str = "Uninstallation "
inst_uninst = "msiexec.exe /quiet /x"
ProductCode = "{DB45895B-C92C-44EB-96C9-8C00DEC468DE}"
output = ""
output = output & "Executing command: " & Chr(34) & inst_uninst & " " & ProductCode & Chr(34) & vbNewLine
output = output & RunCmd(inst_uninst, "", "", ProductCode, ExitCode) & vbNewLine
If ExitCode <> 0 Then
output = output & vbNewLine & inst_str & "failed."
ErrorExit output, ExitCode
Else
output = output & vbNewLine & inst_str & "completed successfully." & vbNewLine
End If
If (InStr(output, "FAILED") <> 0) Or (InStr(output, "Error") <> 0) Or (InStr(output, "The system cannot find the file specified") <> 0) Or (InStr(output, "The network path was not found") <> 0) Then
ErrorExit output, 2
Else
WScript.Echo output
End If
End Sub
Sub ErrorExit(ByVal errorMessage, ByVal errorCode)
WScript.StdErr.WriteLine errorMessage
Wscript.Quit errorCode
End Sub
Private Function RunCmdUpdate(ByVal applicationPath, ByVal appName, ByVal installPath, ByRef ExitCode)
Dim shell, oexec, strPResult
strPResult = ""
Set shell = CreateObject("WScript.Shell")
Set oexec = shell.exec("%comspec% /c copy /Y /V " & Chr(34) & applicationPath & appName & Chr(34) & " " & Chr(34) & installPath & Chr(34))
Do While Not oexec.StdOut.AtEndOfStream
strPResult = strPResult & oexec.StdOut.Read(1024)
Loop
ExitCode = oexec.ExitCode
Set oexec = Nothing
Set shell = Nothing
RunCmdUpdate = strPResult
End Function
Private Function RunCmd(ByVal inst_uninst, ByVal curDir, ByVal sFile, ByVal params, ByRef errorCode)
Dim shell, oexec, fso, strPResult, cmdLine
strPResult = ""
cmdLine = "%comspec% /c " & inst_uninst & Chr(34) & curDir & sFile & Chr(34) & params
Set fso = CreateObject("Scripting.FileSystemObject")
If (Len(sFile) = 0 Or fso.FileExists(curDir & sFile)) Then
Set shell = CreateObject("WScript.Shell")
If Len(curDir) > 0 Or Len(sFile) > 0 Then
Set oexec = shell.exec("%comspec% /c " & inst_uninst & Chr(34) & curDir & sFile & Chr(34) & params)
Else
Set oexec = shell.exec("%comspec% /c " & inst_uninst & " " & params)
End If
oexec.stdin.close()
'wait until process ends
Dim iCnt, timeouted
iCnt = 600
timeouted = False
Do While oexec.Status = 0
WScript.Sleep 50
iCnt = iCnt - 1 'timeout
If 0 > iCnt Then
timeouted = True
Exit Do
End If
Loop
Do While Not oexec.StdOut.AtEndOfStream
'strPResult = strPResult & oexec.StdOut.ReadLine() & vbNewLine
strPResult = oexec.StdOut.ReadAll()
strPResult = unescape(replace(escape(strPResult),"%00","")) 'convert unicode to ascii
Loop
If timeouted = False Then
errorCode = oexec.ExitCode
Else
errorCode = getErrorCodeFromOutput(strPResult)
End If
Else
strPResult = "File does not exist."
errorCode = 1
End If
Set fso = Nothing
RunCmd = strPResult
End Function
Private Function RunCheckPrereqCmd(ByVal curDir, ByVal sFile, ByVal parameters, ByRef errorCode)
Dim shell, oexec, fso, strPResult
strPResult = ""
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(curDir & sFile)) Then
Set shell = CreateObject("WScript.Shell")
shell.currentdirectory = curDir
Set oexec = shell.exec("%comspec% /c " & Chr(34) & curDir & sFile & Chr(34) & " " & parameters)
oexec.stdin.close()
'wait until process ends
Dim iCnt, timeouted
iCnt = 600
timeouted = False
Do While oexec.Status = 0
WScript.Sleep 50
iCnt = iCnt - 1 'timeout
If 0 > iCnt Then
timeouted = True
Exit Do
End If
Loop
Do While Not oexec.StdOut.AtEndOfStream
strPResult = oexec.StdOut.ReadAll()
Loop
If timeouted = False Then
errorCode = oexec.ExitCode
Else
errorCode = getErrorCodeFromOutput(strPResult)
End If
Else
strPResult = Chr(34) & curDir & sFile & Chr(34) & " does not exist."
errorCode = 1
End If
Set oexec = Nothing
Set shell = Nothing
RunCheckPrereqCmd = strPResult
End Function
Private Function getErrorCodeFromOutput(ByVal strPResult)
Dim CHECK_OK, CHECK_FAILED, CHECK_WARNING, pos
CHECK_OK = "[ OK ]"
CHECK_WARNING = "[ Warning ]"
CHECK_FAILED = "[ Failed ]"
getErrorCodeFromOutput = 0
pos = InStr(strPResult, CHECK_WARNING)
If pos > 0 Then
getErrorCodeFromOutput = 1
End If
pos = InStr(strPResult, CHECK_FAILED)
If pos > 0 Then
getErrorCodeFromOutput = 2
End If