Migwell
Migwell

Reputation: 20117

Starting an external program using the Windows API

I'm fairly new to the Windows API, and I'm having a lot of trouble just getting the dot program (which creates a graph using graphviz) to run from my program.

The command that I want to run, which works in cmd is this:

dot -Kfdp -n -Tpng -o "D:\Programming\VS\Visual Studio 2013\Projects\Graphs\Graphs\Input\topsort.png" "D:\Programming\VS\Visual Studio 2013\Projects\Graphs\Graphs\Input\topsort.dot"

However I'm consistently getting the error 2, The system cannot find the file specified

Here's my (simplified) code. Note that the value of ca at the end of the processing is the correct string, so that's not the problem.

//From earlier in the file
//#include <Windows.h>
//std::string filepath = "D:\\Programming\\VS\\Visual Studio 2013\\Projects\\Graphs\\Graphs\\Input\\topsort";

//Create structs
PROCESS_INFORMATION ProcessInfo; 
STARTUPINFO StartupInfo; 
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo;

//Create a string and copy to a char*
string procArg = "dot -Kfdp -n -Tpng -o \"" + filepath + ".png\" \"" + filepath + ".dot\"";
char* ca = new char[procArg.size() + 1];
int size = (procArg.size() + 1) * sizeof(*ca);
int count = procArg.length();
procArg._Copy_s(ca, size, count);
ca[procArg.size()] = '\0';

//At the end of this, size = 182, count = 181,
//and ca = dot -Kfdp -n -Tpng -o "D:\Programming\VS\Visual Studio 2013\Projects\Graphs\Graphs\Input\topsort.png" "D:\Programming\VS\Visual Studio 2013\Projects\Graphs\Graphs\Input\topsort.dot"

//Attempt to create process
CreateProcess(NULL,
    ca,
    NULL,
    NULL,
    FALSE,
    0,
    NULL,
    NULL,
    &StartupInfo,
    &ProcessInfo);

Thanks!

Upvotes: 0

Views: 500

Answers (2)

david.pfx
david.pfx

Reputation: 10868

CreateProcess has a number of different choices. The one you have picked may not be the best. Choices are:

  1. Pass the full path for the application as the first argument to CreateProcess. In that case your command line will probably be interpreted correctly. This is my preference.
  2. Include the application as the first token of the command line. In this case you usually have to include it twice, once for Windows and once to be argv[0] of the called program. If DOT is not on the Windows PATH, this will have to be the full path. If the path has any spaces you need to quote them. I find this a pain, so I avoid this way.
  3. Invoke the program via CMD. Pass CMD as the first argument to CreateProcess and let it do the work. You will still need DOT (or the full path) as the first command argument, but only once. Use this method if you are doing command line redirection or running a BAT file.

Upvotes: 1

user3526274
user3526274

Reputation: 31

Looks like system could not find the PATH for dot program. I suggest you try giving absolute path of the dot program as part of procArg. This can also be validate by executing CMD and then try to execute the same command there.

Upvotes: 3

Related Questions