GTK3:在 Windows 中使用 Firefox 作为默认浏览器打开本地 html 文件失败

问题描述 投票:0回答:0

我正在 Windows 上编译一个 GTK3 程序,我需要在其中使用默认应用程序/浏览器打开一个具有本地路径的特定 html 文件。我正在使用

gtk_show_uri_on_window()
(或者
g_app_info_launch_default_for_uri()
具有相同的结果)。我知道 URI 被传递给
gspawn-win64-helper.exe
,然后调用相应的程序。这适用于 Internet Explorer 或 Edge,但不适用于 Firefox,因为 URI 被转换为 Windows 路径(
file:///C:/path/to/file.html
变为
C:\path\to\file.html
),Firefox 无法理解。

有没有办法以任何普通网络浏览器都能理解的方式传递完整的 URI?

我的代码看起来像这样(我用纯 C 编写):

{
    MyAppWindow *window = (MyAppWindow *)user_data;
    gchar *filename = NULL;
    gchar *url = NULL;
    const gchar * const *dirs = g_get_system_data_dirs();
    GError *error = NULL;

    while (*dirs != NULL) {
        filename = g_build_filename(*dirs++, "path", "to", "file.html", NULL);
        if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
            url = malloc((strlen(filename) + 9) * sizeof(gchar));
            strcpy(url, "file:///");
            strcat(url, filename);
            gtk_show_uri_on_window(GTK_WINDOW(window), url, GDK_CURRENT_TIME, &error);
            if (error != NULL) {
                fprintf (stderr, "Error: %s\n", error->message);
                g_error_free (error);
            }
            if (url != NULL)
                g_free(url);
            if (filename != NULL)
                g_free(filename);
            break;
        }
    }

Firefox 的错误信息是:

地址没看懂

Firefox 不知道如何打开此地址,因为以下协议 (c) 之一未与任何程序关联或在此上下文中不允许。

在Linux和macOS下都没有出现这个问题,这让我得出结论,从路径中删除

C:
后,驱动程序字母(
file:///
)确实被误解为协议。

编辑:我更正了下面评论中提到的 malloc 中错误的字符串长度。

我在 Windows 7 和 Windows 10 上用当前版本的 Firefox 重现了这个问题。当 Firefox 打开时,它会尝试使用默认搜索引擎 (Google) 搜索路径字符串,而不是错误消息。

在字符串文件名中为

/
更改
\
并没有改变任何事情;事实上,URI 仍然是用斜杠导出的。

c windows uri gtk3 glib
© www.soinside.com 2019 - 2024. All rights reserved.