0

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.

IVlad
  • 43,099
  • 13
  • 111
  • 179
  • What's the working directory? – lunixbochs Mar 15 '10 at 16:41
  • Can you try to print out the `SHELLEXECUTEINFO` structure's value before calling the `ShellExecute` function? – dirkgently Mar 15 '10 at 16:42
  • @lunixbochs - the bat file and the runner.exe file are both in the same directory. notepad.exe is somewhere else, but should it matter, since `runner.exe notepad.exe` works fine? Could `shExecInfo.lpDirectory = NULL;` be the problem? I'm not sure I answered your question, sorry if I didn't... – IVlad Mar 15 '10 at 16:44
  • @dirkgently - how do you print a structure's value? Do you mean the address of the `shExecInfo` variable? The address is the same for both instances of the program - is that supposed to happen? – IVlad Mar 15 '10 at 16:47

3 Answers3

3

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.

interjay
  • 107,303
  • 21
  • 270
  • 254
1

The problem is this line:

Params += argv[i] + ' '

Because argv[i] is of type char * that adding 32 to argv[i] so will give you a pointer to the middle of random memory.

I would rewrite that to:

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

Or if you wanted to be real good, you can modify that to only add a space when it's really necesssary:

for ( int i = 2; i < argc; ++i )
{
    if (!Params.empty())
        Params += ' ';
    Params += argv[i];
}
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
1

I think the problem is how you create the command line parameters for your next call:

 Params += argv[i] + ' ';

does not work as expected. Try the following out:

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

#ifdef _UNICODE
    typedef std::wstring tstring;
#else
    typedef std::string tstring;
#endif

int _tmain(int argc, TCHAR *argv[])
{
    {
        std::ofstream f("C:\\output.txt", std::ios::app);
        f << "app called" << std::endl;
    }
    SHELLEXECUTEINFO shExecInfo;

    shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

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

    tstring Params(_T(""));
    for ( int i = 2; i < argc; ++i )
    {
        Params += argv[i]; 
        Params += _T(' ');
    }

    shExecInfo.lpParameters = Params.c_str();

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

    ShellExecuteEx(&shExecInfo);

    return 0;
}
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • @dirkgently - hope you don't mind, but I modified your code to be fully tchar enabled so it could be compiled either in Unicode or MBCS mode. – R Samuel Klatchko Mar 15 '10 at 18:06