1

I am trying to launch multiple batch files at the same time to make it easier for what I'm doing. I would like them to have separate command windows and have their own titles like "Server: Hub", "Server: Spleef", etc. I know Startupinfo can do that somehow, but I don't even know what code to put in for that. I got the CreateProcess code from here: https://stackoverflow.com/a/15440094/18369260.

Code:

#include <windows.h>
#include <string>
#include <iostream>
using namespace std;

VOID startup(LPCTSTR lpApplicationName)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

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

    CreateProcess(lpApplicationName,
        NULL,
        NULL,
        NULL,
        FALSE,
        0,
        NULL,
        L"C:\\Users\\Anston Sorensen\\Desktop\\Minecraft Servers\\Minecraft Server\\",
        &si,
        &pi
    );
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
}

int main() {
    startup(L"C:\\Users\\Anston Sorensen\\Desktop\\Minecraft Servers\\Minecraft Server\\ServerStart512mb.bat");
    startup(L"C:\\Users\\Anston Sorensen\\Desktop\\Minecraft Servers\\Hub\\ServerStart512mb.bat");
    startup(L"C:\\Users\\Anston Sorensen\\Desktop\\Minecraft Servers\\Spleef\\ServerStart512mb.bat");
    startup(L"C:\\Users\\Anston Sorensen\\Desktop\\Minecraft Servers\\PVP\\ServerStart512mb.bat");
}

So far I only get the first file to launch.

  • what happens, does the second startup call execute, does it abort, where does it stop? – pm100 Mar 12 '22 at 01:31
  • 3
    works for me if I change to run wordpad 4 times. I note you are not checking the return of CreateProcess, do that – pm100 Mar 12 '22 at 01:45
  • 2
    I don't believe you can launch a `.bat` file directly with `CreateProcess`. It is not an executable, it is a script that runs under the `cmd.exe` command processor. Try executing `cmd.exe` with `/c file.bat` as the command-line arguments. Or possibly look into `ShellExecute`. – jkb Mar 12 '22 at 02:29
  • yeah I'll try that. But, ShellExecute doesn't offer creating them with titles. @pm100 it completely stops after the first one. I will look into the return deal – ThrownRedstone Mar 12 '22 at 04:00
  • @jkb actually how do I put the command line arguments in there properly? – ThrownRedstone Mar 12 '22 at 04:08
  • @jkb DUH, I could just set the title in the batch file. I guess I'll just stick with ```ShellExecute``` then until someone figures out how to use this because you can do more things with ```CreateProcess```. – ThrownRedstone Mar 12 '22 at 04:16
  • 1
    Regarding the command-line parameters, put them into the second parameter to `CreateProcess`. Another option would be to leave the first parameter `NULL` and pass the entire command in the second parameter, something like `CreateProcess(NULL, L"cmd.exe /c file.bat",...`. – jkb Mar 12 '22 at 05:07

0 Answers0