使用createProcess()运行批处理文件

时间:2016-11-17 12:05:23

标签: windows batch-file createprocess

是否有必要将lpApplicationName设置为cmd.exe,如文档中所述,以运行批处理文件?

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

假设批处理文件的路径为" C:/Users/abc.bat"。 我如何传递上述字符串作为参数传递给批处理文件?

1 个答案:

答案 0 :(得分:1)

承担标准配置,答案是否定的,它不是必需。您可以在lpCommandLine参数中包含批处理文件。其余的参数只是在批处理文件后面跟引号。

<强> 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 );    
};

<强>输出

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