简体   繁体   中英

Run a batch file using createProcess()

Is it necessary to set lpApplicationName to cmd.exe as mentioned in the documentation to run a batch file?

  • "port=5598 dbname=demo host=localhost"
  • "port=5599 dbname=demo host=localhost"
  • "C:/tmp/000002AB-1.16432"
  • "C:/bin/pg_restore.exe"

Assume that the path of the batch file is "C:/Users/abc.bat". How can i pass the above strings are to be passed as arguments to the batch file?

Assumming a standard configuration, the answer is no, it is not required . You can include the batch file in the lpCommandLine argument. The remaining arguments just follow the batch file with quotes where needed.

test.cmd

@echo off
    setlocal enableextensions disabledelayedexpansion
    echo %1  
    echo %~1
    echo %2  
    echo %~2

test.c

#define _WIN32_WINNT   0x0500
#include <windows.h>

void main(void){

    // Spawn process variables
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    CreateProcess(
        NULL
        , "\"test.cmd\" \"x=1 y=2\" \"x=3 y=4\""
        , NULL
        , NULL
        , TRUE
        , 0
        , NULL
        , NULL
        , &si
        , &pi 
    );

    WaitForSingleObject( pi.hProcess, INFINITE );
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );    
};

Output

W:\>test.exe
"x=1 y=2"
x=1 y=2
"x=3 y=4"
x=3 y=4

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