如何在Windows中使用glib/gtkmm打开/产生文件 [英] How to open/spawn a file with glib/gtkmm in Windows

查看:113
本文介绍了如何在Windows中使用glib/gtkmm打开/产生文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过:

  GError *pError = NULL;
  string uri = g_filename_to_uri(file.c_str(), NULL, &pError);
  if (!g_app_info_launch_default_for_uri(uri.c_str(), NULL, &pError)) {
      cout << "Failed to open uri: " << pError->message;
  }

在这里,我收到错误消息不支持URI".我在这里创建的uri错误吗?

Here I get the error "URIs not supported". Is the uri I create here wrong?

第二种方法是使用异步命令行生成文件:

My second approach was to spawn the file with an asynchronous command line:

  file = quoteStr(file);
  try {
    Glib::spawn_command_line_async(file);
  } catch (Glib::SpawnError error) {
    cout << error.what();
  } catch (Glib::ShellError error) {
    cout << error.what();
  }

在这里,将引发Glib :: SpawnError异常,并显示错误:无法执行帮助程序(无效参数)".我的意思是,当我在Windows cmd中执行带引号的绝对文件路径时,它将打开文件(在本例中为pdf文件).此功能是否有所不同?

Here the Glib::SpawnError exception is thrown with the error: "Failed to execute helper program (Invalid argument)". I mean, when I execute the quoted absolute file path in the Windows cmd, it opens the file (in this case a pdf file). Does this function work different?

推荐答案

我遇到了类似的问题,我不得不放弃使用glib来做到这一点,最终实现了一种简单的跨平台(win,mac和linux)兼容的方式来做到这一点:

I had a similar problem and I had to give up using glib to do that and ended up implementing a simple crossplatform (win, mac and linux) compatible way to do it:

// open an URI, different for each operating system
void
openuri(const char *url)
{
#ifdef WIN32
    ShellExecute(GetActiveWindow(),
         "open", url, NULL, NULL, SW_SHOWNORMAL);
#elif defined(__APPLE__)
    char buffer[512];
    ::snprintf(buffer, sizeof(buffer), "open %s", url);
    ::system(buffer);
#else
    char buffer[512];
    ::snprintf(buffer, sizeof(buffer), "xdg-open %s", url);
    ::system(buffer);
#endif
}

...虽然不是很好,但是它很小并且可以正常工作:)

... it's not very nice but it's small and it works :)

这篇关于如何在Windows中使用glib/gtkmm打开/产生文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆