CreateProcess is able to execute batch files, but the documentation says the opposite

Consider the following files:

a.bat:

@echo Hello from bat %1

and c.cpp:

#define UNICODE
#include <windows.h>
#include <stdio.h>

void check(TCHAR *cmd, TCHAR *args) {
  STARTUPINFO sinf;
  PROCESS_INFORMATION pinf;
  memset(&sinf, 0, sizeof sinf);
  sinf.cb = sizeof(sinf);

  CreateProcess(cmd, args, NULL, NULL, FALSE, 0, NULL, NULL, &sinf, &pinf);
  WaitForSingleObject(pinf.hProcess, INFINITE);
}

int main() {
  TCHAR cmd1[] = L"a";
  TCHAR cmd2[] = L"a.bat";
  TCHAR cmdargs1[] = L"a argument";
  TCHAR cmdargs2[] = L"a.bat argument";
  TCHAR args[] = L"argument";

  #define run_check(a, b) printf(#a " + " #b "\n"); fflush(stdout); check(a, b)
  run_check(cmd1, cmdargs1);
  run_check(cmd1, cmdargs2);
  run_check(cmd1, args);

  run_check(cmd2, cmdargs1);
  run_check(cmd2, cmdargs2);
  run_check(cmd2, args);

  run_check(NULL, cmdargs1);
  run_check(NULL, cmdargs2);
  printf("Done\n");
  return 0;
}

Please note that I did not specify cmd.exein any of the calls CreateProcess, while MSDN says that I should do this

To run a batch file, you must run the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine with the following arguments: / c plus the name of the batch file.

However, I get the following output:

cmd1 + cmdargs1
cmd1 + cmdargs2
cmd1 + args
cmd2 + cmdargs1
  Hello from bat argument
cmd2 + cmdargs2
  Hello from bat argument
cmd2 + args
"argument"     
,     .
NULL + cmdargs1
NULL + cmdargs2
  Hello from bat argument
Done

, , .bat lpApplicationName, lpCommandLine, . .cmd, .vbs. - ? " " Windows? ( Windows 7 HP) - , ?

+3
1

, . - , , " -", . Windows, .

batch/vbs.

CMD/BAT, GetEnvironmentVariable("ComSpec"), cmd.exe, CreateProcess, = cmd.exe, arguments /C path_to_CMD_file arg1 arg2. , . , ", ^".

VBS, ExpandEnvironmentStrings("%windir%\\System32\\cscript.exe") wscript.exe, VBS .exe. , .

0

All Articles