Is there a non-java, cross platform way to launch

2020-08-26 06:25发布

问题:

First, I found a couple of java specific questions and answers for this. I am looking for more "native", but cross platform solution, using C, C++, some kind of shell scripts, or, in my case, Qt.

So the question is, are there standard, cross platform, ways to programmatically open the associated application for certain file types. Or at least to find out if there are associated applications and be able to locate and launch them?

By cross platform I mean Windows, OSX and linux (gnome/kde). The use case is having a database with stored files as blobs that will be read on the three different targets.

回答1:

I don't know of any cross-platform way.

In Windows, there is the start command, which will launch the associated default application. (E.g. start foo.doc will launch the default Word document editor, start http://StackOverflow.Com/ the default web browser and start mailto:mail@example.com the default mail app.)

In OS X there is the open command, which does the same thing.

Linux is just an Operating System kernel. OS kernels don't know anything about "filetypes" or "MIME types" or "associated applications" or anything like that. Therefore, such a thing simply cannot exist for Linux.

The Freedesktop Group has a specification for an xdg-open command, which works on all Freedesktop-compliant graphical desktops (be they Linux, FreeBSD, NetBSD, OpenBSD, DragonflyBSD, OpenSolaris or otherwise). However, it is obviously not guaranteed to work on non-Freedesktop systems and it is certainly not guaranteed to work on non-graphical systems.

In all three cases, this is a command line application, not a C or C++ API, but you can obviously call it via system.



回答2:

Since you have noted that you are using Qt, it's worth mentioning the QDesktopServices class, and especially the openUrl(QUrl) method. It does pretty much what you've described on all platforms supported by Qt.



回答3:

There is the system call in C, for example:

system("main.cpp");

This will open the file using the default editor (Visual Studio in my case). I'm not sure about Linux and Mac, you may need to write "open main.cpp" there (which can be taken care of by #ifdef constructs).