c++ - 使用 Windows API 启动外部程序

标签 c++ winapi createprocess

我是 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/

相关文章:

c - 我正在使用 PID 创建一个终止进程

winapi - CreateProcess 以管理员身份运行

c++ - 从 long long 中减去 int

c++ - C++ 中的 Gdiplus 托管还是非托管?

wpf - 如何构建一个 Windows 应用程序来拦截击键和鼠标点击并将其传递给其他应用程序?

检查(作为普通用户)管理进程是否正在运行(C,Windows)

c++ - 为什么我的 Stack 实现中的第 98 个元素的值如此奇怪?

c++ - 基于模板类型的类类型

sockets - 将对 TCP 服务的访问限制为当前用户 session

winapi - 如何初始化 LPBOOL? Kotlin Native 中的 Win32 Api