除了qt,如何通过c/c++程序跨平台打开任何应用程序?

8yparm6h  于 2022-12-15  发布在  C/C++
关注(0)|答案(2)|浏览(218)

Hi i want to open any application that has been installed on the system through terminal or c/c++ program.
The reason i said through terminal or c/c++ is the command which we use in terminal will also be used in c/c++ program!!.
I referred this link Open Application In C but its not a cross platform one. I came to know that system command is used with open to launch any application from mac.For Example:- open -a "Google chrome" through terminal and if wanted in c/c++ program it will be system("open -a 'Google chrome'"); .
But how to open any installed application in linux? Or Windows? and how to make it cross platform c/c++?? i know based on macros we can identify OS and execute respective commands based on OS. But i want to know to open application in linux and windows?
Sample code:

#include<iostream>
#include<cstdlib>

int main(){
    system("open -a 'Google chrome'");
    return 0;
}

This code works in Mac, But based on my research i found in linux it has to be the path of the application that is enough. Please can anyone clarify this? I need a cross platform solution!!

hs1ihplo

hs1ihplo1#

I think it's insane that this question is over a year old and the only answer implies pumping several megabytes into a program that otherwise wouldn't need it.
You can use platform-specific preprocessor directives to achieve what you want.

#include <string>

#ifdef _WIN32
static int platform = 1;
#elif _WIN64
static int platform= 1;
#elif __linux__
static int platform = 2;
#elif __APPLE__
static int platform = 3;
#else
static int platform = 0;
#endif

int main(int argc, char *argv[])
{
  std::string str;
  std::string p = "https://crouton.net";
  if(platform) switch(platform)
    {
    case 1:
      str = "explorer";
      break;
    case 2:
      str = "xdg-open";
      break;
    case 3:
      str = "open";
      break;
    default:
      return 1; // Should never happen on the 3 defined platforms
    }
  str.append(" " + p);
  std::system(str.data());
  return 0;
}

It is important to keep in mind that this code can be run on more than just Windows, Mac and Linux. The usage of __APPLE__ is based on the assumption that what you're developing is a desktop program -- it likely wouldn't work on iOS, where the definition is still valid.
This example will only work for URLs (e.g. you can't replace the URL with the name of an application). If you don't know the path of what you're trying to open, you can attempt to get it using other platform-specific commands, but it's never guaranteed that you will find it even if the user has it installed. It is a very platform-specific thing because Windows applications often don't utilize the PATH, as they're expected far less to be invoked on the command line.

ha5z0ras

ha5z0ras2#

可以使用Qt cross-platform C++ framework,具体启动进程可以使用QProcess启动程序,也可以使用QDesktopServices:

#include <QtWidgets/QApplication>

#include <QProcess>
#include <QDesktopServices>
#include <QUrl>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // You can specify the path in some config file
    const QString pathToApp = "/usr/bin/google-chrome-stable";

    // Open an application by given path
    // No need to add "open" or some other OS-specific prefix
    QProcess::startDetached(pathToApp);

    // OR
    // Open target web address in a default system browser in any OS
    QDesktopServices::openUrl(QUrl("www.google.com"));

    return a.exec();
}

相关问题