How to find path to a specific program

Hi,

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.

Hope the question is clear enough.


roarkr
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.
Last edited on
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?

Not a standard function. What you're trying to do is a file-search of the entire filesystem.

Normally when I need to iterate through directories, I use boost::filesystem. It's platform independent.

Something like this might work for you (plus I added a sample int main() which i just tested with).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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.
Last edited on
Topic archived. No new replies allowed.