Note that there are some explanatory texts on larger screens.

plurals
  1. PODelphi: How to start application with elevated status and wait for it to terminate?
    primarykey
    data
    text
    <p>I'm trying to start another application from my program with elevated rights, and wait for it to terminate before continuing.</p> <p>I've tried several different solutions on the web, but I can't find one that works exactly right. </p> <p>The code below is the closest I have to working right. It runs the app with elevated privileges and waits for it to terminate, but it freezes once the external app is terminated. In other words, it doesn't keep processing once the launched app is closed.</p> <p>How can I accomplish what I'm after here?</p> <pre><code>procedure TfMain.RunFileAsAdminWait(hWnd: HWND; aFile, aParameters: string); var sei: TShellExecuteInfo; begin FillChar(sei, SizeOf(sei), 0); sei.cbSize := SizeOf(sei); sei.Wnd := hWnd; sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI; sei.lpVerb := 'runas'; sei.lpFile := PChar(aFile); sei.lpParameters := PChar(aParameters); sei.nShow := SW_SHOWNORMAL; if not ShellExecuteEx(@sei) then RaiseLastOSError else while WaitForSingleObject(sei.hProcess, 50) &lt;&gt; WAIT_OBJECT_0 do Application.ProcessMessages; CloseHandle(sei.hProcess); end; </code></pre> <p><strong>Update:</strong></p> <p>I've come up with the following function, but it only works if I have a ShowMessage statement after calling it. So, I have to have:</p> <pre><code>RunFileAsAdminWait(Handle, ExtractFilePath(Application.Exename) + 'AutoUpdate.exe', '/auto'); ShowMessage('test'); </code></pre> <p>in order to make the function work. <strong>How can I make it work without the ShowMessage call?</strong></p> <p>Here's the updated function:</p> <pre><code>procedure TfMain.RunFileAsAdminWait(hWnd: HWND; aFile, aParameters: string); var sei: TShellExecuteInfo; begin FillChar(sei, SizeOf(sei), 0); sei.cbSize := SizeOf(sei); sei.Wnd := hWnd; sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI; sei.lpVerb := 'runas'; sei.lpFile := PChar(aFile); sei.lpParameters := PChar(aParameters); sei.nShow := SW_SHOWNORMAL; if not ShellExecuteEx(@sei) then RaiseLastOSError else if sei.hProcess &lt;&gt; 0 then WaitForSingleObject(sei.hProcess, 50) else Exit; CloseHandle(sei.hProcess); end; </code></pre>
    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.
 

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