Search code examples
c++winapishellexecute

Chaining multiple ShellExecute calls


Consider the following code and its executable - runner.exe:

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{
    SHELLEXECUTEINFO shExecInfo;

    shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

    shExecInfo.fMask = NULL;
    shExecInfo.hwnd = NULL;
    shExecInfo.lpVerb = "open";
    shExecInfo.lpFile = argv[1];

    string Params = "";
    for ( int i = 2; i < argc; ++i )
        Params += argv[i] + ' ';

    shExecInfo.lpParameters = Params.c_str();

    shExecInfo.lpDirectory = NULL;
    shExecInfo.nShow = SW_SHOWNORMAL;
    shExecInfo.hInstApp = NULL;

    ShellExecuteEx(&shExecInfo);

    return 0;
}

These two batch files both do what they're supposed to, which is run notepad.exe and run notepad.exe and tell it to try to open test.txt:

1.
runner.exe notepad.exe

2.
runner.exe notepad.exe test.txt

Now, consider this batch file:

3.
runner.exe runner.exe notepad.exe

This one should run runner.exe and send notepad.exe as one of its command line arguments, shouldn't it? Then, that second instance of runner.exe should run notepad.exe - which doesn't happen, I get a "Windows cannot find 'am'. Make sure you typed the name correctly, and then try again" error. If I print the argc argument, it's 14 for the second instance of runner.exe, and they are all weird stuff like Files\Microsoft, SQL, Files\Common and so on. I can't figure out why this happens. I want to be able to string as many runner.exe calls using command line arguments as possible, or at least 2. How can I do that?

I am using Windows 7 if that makes a difference.


Solution

  • You have a bug in this line:

    Params += argv[i] + ' ';
    

    This will add 32 to the pointer argv[i], which isn't what you want. You can separate it to two lines:

    Params += argv[i];
    Params += ' ';
    

    Or use:

    Params += string(argv[i]) + ' ';
    

    You may also want to add quotes around each parameter in case it contains spaces.


    I'd also set fMask to SEE_MASK_NOASYNC, because MSDN states:

    Applications that exit immediately after calling ShellExecuteEx should specify this flag.