user3356861
user3356861

Reputation: 1

Application restart itself

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;
}

Upvotes: 0

Views: 1260

Answers (1)

Adrian McCarthy
Adrian McCarthy

Reputation: 47962

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.

Upvotes: 1

Related Questions