1

I am trying to build a C++ application that makes a bunch of command line calls on the Windows command prompt using the system() function. The C++ runs in directory BSP below. BSP contains a sub-folder BSP/XST/Files. One of the commands the application makes, needs to call a command line tool (Xilinx Synthesis tools) that needs to run in the Files directory.

BSP
|---XST
|---|---Files

Doing it manually on the command prompt I would do something similar to:

>>cd XST
>>cd Files
>>xst -h

Is there a way to call a tool in the sub-directory from the BSP directory? I looked at this question here, but it does not work. I'm guessing because they are talking about an executable that is stored within the sub-directory whereas I am calling a command line tool (i.e. uses environment variables).

To simplify: Is there a command/option to run a command line tool in a sub-folder on the Windows command prompt? I can just emulate the statement via my C++.

Community
  • 1
  • 1
Nick
  • 862
  • 12
  • 35
  • You specify the full path to the tool. Or are you talking about setting the application's working directory? You can call the `SetCurrentDirectory` function. – Cody Gray - on strike Jun 15 '16 at 15:03
  • As its not answering your question as such i'll leave this as a comment. If you're only working on windows I'd recommend using [CreateProcess](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx) as this lets you specify all the various parameters when you spawn a process including its working directory, etc. – Mike Vine Jun 15 '16 at 15:28
  • @MikeVine that looks really interesting. Thanks I'll look into it. – Nick Jun 15 '16 at 15:33

1 Answers1

1

As suggested by @CodyGray in the comments, my idea was to use SetCurrentDirectory. If your program is located in the BST directory and you want to run xst in the sub-folder XST\Files relative to it, then it makes sense to also use GetModuleFileName. Use this function to retrieve the path to your program and then replace the file name by your sub-folder. Finally change directory to the modified path:

#include <string>
using namespace std;

int main()
{
    // Get the path to your program.
    char moduleFilePath[MAX_PATH];
    GetModuleFileName(NULL, moduleFilePath, MAX_PATH);

    // Find the backslash in front of the name of your program.
    string::size_type pos = string(moduleFilePath).rfind("\\");

    // Replace the program name by your sub-folder.
    string subFolderPath = string(moduleFilePath).substr(0, pos) + "\\XST\\Files";

    // Change into the sub-folder relative to your program.
    SetCurrentDirectory(subFolderPath.c_str());

    // Execute some program in your sub-folder.
    system("type test.txt");

    return 0;
}

As I don't have xst, I put a text file test.txt into the sub-folder for testing purposes. The file just contains Test test test, so the program above prints out the following:

Test test test

But as suggested by @MikeVine, CreateProcess might be a smarter solution.

honk
  • 9,137
  • 11
  • 75
  • 83