createProcess 呼叫命令行工具

CreateProcess + Call command line tool

本文关键字:工具 命令行 呼叫 createProcess      更新时间:2023-10-16

我对线程和流程的新手很新,我发现即使还有很多其他论坛帖子,我也无法让createProcess()启动可执行文件。从我认为我认为我的参数设置正确的微不足道,但我会收到Create Process failed (267)错误。我试图运行的可执行文件是属于Xilinx套件的命令行工具,称为XST。我想要的是在由path Global变量定义的目录中运行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设置的环境变量,以便我可以在命令行中的任何地方称呼它

这里有几个问题。

首先,正如@paulmckenzie在评论中指出的那样,lpCommandLine参数的类型是LPTSTR,而不是LPCTSTR。因此,您无法传递std::string::c_str()的返回值,因为那会返回const缓冲区。您需要将std::string的内容复制到char数组中,然后将其传递给CreateProcess()

其次,您没有传递lpCommandLine参数的正确值。它的值不是该过程的参数,而是要执行的整个命令行。除了任何参数外,这还必须包括通往可执行文件的路径。将其视为您将在命令提示下运行同一件事的键入的内容。