How to launch the associated application for a file / directory / URL? - c++

Linux seems to be easy: xdg-open <file/directory/URL>.
Apparently, Mac is similar: open should be used instead of xdg-open. I don't have access to a Mac so I couldn't test it.
For Windows, I found 4 different suggestions and those that I have tried failed.
Is there a non-java, cross platform way to launch the associated application for a certain file type?
suggests start
How to give focus to default program of shell-opened file, from Java? suggests
cmd /c start ...
How to open user system preferred editor for given file?
How to Find Out Default File Opener with Java?
suggest RUNDLL32.exe
What is the correct way to use ShellExecute() in C to open a .txt
Open file with Windows' native program within C++ code
How to use ShellExecute to open html files in Windows using C++? suggest
ShellExecute
I have tried the first 3 with system() and QProcess::startDetached() and "http://www.stackoverflow.com" as argument but they all failed; start works just fine from the command line though. I haven't tried ShellExecute yet.
What is the Windows equivalent of xdg-open? It seem to me, it is start but why did my attempts with start fail?
Is ShellExecute my only option?
EDIT I thought QDesktopServices::openUrl() was for web pages only because it did not work for files or directories.
After some debugging I figured out that if I replace \\ with / in the path on Windows, it works for files but the directories are still not opened. Any ideas what I am doing wrong?
QDir dir("C:/Documents and Settings/ali");
qDebug() << "Exists? " << dir.exists();
qDebug() << dir.absolutePath();
QDesktopServices::openUrl(QUrl(dir.absolutePath()));
qDebug() << "External app called";
Application Output:
Exists? true
"C:/Documents and Settings/ali"
External app called
But nothing happens, the directory is not opened. On Linux, directories are opened with the default file manager as expected.
SOLUTION: Due to the Qt bug and Windows quirks (malformed application window), I ended up using ShellExecute. That gives me enough flexibility to achieve exactly what I want at some expense...

Why don't you just use Qt's support for this? For example:
QDesktopServices::openUrl(QUrl("/home/realnc/test.pdf"));
This opens the document in Acrobat Reader. In general, it obeys the preferred application settings in my OS for all file types that have one or more applications associated with them. Best of all, it's platform-independent.
Edit:
The fact that it opens directories on Linux but not on Windows smells like a bug. It might be best to report this on Qt's bug tracker. In the meantime, you could have a workaround for Windows for when the file is a directory:
#ifdef Q_WS_WIN
if (QFileInfo(path).isDir())
QProcess::startDetached("explorer", QStringList(path));
else
#endif
QDesktopServices::openUrl(QUrl(path));
You can also do it with cmd.exe's start command, but you'll get an ugly terminal pop up for a few fractions of a second:
QProcess::startDetached("cmd", QStringList() << "/C" << "start"
<< QDir::toNativeSeparators(path));

Related

osx: external program (qprocess) crash if parent run with open, but works fine if parent run directly

I faced the strange situation on MacOS 10.13 and can't find what is the root of it.
I have 32-bit Qt application packed in a bundle. Because of MacOS limitation for one of the operations I need to start small 64-bit console binary that does a trick. This console binary placed in Contents/MacOS and I start it using QProcess.
Everything works fine if I run the main application from IDE. Also, everything fine if I open a terminal, cd to Contents/MacOS and run the main application directly.
But once I use "open myApp.app" or start it via UI, then QProcess exitCode() returns 255, which seems to mean crash.
Code for starting subprocess:
QProcess p;
p.start("./papply", QStringList() << osid << filepath);
p.waitForFinished(5000);
qDebug() << p.readAllStandardOutput();
qDebug() << p.readAllStandardError();
qDebug() << p.state();
if(p.state()==QProcess::Running)
{
qDebug() << "peapply freezed - kill";
p.kill();
return false;
}
qDebug() << "Apply" << osid << filepath << "=" << p.exitCode();
return p.exitCode()==0;
Any help will be very appreciated.
I have 32-bit Qt application packed in a bundle.
Firstly, just in-case you missed this, Apple have stated that the next version of the OS (10.14) will not support 32-bit applications, so you'll need to change this if you want to run this application on future versions of macOS.
If you use the debugger, or run a binary from a bundle's Contents/MacOS folder, it executes directly. In contrast, if you double-click on a binary, or use the open keyword from the terminal, a request is sent to Launch Services, to open the application on your behalf.
Launch Services (LS) maintains an association to an application's Bundle Identifier, which is located in the application bundle's Info.plist file.
When a request to open an application with LS occurs, LS is presented with the Bundle Identifier, from the application's plist and LS will execute the application that has been registered, with that identifier.
In the plist, we also have the key CFBundleExecutable, which is defined as the "(Recommended) Name of the bundle's executable file". This is the name of the binary that will likely be executed, residing in the Contents/MacOS folder.
Note, since LS launches the application associated with the given identifier, if there is a copy of the same application on your machine, with the same version number and identifier, it may not be necessarily executing the application you double-clicked, to run.
Therefore, the reason for the crash is most likely due to a different application being launched by LS and not the one you think is being executed. Ensure you have no other copies of the application residing on the machine.
If a crash report is generated, you should be able to see the path to the application, at the start of the images section, where it includes paths to dynamic libraries and frameworks.
While #TheDarkKnight provided great information that is not exactly an answer.
For people who will face the same situation:
In macOS bundle application has working directory related to bundle, but not to the execution file itself as it happens in Windows.
So in my case next code works:
QString path = qApp->applicationDirPath();
if(!path.endsWith("/"))
path += "/";
QProcess p;
p.start(path + "papply", QStringList() << osid << filepath);

Qt deployed (macdeployqt) application does not create a file

I'm facing the following problem:
I wrote a simple application to track working hours. Therefor I create a *.db file programmatically.
Launching the application from Qt Creator (debug or release) works perfectly fine.
I used the macdeployqt tool to get a *.dmg file. When starting the application now the method (see below) cannot open, respectively create the *.db file and I run out of ideas why. In addition to this, the application should output some *.csv files. This also fails.
One more information, running the application as administrator using the sudo command on terminal...well it works and the *.db file and *.csv files get created.
So I am quite sure it must deal with file permissions but I have no idea how to change this except for changing it in the information context menu but this didn't help at all.
Below the method for the *.db file which always returns false when not launching the app from Qt Creator:
QFile file(Globals::Environment::WORKING_DIRECTORY + "/" + "Records.db");
if(file.open(QIODevice::ReadWrite))
return true;
else
{
QMessageBox msg;
msg.setWindowTitle("Error");
msg.setText("Failed to create database file!");
msg.exec();
}
return false;
I am on MacOS 10.11.3.
Qt 5.5.1 (Clang 6.1 (Apple), 64 bit)
If more information is needed, I will provide it of course.
Thanks a lot for every help in advance.

C++ file permissions when automatically started by Windows on login

Problem: my C++ program cannot write to a file after reboot because it is denied access.
The program is added into the registry at the following location, thus enabling it during start up:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
The following snipper within the code is similar to the one that is causing the problems:
ofstream myfile;
myfile.open ("example.txt", ios:app);
myfile << "Writing this to a file.\n";
myfile.close();
Because this snippet of code runs within a loop the file is repeatedly opened and closed, but when the computer is rebooted the program seems to loose file permissions to it, and .open says it got an Acess Denied error with respect to the file.
What is the problem here? How come the program runs when it is initiated by me without admin rights, but not when Windows initiates it?
As I understand the problem is not in C++ libs but in rights that your application has when windows run it. So, try the following:
1) create shortcut for your exe-file;
2) in HKEY_CURRENT_USER\...\Run change name of application from *.exe to *.lnk (just to run application with help of shortcut);
3) in the shortcut's properties in the Advanced Security Settings section change OWNER to SYSTEM.
perhaps this helps.
"example.txt" is relative to your working directory, which is probably different when run at startup. Add a hardcoded path to test this hypothesis.

"windows cannot access the specified device....." error in c++

I am MFC guy working on visual studio 2010 create some executables using visual studio!! but on linux and mac my executables are not working as usual windows!!.
So i decided to use "MinGW" compiler to create executables.
Note:-Please give me one suggestion is that," is minGW is best compiler for cross plateform working ??or any thing else is there??"
I successfully install WinGW compiler on my C drive and start working with following program..
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello ";
return 0;
}
I compile it using following command,
g++ -static-libgcc -static-libstdc++ Main.cpp
I found one executable in same folder with name a.exe :).Working fine:)
But after some time i decide to modified same program in following manner like,
int main ()
{
return 0;
}
I compile it with same command but when i execute it using command line it show me error "Access is denied so i goto that folder and run same executable as "Run as Administrator" it show me one messagebox with the message windows cannot access the specified device path or file. you may not have appropriate permissions
---EDIT--
follwing code is NOT WORKING:-
int main ()
{
int k;
return 0;
}
but this program WORKING :-
int main()
{
int k;
k = 0;
return 0;
}
If you are getting this access denied error, then the most likely cause is that the executable file is open in another process, probably the linker or debugger. Try installing Process Explorer and hit Ctrl+F and type in the name of your .exe. This should show the processes that the .exe file open. Kill those processes (or if you are still debugging, then end debugging first). You then should be able to build again.
Note that this has nothing to do with Microsoft APIs, as in any case you're using gcc.
EDIT: If there are no processes holding the .exe then it may be that there is some other kind of permission problem. Does the .exe file exist? Can you delete the file and rebuild? Another thing to try is run Process Monitor and filter for the name of the .exe -- that may show a regular permission denied error, or perhaps another error such as a sharing conflict.
Note:-Please give me one suggestion is that," is minGW is best compiler for cross plateform working ??or any thing else is there??"
No. And there's nothing else out there.
Use whatever compiler is available on target platform, ensure your code compiles on all of them.
Avoid platform-specific and compiler-specific code at all costs (use cross-platform frameworks).
I successfully install WinGW
There are many different versions of mingw provided by different sites. If you install compiler from mingw.org using mingw-get, it'll probably work. If you install mingw from some other site, it may or may not work.
I compile it using following command,
Use a build systems. cmake, qmake or something similar.
it show me error "Access is denied
Launch process monitor and see after which system call it terminates. It is also possible that your antivirus software interferes with your program, or maybe there's some stray dll in your path or something like that.
Check the permissions for the entire folder in which the executable resides. Trying to 'Run as Administrator' doesn't have any effect if the folder doesn't allow the permissions.
It doesn't have anything to do with your code. This is an environmental problem, something is pretty messed up about the permissions your user account has for one or more of the directories on your hard disk. The generic diagnostic is that the default working directory for the program does not permit read or list access.
A possible starting point would be to use Explorer and right-click the directory where MinGW is installed. Use the Security tab and ensure that your user account has all permissions enabled. Further narrow it down to trying to run the program from the command prompt, using different directories as the default directory.

system() c++ wont run in VirtualBox

I'm trying to compile and run the app, which was created 4 years ago. It was developed for Windows in Embarcadero RAD Studio C++ builder. Now I try to compile and run it in Windows in VirtualBox using the latest version of RAD Studio. I have a system call to another app in my app:
system("dot.exe -Tjpg -o nfa.jpg NFA_graph.txt");
It keeps returning 1 and the file is not created. I also tried
system("Echo %CD% >> z:\log.txt");
and the file is not created. I also tried like this:
FILE *fpipe;
char *command = "Echo %CD% >> z:\log.txt";
char line[256];
if (0 == (fpipe = (FILE*)_popen(command, "r")))
{
perror("popen() failed.");
exit(1);
}
while (fread(line, sizeof line, 1, fpipe))
{
ShowMessage(line);
}
_pclose(fpipe);
And nothing I get. I wonder if the reason of such strange behaviour is that I'm running this all in VirtualBox?
You're not escaping your \ characters. You should use / in file paths, or \\ if you must. In addition, Windows 7 won't let you write to the root directory of a hard drive w/o administrator access.
To determine if a command environment is available, first do this:
if (!system(NULL)) {
// Can't make any system() calls
}
If your command environment is available, then you need to fully specify the path, making sure to escape the \'s like I mentioned above, and don't write anything to a drive's root directory. Also make note that opening files does not default create directories.
No, it's very unlikely. I see few issues with your code: you did not check errno if system() returns 1. It can help you to spot a real problem. Also, all backslashes must be Esc'ed.
I'm suggesting that dot.exe is not in PATH environment variable, that's the reason of the system() failure.