【问题标题】:How can I launch Mozilla Firefox using C++?如何使用 C++ 启动 Mozilla Firefox?
【发布时间】:2014-07-06 07:54:02
【问题描述】:

通过像Code::Blocks这样的编译器编译,我尝试了以下,有点不起作用:

    /*Running Firefox.exe*/

        #include <stdio.h>

    /*using c++*/

        #include <iostream>

        #include <stdlib.h>

        using namespace std;

        int main ()
        {
            int x;
            cout << "Checking if processor is available..." << endl;
/*System used here*/
            if (system(NULL)) puts ("Proceed");

            else exit (1);

            cout<< "Executing Firefox..." << endl;

    /*Having some error here saying not recognized as internal or external command*/

            x = system ("C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
/*cout here*/
            cout <<"The value returned was:" << x << endl;


        return 0;
        }

是不是因为 Firefox 不被识别为 windows 系统?如果是这样,我如何从代码中运行 Firefox 甚至 Internet Explorer?

【问题讨论】:

  • 试试system("\"C:/Program...\"");,即引用路径
  • 如果你尝试\\ 而不是/呢?
  • 完整路径的示例?路径文件目录与 / 但从我所指的示例中,这是一个计算器.exe,它使用 \ 作为其目录路径
  • Code::Blocks in not 编译器;它是一个 IDE。很可能您将它与 MinGW/GCC 一起使用。
  • 是否可以在后台运行firefox而不执行它?

标签: c++ file execution


【解决方案1】:

运行 cmd.exe(Windows 命令外壳)并在命令行输入字符串 C:/Program Files (x86)/Mozilla Firefox/firefox.exe,您会看到同样的问题 - 即问题出在您的命令字符串而不是您的 C++ 代码上。

路径中的空格需要命令字符串被引用:

system ("\"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\"") ;

system ("\"C:/Program Files (x86)/Mozilla Firefox/firefox.exe\"") ;

【讨论】:

    【解决方案2】:

    我认为您需要将路径转换为 ​​DOS 格式

    在 Mozilla Firefox 文件夹中启动命令提示符 cd

    运行这个:for %I in (.) do echo %~sI

    将输出复制到system 命令,将\ 替换为\\ 在末尾追加firefox.exe

    【讨论】:

      【解决方案3】:

      是不是因为火狐不被识别为windows系统?

      如果您打开 cmd.exe 并输入 C:/Program Files... 它将不起作用,因为空格被用作分隔符。引用你的路径:

      system("\"C:/Program Files (x86)/Mozilla Firefox/firefox.exe\"");
      

      虽然如果您的目标是 Windows,您应该考虑使用 CreateProcess,这样可以省去这个麻烦。

      如果是这样,我如何从代码中运行 Firefox 甚至 Internet Explorer?

      如果你想显示一个网页,请使用ShellExecute* 并让 shell 完成工作。它将负责启动 Firefox、Internet Explorer、Chrome 或用户配置用于查看网页的任何浏览器。

      *首先阅读关于初始化 COM 的备注部分。

      【讨论】:

        【解决方案4】:

        尝试使用windows API CreateProcess API

        【讨论】:

        • 这是否允许我在没有实际启动 firefox 的情况下在后台运行 firefox?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-26
        • 1970-01-01
        • 2011-02-18
        • 1970-01-01
        相关资源
        最近更新 更多