CreateProcess command line arguments

4.6k views Asked by At

I am having a bit of problem using CreateProcess().

In this example, CreateProcess() works perfectly fine:

bSuccess = CreateProcess(
                TEXT("os-util.exe"), 
                TEXT("os-util.exe 0x273e:0x0007:0x0100 --get-channel"), NULL, NULL, TRUE, 
                0,  
                NULL, szFileName, &si, &pi);

The problem is that I want to modify the command line that I pass. I tried a few solutions, but they didn't give me any good results. For example:

LPWSTR cmdArgslistSetChannel[] = { TEXT("os-util.exe"), TEXT("0x273e:0x0007:0x0100"), TEXT("--set-channel"), TEXT("11") };
bSuccess = CreateProcess(
                TEXT("os-util.exe"),
                cmdArgslistSetChannel, NULL, NULL, TRUE, 
                0,  
                NULL, szFileName, &si, &pi);
  1. how can I change just part of the TEXT("")?

  2. how can I make the command line from more than one TEXT("")?

If none of these options are available, what can be done? I want a UI button push to call CreateProcess() with different command line arguments.

1

There are 1 answers

1
Remy Lebeau On BEST ANSWER

Use std::wstring and string concatenations to build up your command line dynamically, eg:

std::wstring cmdArgslistSetChannel = L"os-util.exe";
cmdArgslistSetChannel += L" 0x273e:0x0007:0x0100";
cmdArgslistSetChannel += L" --set-channel";
cmdArgslistSetChannel += L" 11";
bSuccess = CreateProcessW(
                L"os-util.exe",
                &cmdArgslistSetChannel[0],
                NULL, NULL, TRUE, 0,  
                NULL, szFileName, &si, &pi);

Alternatively, use std::wostringstream for the buildup, and then retrieve a std::wstring from it:

std::wostringstream cmdArgslistSetChannel;
cmdArgslistSetChannel << L"os-util.exe";
cmdArgslistSetChannel << L" " << L"0x273e:0x0007:0x0100";
cmdArgslistSetChannel << L" " << L"--set-channel";
cmdArgslistSetChannel << L" " << L"11";

std::wstring cmd = cmdArgslistSetChannel.str();
bSuccess = CreateProcessW(
                L"os-util.exe",
                &cmd[0],
                NULL, NULL, TRUE, 0,  
                NULL, szFileName, &si, &pi);

Either way, you can then replace any individual substring as needed.