我想和你分享解决方案
CreateProcess
(多亏了@DavidHeffernan)。
ShellExecute
不适合生成新进程,因为该进程是从从主应用程序加载的DLL生成的,并且
需要一个
HWND
.
function RunProcess(ACommand: string): boolean;
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
UniqueString(ACommand); // CreateProcess requires writeable string for lpCommandLine arg
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
StartupInfo.wShowWindow := SW_HIDE;
Result := CreateProcess(nil, PChar(ACommand), nil, nil, False, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);
if Result then
begin
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;
// ...
begin
if not(RunProcess('MyConsoleApp.exe')) then
raise Exception.Create('RunProcess failed');
end.