Run a batch file using createProcess()
Asked Answered
S

1

1

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?

Selfeducated answered 17/11, 2016 at 12:5 Comment(0)
H
1

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
Hayton answered 17/11, 2016 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.