我对线程和进程都很陌生,我发现即使有很多关于它的论坛帖子,我也无法让CreateProcess()启动可执行文件。从我对它如何工作的微薄了解我认为我有正确的参数设置但我得到Create Process failed (267)
错误。我试图运行的可执行文件是一个命令行工具,属于Xilinx套件xst。我想要的是在path
全局变量定义的目录中运行xst,以便它可以处理存储在那里的一些文件。我有CreateProcess()的参数错误吗?
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sddl.h>
#include <windows.h>
#include <AccCtrl.h>
#include <Aclapi.h>
std::string path = "C:\\FPGA\\BSP\\BSP\\Xilinx\\SingleItemTest\\";
void testXST(std::string filePath, std::string arguements) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcess(
LPTSTR(filePath.c_str()),
LPTSTR(arguements.c_str()),
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
LPTSTR(path.c_str()),
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
int main()
{
std::string xstPath = "C:\\Xilinx\\14.7\\ISE_DS\\ISE\\bin\\nt\\xst.exe";
std::string args = " -h";
testXST(xstPath, args);
return 0;
}
我为xst设置了环境变量,所以我可以在命令行的任何地方调用它,但是因为我提供了不重要的可执行文件的直接路径,是否正确?
答案 0 :(得分:3)
这里有几个问题。
首先,正如@PaulMcKenzie在评论中指出的那样,lpCommandLine
参数的类型为LPTSTR
,而不是LPCTSTR
。因此,您无法传递std::string::c_str()
的返回值,因为它返回const缓冲区。您需要将std::string
的内容复制到char
数组中,然后将其传递给CreateProcess()
。
其次,您没有传递lpCommandLine
参数的正确值。这个值不是进程的参数,而是要执行的整个命令行。除了任何参数之外,这必须包括可执行文件的路径。可以把它想象成你在命令提示符下输入同样的东西。