Check if adb is installed - c++

In Qt, I'd like to check if the adb command in a cmd is working (--> installed). I already tried to work with system and process but, wasn't able to get any information about if adb could be ran.

The solution I can think of would be iterating over all directories in PATH environmental variable and checking whether the adb file exists there or not.
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QStringList dirs = env.value("PATH").split(":");
foreach(QString p, dirs){
QFileInfo check_file(p + "/" + "adb");
if (check_file.exists() && check_file.isFile()) {
qDebug() << "Yes!";
}
}
By the way, I tested in on my linux machine and worked fine.
Windows
On Windows machines you would need to replace adb with adb.exe and : with ;.

Related

Getting git commit id on Qt qmake project at make time

On Windows 10 Qt 5.15, I'm getting git commit id on Qt qmake project at qmake time with
COMMIT = '\\"$$system(git rev-parse --verify master)\\"'
DEFINES += COMMIT_VERSION=\"$${COMMIT}\"
And on main.cpp I can print that with qDebug() << COMMIT_VERSION;
Since some commits do not require a full project rebuild I'm getting old (not the latest) commit ids.
How can I get that updated on every build?
I use Ubuntu but for doing what you want I try this:
what you do is like in cmd or terminal always type this command :
git rev-parse --verify master
so for doing this I use QProcess like this :
first of all, I create one bash file and put that command there.
I always put commands in one shell script file because it's easy and just put.sh file in the start process.
test.sh
#!/bin/sh
cd /home/parisa/projectFolderName;git rev-parse --verify master
and this is code :
QProcess *myProcess = new QProcess();
myProcess->start("/home/parisa/untitled1/test.sh");
myProcess->waitForFinished(3);
QString str = myProcess->readAll();
qDebug() << "git id : " << str;
delete myProcess;
and this is the output :
In QTCreator short cut Alt + K or Alt +G will show you the git log.

The command started in the QProcess does not return a response on linux

I'm tryin get loaction of node package. Using QProcess to execute commang which node:
QProcess process;
process.start("/usr/bin/which", QStringList() << tr("node"));
process.waitForFinished(-1);
response = process.readAllStandardOutput();
qDebug() << response;
But in debug I got "".
Project located on GitHub. Problem in https://github.com/Dissfall/nodeGator/blob/develop/mainwindow.cpp
It may depend under what shell is running your app in QtCreator and under what one in terminal (bash/sh/csh etc.). The PATH variable may differ;
u can check process.exitCode() i.e. when 'which' doesn't find tool, it return 1; otherwise 0
may be, try to list the env. with:
qDebug() << "env:" << QProcessEnvironment::systemEnvironment().toStringList();
You can see actual settings in QtCreator -> Projects -> ur KIT -> Run and "Run Environment" details. There is an option to set in which environment the app is running: build/system/clear - try change.

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);

How to launch terminal in a specific folder?

I've tried both launching /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal as a process and using std::system to launch it from sh script (open -a /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal) with different arguments (detsdir as argument, cd destdir etc.). Nothing works. It either opens in ~/, or it says "destdir is a directory` and logs off.
I've also tried launching it as a process with destdir set as working directory (using QProcess::startDetached). No luck either, it opens in ~/.
You can do it like this, with osascript:
osascript -e "tell application \"Terminal\" to do script \"cd ~/Desktop\""
This will create a new terminal window with working directory ~/Desktop.
QProcess has method setWorkingDirectory()

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.