检测是否使用 Inno Setup 安装了 WebView2 Runtime

Detecting if WebView2 Runtime is installed with Inno Setup

Microsoft 网站上对此有很好的讨论:

Detect if a suitable WebView2 Runtime is already installed

其中一部分指出:

Inspect the pv (REG_SZ) regkey for the WebView2 Runtime at both of the following registry locations. The HKEY_LOCAL_MACHINE regkey is used for per-machine install. The HKEY_CURRENT_USER regkey is used for per-user install.

For WebView2 applications, at least one of these regkeys must be present and defined with a version greater than 0.0.0.0. If neither regkey exists, or if only one of these regkeys exists but its value is null, an empty string, or 0.0.0.0, this means that the WebView2 Runtime isn't installed on the client. Inspect these regkeys to detect whether the WebView2 Runtime is installed, and to get the version of the WebView2 Runtime. Find pv (REG_SZ) at the following two locations.

The two registry locations to inspect on 64-bit Windows:

  • HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}
  • HKEY_CURRENT_USER\Software\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}

The two registry locations to inspect on 32-bit Windows:

  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}
  • HKEY_CURRENT_USER\Software\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}

我的安装程序使用提升,因此我将检查限制为 HKEY_LOCAL_MACHINE。我相信这是正确的做法。这是我编写此函数的尝试:

function IsWebView2RuntimeNeeded(): boolean;
var
    Version: string;
    RuntimeNeeded: boolean;
    VerifyRuntime: boolean;
begin
    { See: https://docs.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution#detect-if-a-suitable-webview2-runtime-is-already-installed }

    RuntimeNeeded := true;
    VerifyRuntime := false;

    { Since we are using an elevated installer I am not checking HKCU }
    if (IsWin64) then
    begin
        { Test x64 }
        if (RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}', 'pv', Version)) then
        begin
            { We need to verify }
            VerifyRuntime := true;
        end
    else
    begin
        { Test x32 }
        if (RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}', 'pv', Version)) then
        begin
            { We need to verify }
            VerifyRuntime := true;
        end;
    end;

    { Verify the version information }
    if (VerifyRuntime) then
    begin
        if (not Version = '' and not Version = '0.0.0.0') then
            Log('WebView2 Runtime is installed');
            RuntimeNeeded := false;
        else
            Log('WebView2 Runtime needs to be downloaded and installed');
        end;
    end;

    Result := RuntimeNeeded;
end;

但它不会编译并说:

Column 40 type mismatch.

不喜欢这一行:

if (not Version = '' and not Version = '0.0.0.0') then

我对编写本机 Pascal 脚本没有信心,因此非常感谢有关更正或改进此代码以使其高效运行的指导。


我对实际下载文件并启动其安装程序的其他方面很满意,因为我可以在我的安装脚本中遵循其他下载的原则。只是把这个实际测试放在一起来验证是否安装了 WebView2 运行时。

我能够从另一个网站上对这个问题的评论之一中收集到一些建议:

How to write an if and statement?


我现在的代码是:

function IsWebView2RuntimeNeeded(): boolean;
var
    Version: string;
    RuntimeNeeded: boolean;
    VerifyRuntime: boolean;
begin
    { See: https://docs.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution#detect-if-a-suitable-webview2-runtime-is-already-installed }

    RuntimeNeeded := true;
    VerifyRuntime := false;

    { Since we are using an elevated installer I am not checking HKCU }
    if (IsWin64) then
    begin
        { Test x64 }
        if (RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}', 'pv', Version)) then
        begin
            { We need to verify }
            VerifyRuntime := true;
        end
    else
    begin
        { Test x32 }
        if (RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}', 'pv', Version)) then
        begin
            { We need to verify }
            VerifyRuntime := true;
        end;
    end;

    { Verify the version information }
    if (VerifyRuntime) then
    begin
        if (Version <> '') and (Version <> '0.0.0.0') then
        begin
            Log('WebView2 Runtime is installed');
            RuntimeNeeded := false;
        end
        else
            Log('WebView2 Runtime needs to be downloaded and installed');
        end;
    end;

    Result := RuntimeNeeded;
end;

我还必须在 if 语句中添加 beginend 关键字。如果您发现编写此代码的更有效方法,请随时更新或添加您自己的答案。例如,我链接到的原始文章提到了一个替代方案 (GetAvailableCoreWebView2BrowserVersionString)。我想我们不能使用那种方法。