How to wait for ShellExecute to run?

There is a CodeProject article that shows how, by using ShellExecuteEx instead of ShellExecute: SHELLEXECUTEINFO ShExecInfo = {0}; ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = NULL; ShExecInfo.lpFile = “c:\\MyProgram.exe”; ShExecInfo.lpParameters = “”; ShExecInfo.lpDirectory = NULL; ShExecInfo.nShow = SW_SHOW; ShExecInfo.hInstApp = NULL; ShellExecuteEx(&ShExecInfo); WaitForSingleObject(ShExecInfo.hProcess, INFINITE); CloseHandle(ShExecInfo.hProcess); The crucial point is the flag … Read more

OSX equivalent of ShellExecute?

You can call system(); in any C++ application. On OSX, you can use the open command to launch things as if they were clicked on. From the documentation for open: The open command opens a file (or a directory or URL), just as if you had double-clicked the file’s icon. If no application name is … Read more

How to shell to another app and have it appear in a delphi form

All error checking omitted, but this should get you started: procedure TForm1.Button1Click(Sender: TObject); var Rec: TShellExecuteInfo; const AVerb = ‘open’; AParams=””; AFileName=”Notepad.exe”; ADir=””; begin FillChar(Rec, SizeOf(Rec), #0); Rec.cbSize := SizeOf(Rec); Rec.fMask := SEE_MASK_NOCLOSEPROCESS; Rec.lpVerb := PChar( AVerb ); Rec.lpFile := PChar( AfileName ); Rec.lpParameters := PChar( AParams ); Rec.lpDirectory := PChar( Adir ); Rec.nShow := … Read more

How do I run a command-line program in Delphi?

An example using ShellExecute(): procedure TForm1.Button1Click(Sender: TObject); begin ShellExecute(0, nil, ‘cmd.exe’, ‘/C find “320” in.txt > out.txt’, nil, SW_HIDE); Sleep(1000); Memo1.Lines.LoadFromFile(‘out.txt’); end; Note that using CreateProcess() instead of ShellExecute() allows for much better control of the process. Ideally you would also call this in a secondary thread, and call WaitForSingleObject() on the process handle to … Read more

How to run application which requires admin rights from one that doesn’t have them [closed]

Real problem: (from Wikipedia: http://en.wikipedia.org/wiki/User_Account_Control) An executable that is marked as “requireAdministrator” in its manifest cannot be started from a non-elevated process using CreateProcess(). Instead, ERROR_ELEVATION_REQUIRED will be returned. ShellExecute() or ShellExecuteEx() must be used instead. (BTW, ERROR_ELEVATION_REQUIRED error == 740) Solution: (same site) In a native Win32 application the same “runas” verb can be … Read more

How can I run a child process that requires elevation and wait?

Use ShellExecuteEx, rather than ShellExecute. This function will provide a handle for the created process, which you can use to call WaitForSingleObject on that handle to block until that process terminates. Finally, just call CloseHandle on the process handle to close it. Sample code (most of the error checking is omitted for clarity and brevity): … Read more