VISWESWARAN NAGASIVAM
VISWESWARAN NAGASIVAM

Reputation: 352

How to check the completion of process

I have written a C++ program which fixes the attributes using ShellExecute Function and displays the result inn the list box

  int USB::FixAttributes(wchar_t buffer[10])
{
    PWSTR show = L"/c attrib -s -r -h *.* /s /d /l ";
    ShellExecute(NULL, L"open", L"cmd.exe", show, buffer, SW_HIDE);
    wchar_t* no1 = L"Attributes has been fixed successfully";
    SendMessage(list1, LB_ADDSTRING, NULL, (LPARAM)no1);//add message    to list box
    return 0;
}

buffer is nothing but an drive string e.g H:\

But the message("Attributes has been fixed successfully") has been added to the list box before the completion of the ShellExecute function thereby it gives false information how to solve it!

Thank you

Upvotes: 0

Views: 165

Answers (1)

David Heffernan
David Heffernan

Reputation: 613572

You are using the wrong function to create a new process. ShellExecute does not offer a way to wait for termination. Your options are:

  1. ShellExecuteEx. Specify the SEE_MASK_NOCLOSEPROCESS to obtain a process handle. Wait on it with WaitForSingleObject. When that wait completes the child process has terminated.
  2. CreateProcess. This always yields a process handle on which you can wait in the same way.

In both cases you must close the handles to tidy up and avoid leaking. CreateProcess is the preferred choice for creating a process. It gives you more freedom. You can avoid showing a console window if you wish, for instance.

Having said all of that, you might be better off doing all the work in your process rather than creating a child process. That would allow you to report progress, richer error messages, and so on.

Upvotes: 1

Related Questions