簡體   English   中英

CreateProcess + Call命令行工具

[英]CreateProcess + Call command line tool

我對線程和進程還很陌生,即使有很多其他論壇帖子也無法使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設置了環境變量,因此我可以在命令行中的任何地方調用它,但是由於我給出了不重要的可執行文件的直接路徑,對嗎?

這里有兩個問題。

首先,正如@PaulMcKenzie在評論中指出的那樣, lpCommandLine參數的類型是LPTSTR ,而不是LPCTSTR 因此,您不能傳遞std::string::c_str()的返回值,因為它會返回const緩沖區。 您將需要將std::string的內容復制到char數組中,並將其傳遞給CreateProcess()

其次,您沒有為lpCommandLine參數傳遞正確的值。 此值不是進程的參數,而是要執行的整個命令行。 除任何參數外,它還必須包括可執行文件的路徑。 您可以將其視為在命令提示符下鍵入以運行相同內容的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM