Open an additional program with c++ on Mac [XCode] - c++

I want to open an additional program with c++ on XCode.
It is Firefox.
But if I make
Shell Execute("file://localhost/Applications/Firefox.app");
There is an error 'ShellExecute' was not declared in this scope
In other forum there was a clue to include windows.h and shellapi.h
#include <shellapi.h>
#include <windows.h>
but that makes other errors
shellapi.h: No such file or directory
windows.h: No such file or directory
What should I do? I want to open frefox with c++ in XCode on Mac?

Try running this in Terminal to open Firefox:
open -a Firefox http://www.ibm.com
If that does what you want, you need to wrap it in system() like this:
#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
std::system("open -a Firefox");
}

ShellExecute() is only available through the Windows API. You don't have a Windows system.
You can simply use the (more portable) system() function, or one of the exec() functions available on POSIX compliant systems.

I tried the same with chrome and I had to set it in inverted commas:
int main() {
system("open -a 'Google Chrome'");
return 0;
}
with the apostrophe it worked!

Related

How do I execute an existing binary that's in the same location as the main cpp file?

I'm making a program that depends heavily on another C binary. Since I don't feel like learning how to use headers and what not yet, I wanted to take the simple rout and just run a pre-compiled binary from the same folder in my cpp program.
Right now, my folder is setup like this: It has main.cpp, CMakeLists.txt, and the ibootim binary. Inside of main.cpp, how would I call ibootim?
From coding in python, it's taught me that I should be able to run
system("./ibootim");
but that doesn't work. Terminal tells me that there's no file found. Obvioiusly if I were to put the entire path to that binary, it would work. However, if other users were to download this, it would not work for them since they don't have the same computer, username, etc. as I do.
So my first question, my primary concern would be:
How do you run another binary that's in the same directory in a c++ program?
If this isn't possible for some reason, then I can try downloading ibootim from source and maybe using the header file:
How do you execute code from a C header in a C++ program?
For Windows, you can use GetModuleFileNameW() to get the absolute path to the running exe even if the working directory is different from the exe's directory. Then, you can use PathRemoveFileSpecW() to remove the filename from the path to get the exe's directory path. Then, you can use ShellExecuteW() to launch the exe with the filename you want while telling the function what directory to look in for the exe.
Here's a command-line example:
#include <windows.h>
#include <shlwapi.h>
#include <iostream>
#include <string>
#include <cwchar>
#include <cstdlib>
#include <cstdint>
#include <stdexcept>
#include <clocale>
using namespace std;
wstring get_exe_dir() {
wchar_t buffer[65537] = {L'\0'};
if (!GetModuleFileNameW(NULL, buffer, sizeof(buffer))) {
MessageBox(NULL, "GetModuleFileNameW length error", "EXE path is too long!", MB_OK);
throw length_error("");
}
wcout << buffer << L'\n';
PathRemoveFileSpecW(buffer);
wcout << buffer << L'\n';
return buffer;
}
int main() {
setlocale(LC_CTYPE, ".OCP");
const wstring exe_dir = get_exe_dir();
const intptr_t result = reinterpret_cast<intptr_t>(ShellExecuteW(NULL, L"open", L"\"other.exe\"", NULL, exe_dir.c_str(), SW_SHOWNORMAL));
if (result < 33) {
MessageBox(NULL, "Error launching other.exe", "Launch error", MB_OK);
return EXIT_FAILURE;
}
}
// g++ -Wall -Wextra exe_path.cc -o exe_path -O3 -s -lshlwapi
Maybe Mac has a similar function. I see _NSGetExecutablePath(). For shellExecute(), I see this answer that might help. But, perhaps system() is fine on Mac where it doesn't spawn another terminal window like it does on Windows.
In c++ if you want to use a binary you can use std::system() function.
But to do this the binary must be on the PATH.
If your binary is not on the path you can do something like this.
#include <iostream>
int main(){
#if _WIN32
std::system("./mybinarie.exe");
#else
std::system("./mybinarie");
#endif
return 0;
}
Starting the shell with std::system will ensure that you are in your working folder and that if the binary is in the working folder it should work.

Cannot create new text files by C++ in Codeblocks in Mac

I have a C++ code in Codeblocks in Mac OS that should create some .txt files and write something on them. It worked on Windows, however, it does not run correctly on Mac. It does not make any errors, and it announces that the procedure is terminated with status 0; however, it does not create any .txt files!! What can be the problem?
It even does not claim that Codeblocks needs special permission. Moreover, the execution window background is White; different from the usual execution window of Codeblocks, which is Black.
This is the code:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
int main(){
for (int i=1;i<=1;i++){
ofstream fout ("test1.txt");
int n=rand()%10+1;
// int n=15;
fout<<n<<endl;
}
return 0;
}
This code just does not create any .txt file in Mac, while it worked on Windows.
"test1.txt",this is a relative path, you maybe hava use clion,you must know where relative path, but it is using a absolute path.

Is it possible to output a variable as a cmd command from a c++ program

Im making a simple command line program just to get my C++ knowledge to a basic level,
I would like to send a command from the C++ program to CMD that is made up of part of a command and the rest a variable here is my code so far:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
string directory;
cout<<"Input the directory you would like to make your folder in: ";
cin>> directory;
system("mkdir" directory);
}
I get the error:
error: expected ')' before 'directory'|
Does anyone know a way of doing this? In the end I would like CMD to execute the command "mkdir C:*Inputted Directory*
I have hacked around with it trying to work it out, but to no success, I also have had a look on the internet but to no avail, thanks in advance.
Rather than call out to the shell use the _mkdir (for Windows) or mkdir (for Linux) function instead:
_mkdir(directory.c_str());
For Windows you'll need #include <direct.h>, for Linux you'll need #include <sys/stat.h> and #include <sys/types.h>

Error using wget in c++

I downloaded wget from gnuwin32 and I am trying to run the command in a c++ program using the system() function. I am using visual studio 2012 on a windows OS as my compiler. wget runs on the command line but does not run when I put it in the system function. My error is " 'wget' is not recognized as an internal or external command,operable program or batch file"
Here is my code:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = string("wget -O test.csv \"http://")+"somewebsitelink\"";
const char *x = str.c_str();
cout << str << endl;
system(x);
system("pause");
return 0;
}
Visual Studio is probably overriding your normal path somehow if it really is in the path and not working.
Just put the full path in manually:
system("c:/path/wget ...");
wget needs have its directory in the list of directories in your PATH environment variable.
Since you are using Windows, you'll have to modify it your user configuration. It has been years since I have used windows, so I no longer recall exactly where it is.

Windows BlockInput function is not working

Why BlockInput isn't working
#include <iostream>
#include <windows.h>
#include <winable.h>
int main() {
BlockInput(true);
Sleep(10000);
return 0;
}
and it simply doesn't block anything!
I can still do everything like I haven't even done that.what I also find weird is that MSDN sais that it should be declared in winuser.h and it is in Winable.h + I thought that winable.h is in windows.h but It's not, I had to include it seperatly >_>
If it helps my IDE is:
Code::Blocks 10.05 MinGW
EDIT: Actually I did the GetLastError() and it prints error 5 ERROR_ACCESS_DENIED
How may I get the access?
Run your code as an administrator. Right-click on the executable and click 'run as Administrator'.