简体   繁体   中英

CreateProcess + Call command line tool

I'm pretty new to threading and processes and I find I am unable to get CreateProcess() to start an executable even though there are lots of other forum posts about it. From my meagre understanding of how it works I think that I have the right parameters set but I get a Create Process failed (267) error. The executable I am trying to run is a command line tool that belongs to the Xilinx suite called xst. What I want is to run xst in the directory that is defined by the path global variable so it can work on some files that are stored in there. Do I have the parameters to CreateProcess() wrong?

#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;
}

I have the environment variables set for xst so I can call it anywhere from the command line but since I am giving the direct path to the executable that shouldn't matter, correct?

There are a couple of problems here.

First, as @PaulMcKenzie pointed out in the comments, the type of the lpCommandLine parameter is LPTSTR , not LPCTSTR . Therefore you can't pass the return value of std::string::c_str() , since that returns const buffer. You will need to copy the contents of the std::string into a char array, and pass that to CreateProcess() .

Secondly, you aren't passing the correct value for the lpCommandLine parameter. The value of this isn't the arguments to the process, but rather the entire command line to execute. This must include the path to the executable in addition to any arguments. Think of this as what you would type at a command prompt to run the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM