In my C++ program, lets call it menu.exe, I want the user to be able to run a specific program when selecting a specific menu, let's call this program cios.exe.
As the users of my program can have the cios.exe installed in any folder location, I need a way to first locate where the cios.exe is located and then start it with ShellExecute. My problem is : How do I find the path to where cios.exe is located in anyones PC as it can be installed it any folder location.
the PATH enviornment variable is used by your OS to locate executables or dynamic libraries (DLL/so). You need to ensure that the path containing cios.exe is included in the PATH enviornment variable. (Usually done during the installation of cios.exe).
In windows if you CreateProcess() or LoadLibrary() with the name of that executable, windows will find the application for you. Otherwise, you'll need to do a search on your PC by iterating through folders to find it.
as the cios.exe installer don't uses the PATH environment variable I then need to do a search to find the full folder path to where cios.exe is located.
That brings up my next question:
Is there a C++ function for doing this with "cios.exe" as argument?
#include <boost/filesystem.hpp>
#include <string>
#include <iostream>
namespace fs = boost::filesystem;
// Returns the absolute path of the first instance
// of the file
// filename = name + extension of the file
// rootDir = starting directory to look in, checks recursivley
// Using std::string for param/return to avoid changing
// interface when non boost backend is used
std::string findFile(std::string filename, std::string rootDir)
{
fs::path dir(rootDir);
fs::recursive_directory_iterator it( dir ), endIter;
for(;it != endIter; ++it)
{
if (it->path().leaf().string() == filename)
return it->path().string();
}
return""; // not found.
}
int main()
{
std::cout << findFile("main.cpp", "/home/stew/SVN/Test");
}
/home/stew/SVN/Test/Test/main.cpp
Process return 0 (0x0) execution time : 0.002 s
Press ENTER to continue.