Research on the realization of program silent installation

Table of contents

1. Silently install the Microsoft Visual C++ release package VC_redist.x86.exe / VC_redist.x64.exe

1.1. Refer to the operation of silently installing VC_redist.x64.exe by WireShark to find the implementation method of silent installation

1.2. Download the new version or VC_redist.x86.exe / VC_redist.x64.exe corresponding to Visual Studio from the official website of Microsoft

2. Silent installation and uninstallation of MSI installation package

2.1. Use msiexec.exe to silently install the MSI installation package

2.2. Stripping of the XPS virtual printer MSI installation package

3. Silent installation of system patch packages

4. Finally


icon-default.png?t=N6B9      This article combines project practice today to talk about how to realize the silent installation of common program types.icon-default.png?t=N6B9icon-default.png?t=N6B9icon-default.png?t=N6B9

1. Silently install the Microsoft Visual C++ release package VC_redist.x86.exe / VC_redist.x64.exe

       Programs compiled by Visual Studio generally require C/C++ runtime libraries. Different versions of Visual Studio correspond to different runtime library versions. These runtime libraries need to be loaded when the program starts. If the dependent runtime library cannot be found on the machine, an error will be reported and the program will fail to start.

1.1. Refer to the operation of silently installing VC_redist.x64.exe by WireShark to find the implementation method of silent installation

       For these runtime libraries required by the program, these runtime libraries can be directly packaged into the program installation package, and directly copied to the program installation path during installation. You can also download the Microsoft Visual C++ distribution package VC_redist.x86.exe / VC_redist.x64.exe officially provided by Microsoft:

       Integrate this program package into the program installation package, and install VC_redist.x86.exe / VC_redist.x64.exe first when executing the program installation operation. Many programs use the latter method, such as our commonly used Windows version of the packet capture tool WireShark, you can see that VC_redist.x64.exe is installed during the installation process:

When executing VC_redist.x64.exe installation, three parameters are passed to VC_redist.x64.exe: /install /quiet /norestart, where the /quiet parameter means silent installation, and the installation interface of VC_redist.x64.exe does not pop up.

        Taking VC_redist.x64.exe as an example, its installation interface is as follows:

You can switch to the path of the VC_redist.x64.exe program in the cmd command line window, and then enter: VC_redist.x64.exe -help or VC_redist.x64.exe /help to check which specific command line parameters VC_redist.x64.exe supports, as follows:

From the above figure, I saw the three parameters used in the WireShark installation package: /install /quiet /norestart.

Generally, programs that support command line parameter passing support the use of /help or -help to view which command line parameters the program supports. For Windows system programs, /? is also supported to view.

       Use C++ code to realize the silent installation of VC_redist.x64.exe:

// lpszVCRedistExePath: VC_redist.x64.exe的绝对路径 
BOOL QuietInstallHotfixPackage( LPCTSTR lpszVCRedistExePath)
{
    PROCESS_INFORMATION pi;
    memset(&pi, 0, sizeof(pi));
    STARTUPINFO si;
    memset(&si, 0, sizeof(si));
    si.cb = sizeof(STARTUPINFO);
    si.wShowWindow = SW_HIDE;
    si.dwFlags = STARTF_USESHOWWINDOW;

    TCHAR achCmdLine[MAX_PATH * 2] = { 0 };

    _tcscpy(achCmdLine, lpszVCRedistExePath);
    _tcscat(achCmdLine, _T(" /install /quiet /norestart")); // VC_redist.x64.exe静默安装命令行参数

    if (!CreateProcess(NULL, achCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
        return FALSE;
    }

    // 等待安装进程安装完成,安装完后再执行后续安装操作
    // 参数INFINITE表示无限等待,直接安装进程自动退出
    // 为了防止安装进程出问题,可以设置一个有限的等待时间
    WaitForSingleObject(pi.hProcess, INFINITE);

    // 可以调用API接口GetExitCodeProcess,获取进程退出码
    // If the specified process has not terminated, the termination status returned is STILL_ACTIVE. 
    // If the process has terminated, the termination status returned may be one of the following: 
    // 1) The exit value specified in the ExitProcess or TerminateProcess function.
    // 2) The return value from the main or WinMain function of the process.
    // 3) The exception value for an unhandled exception that caused the process to terminate.
    DWORD dwExitCode = 0;
    GetExitCodeProcess(pi.hProcess, &dwExitCode);

    // 将进程句柄和线程句柄关闭掉,否则会有句柄泄漏
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);

    return TRUE;
}

1.2. Download the new version or VC_redist.x86.exe / VC_redist.x64.exe corresponding to Visual Studio from the official website of Microsoft

        When you need to use VC_redist.x86.exe / VC_redist.x64.exe, if you download it from the Internet at will, it may be inconsistent with the version of Visual Studio that compiles the program, and there will still be problems. Therefore, it is recommended that you go to the official Microsoft page to download the latest version or the corresponding version of VC_redist.x86.exe / VC_redist.x64.exe :
Microsoft Visual C++ Redistributable latest supported downloads https://learn.microsoft.com/en-US/cpp/windows/latest-supported-vc-redist

2. Silent installation and uninstallation of MSI installation package

        The full name of MSI is Microsoft Installer, and the MSI file is a Windows Installer-based installation package file developed by Microsoft for installing and upgrading Windows system software. The MSI file is a Windows Installer data package. It is actually a database that contains the information needed to install a product and the instructions and data needed to install (and uninstall) the program in many installation situations. The installation of the MSI installation package depends on the Windows Installer service:

When the service is not disabled, double-click the MSI installation package, and the system will automatically start the Windows Installer service to complete the installation of the MSI installation package. If this service is disabled, any MSI type installation package cannot be installed normally, and the system will prompt the following information:

2.1. Use msiexec.exe to silently install the MSI installation package

       Under Windows, you can use the msiexec.exe system program to manage your own MSI installation package. Enter msiexec /? in the cmd command line window, and a help dialog box for msiexec.exe will pop up. You can view the command line parameters supported by msiexec.exe from the dialog box:

From above we saw the /quiet silent installation parameter. Scroll down the scroll bar, and you can also see the /nostart option not to restart after the installation is complete:

Therefore, the command to silently install the MSI package using msiexec on the cmd command line is as follows:

// 静默安MSI安装包,安装完后不重启
msiexec  /i "E:\XXXXXX.msi" /quiet /norestart

2.2. Stripping of the XPS virtual printer MSI installation package

        The project needs to use the Microsoft XPS virtual printer to convert pdf documents into pictures. Win7 and above systems already have this virtual printer, but it needs to be installed manually under the XP system (the XP system is relatively old, but there are still a small number of customers still using the XP system, such as in some ZF institutions).

       The installation packages required to install the XPS virtual printer under the XP system are divided into XPSEP XP and Server 2003 32 bit.msi and XPSEP XP and Server 2003 64 bit.msi. After the installation is complete under the XP system, it is found that in addition to installing the XPS virtual printer, the XPS Viewer viewer is also installed, and a shortcut to the XPS Viewer is generated on the desktop. The XPS Viewer has nothing to do with document conversion and is not needed by us. Moreover, these two MSI installation packages have reached about 17MB, and they must be packaged into the program installation package, which will significantly increase the size of the installation package, which needs to be optimized. In addition, this XPS virtual printer is not perceived by the user. If a shortcut of XPS Viewer is generated on the desktop, the user will feel very strange, so this desktop shortcut also needs to be disposed of.

       Later, by using some msi viewing and editing tools, such as Orca, Advanced Installer, MSI Studio, we checked XPSEP XP and Server 2003 32 bit.msi and found that some files in the Binary data table in the installation package are exe files:

       Later, I used 7-Zip or Haozi to decompress XPSEP XP and Server 2003 32 bit.msi, and forcibly changed the corresponding file in the Binary table to an exe file (then use 7z to decompress the binary file to get a complete xps installation file), and found that EPDPDATE and SCUPDATE in the Binary table are the installers of XPS Viewer and XPS virtual printer respectively:

       Extract the corresponding SCUPDATE files from XPSEP XP and Server 2003 32 bit.msi and XPSEP XP and Server 2003 64 bit.msi respectively, change the file type to exe type, double-click to start the file and find that SCUPDATE is actually a Windows system patch:

After the installation is complete, you can view the installed system patches:

3. Silent installation of system patch packages

        Windows system patches support command line parameters, and silent installation can also be achieved by setting the corresponding parameters. For example, if 360 Security Guard and Tencent Butler cannot see the patch installation interface when installing system patches, they should use silent installation. Detailed descriptions of related topics can be searched in MSDN:

The above parameters are similar to the command line parameters supported by msiexec.exe. The command to silently install the Windows patch on the cmd command line is:

// 补丁包名称+命令行参数
E:\XPS\SCUPDATE_32bit.exe /u /q /z

        Use C++ code to realize the silent installation of the patch package program as follows:

// lpszHotfixExePath:补丁包exe的绝对路径 
BOOL QuietInstallHotfixPackage( LPCTSTR lpszHotfixExePath )
{
    PROCESS_INFORMATION pi;
    memset(&pi, 0, sizeof(pi));
    STARTUPINFO si;
    memset(&si, 0, sizeof(si));
    si.cb = sizeof(STARTUPINFO);
    si.wShowWindow = SW_HIDE;
    si.dwFlags = STARTF_USESHOWWINDOW;

    TCHAR achCmdLine[MAX_PATH * 2] = { 0 };

    _tcscpy(achCmdLine, lpszHotfixExePath);
    _tcscat(achCmdLine, _T(" /u /q /z")); // Windows补丁静默安装命令行参数

    if (!CreateProcess(NULL, achCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
        return FALSE;
    }

    // 将进程句柄和线程句柄关闭掉,否则会有句柄泄漏
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);

    return TRUE;
}

4. Finally

       The above mentioned the implementation of silent installation of several types of programs, hoping to provide some reference and reference for everyone.

Guess you like

Origin blog.csdn.net/chenlycly/article/details/131906646