我是 Windows API 的新手,我在让 dot 程序(使用 graphviz 创建图形)从我的程序运行时遇到了很多麻烦。
我想在 cmd 中运行的命令是这样的:
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"
但是我一直收到错误 2,系统找不到指定的文件
这是我的(简化的)代码。请注意,处理结束时 ca 的值是正确的字符串,所以这不是问题所在。
//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);
谢谢!
最佳答案
看起来系统找不到dot 程序的路径。我建议您尝试将 dot 程序的绝对路径作为 procArg 的一部分。这也可以通过执行 CMD 来验证,然后尝试在那里执行相同的命令。
关于c++ - 使用 Windows API 启动外部程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23027803/