0

I am using the example from here to restart an application:

Restart an application by itself

It works well. Until I want to take the parameter passed into the application and pass it along to the restart instance.

lstrcpy(tbuf, lpCmdLine);
lstrcat(tbuf, L"OSP_PID.EXE ");
lstrcat(tbuf, lpCmdLine);
wsprintf(buf, L"/C ping 127.0.0.1 -n 5 &&  \"%s \"" , tbuf);
ExecWin(L"cmd.exe", buf, TRUE);
PostMessage(hWnd, WM_CLOSE, 0, 0L);


buf==  L"/C ping 127.0.0.1 -n 5 &&  \"C:\\VEC25WIN8\\DEBUG\\OSP_PID.EXE C:\\VEC25WIN8\\DEBUG\\ \""  


//lstrcat(tbuf, lpCmdLine);
buf==  L"/C ping 127.0.0.1 -n 5 &&  \"C:\\VEC25WIN8\\DEBUG\\OSP_PID.EXE  \""    

In the first example, this is what I am trying to accomplish. The ping works but then nothing.

The second example shows the passed argument removed. This works, the application starts up, but there is no argument being passed along.

The "buf==" is from the debug WATCH panel.

I don't see anything wrong with this. Please advise.

Boyd

static void ExecWin(LPTSTR acTaskname, LPTSTR acParameters, int nShow )
{ // everything is on the stack - reenterable

    SHELLEXECUTEINFO execinfo;
    ZeroMemory(&execinfo, sizeof(SHELLEXECUTEINFO));
    execinfo.cbSize = sizeof(SHELLEXECUTEINFO);
    execinfo.lpParameters = acParameters;
    execinfo.lpFile = acTaskname;
    execinfo.nShow = nShow;
    if ((ShellExecuteEx(&execinfo)) && ((int)execinfo.hInstApp > 32))
    {
        return;
    }
    return;
}
Community
  • 1
  • 1
  • 1
    do you have source code for `ExecWin`? – Iłya Bursov Mar 18 '16 at 16:50
  • 2
    off topic: You may be able to save yourself a lot of by-hand string manipulation and potential overflows with `std::wstring` – user4581301 Mar 18 '16 at 16:53
  • This does not answer your question (sorry) - but whenever I had to restart an app, I found the most reliable way to do so was to run a different process to do the restart. This second process can ensure the original app has closed (by waiting on its process handle) before relaunching it. This ensures no file contention by the app, no mutexes locking out a second app instance, etc. The use of ping is a *hack* that may work most of the time but not if the first instance of your app takes too long to close. The ping hack may be appropriate for what you are doing - I don't know the context. – Steve Kidd Mar 18 '16 at 17:44

1 Answers1

1

I believe the && in the command string a feature of the command prompt. ShellExecute isn't going to pay attention to that. It thinks it's just another parameter to the first command on the line (the ping).

Your options are:

  1. Spin up a cmd.exe process instead, and pass the command line to it using /C. (For details, read up on the /C option for cmd.exe.)

  2. Make your program issue the two commands separately. If you need to ensure the ping command finishes successfully before the second, you might need to use CreateProcess instead of ShellExecute. CreateProcess will give you the handles you need to wait on the process and check its exit status.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175