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

606 Views Asked by At

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?

2

There are 2 best solutions below

1
Luc Lambour On BEST ANSWER

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.

0
Shadow2531 On

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.