Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First thing that should be clear is that Win32 API calls that are related to windows/messages/focus and etc. <strong>do not</strong> depend on timing. Every thread has its own window/messaging subsystem, there's no race conditions here.</p> <p>What you describe is something else. You actually launch another process (application), which runs concurrently with yours. Note that <code>ShellExecute</code> is an <strong>asynchronous</strong> function. It returns immediately after creating the process, and from now on your application and the process you've created run concurrently.</p> <p>Now, since only one window in the system may have a focus at a time - it's very likely that the process you've created just steals the focus from you. In order to avoid this - you should first wait for that process to finish working, and only then restore the focus to your window and continue working.</p> <p>For this you have to obtain the handle of the created process, and call a Win32 waiting function on it. <code>ShellExecute</code> doesn't return you the handle of the created process. However <code>ShellExecuteEx</code> - does. BTW it also allows you to launch a process with instruction for it not to show the UI, if this is what you want.</p> <p>You should write it like this:</p> <pre><code>SHELLEXECUTEINFO sei; memset(&amp;sei, 0, sizeof(sei)); sei.cbSize = sizeof(sei); sei.fMask = SEE_MASK_NOCLOSEPROCESS; sei.lpFile = L"notepad.exe"; sei.nShow = SW_SHOWNORMAL; // or SW_HIDE if you don't want to show it if (ShellExecuteEx(&amp;sei)) { // wait for completion WaitForSingleObject(sei.hProcess, INFINITE); CloseHandle(sei.hProcess); } </code></pre> <p>This should be helpful</p> <p>P.S. Of course you should close the handle of the created process. That is, <code>CloseHandle</code> must be called after <code>WaitForSingleObject</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload