Question

For example: I'm on MS DOS, I have a source code in the folder C:\Documents and Settings\Programs. Can i make my source code use a program (for example gnuplot) that is in a random folder?

Was it helpful?

Solution

Here are some options:

  1. Search in the system PATH for the executable you want to run
  2. Allow the user to specify the location on the command-line
  3. Store the location in a configuration file, and allow the user to specify it during install (if you have an install process) or by editing the file by hand

Ideally you'd do all 3

OTHER TIPS

http://www.codeproject.com/KB/system/newbiespawn.aspx

ShellExecute will look into the PATH environment variable, so you don't need to specify the full PATH. Now, if it's really a random location and it's not even in the PATH environment variable, then I guess you are out of luck.

If they aren't even in the PATH, then you have to search for it in the candidates folder. Here's sample code on how to traverse a file system path in C++.

And an example using Boost:

directoryList.h

#ifndef DIRECTORYLIST_H_INCLUDED
#define DIRECTORYLIST_H_INCLUDED
#define BOOST_FILESYSTEM_NO_DEPRECATED

#include <iostream>
#include <list>
#include <string>


class directoryList {

    public:
        directoryList();
        ~directoryList();
        std::list<std::string> getListing(std::string path);
};
#endif // DIRECTORYLIST_H_INCLUDED

directoryList.cpp

#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/convenience.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/progress.hpp"

#include "directoryList.h"

using namespace std;
namespace fs = boost::filesystem;

directoryList::directoryList() {}
directoryList::~directoryList() {}

list<string> directoryList::getListing(string base_dir) {

    list<string> rv;
    fs::path p(base_dir);

    for (fs::recursive_directory_iterator it(p); 
         it != fs::recursive_directory_iterator(); ++it) {

        string complete_filename = it->path().string();
        rv.insert(rv.begin(),complete_filename);

    }

    return rv;

}

Usage sample:

directoryList *dl = new directoryList();
filenames = dl->getListing("C:\\Program Files");
//search for the file here, or modify the getListing to supply a filter

Also there are some core functions _exec/exec and its modifications. Similar functions are available for Linux.

The location of source code has nothing to do with the way programs are located by system() call (I assume you use that call). The only relevant consideration is the location of the compiled executable.

Please take a look at PATH environment variable in Windows - this is how programs are found. It's a semicolon-separated list of directories where Windows looks for executables and BAT files and DLLs. To that list, current directory and (I think) the place where your EXE is are prepended.

The PATH is set in Windows XP from System control panel widget Advanced tab Environment button. For Vista, things are more complicated - you need to do it as administrator.

As Vinko said, the PATH environment variable determines where Windows will look for program files.

It's usually best to avoid hard-coding the path of an executable into your compiled program. Even if gnuplot is in a particular folder on your machine then it might well not be in the same folder on someone else's computer. This would cause your call to the other program to fail. You could store it in the registry and let the user configure the program location, or provide an installer that searched for it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top