当我运行此代码时,calc.exe启动,但我无法将进程句柄分配给作业。 该过程开始,但句柄无效?
我哪里出错了?
GetLastError()的结果始终为6.
我遵循了这个教程: Inno Setup Exec() function Wait for a limited time
const
INFINITE= $FFFFFFFF;
SEE_MASK_NOASYNC= $00000100;
SEE_MASK_NOCLOSEPROCESS= $00000040;
type
TShellExecuteInfo = record
cbSize: DWORD;
fMask: Cardinal;
HWND: HWND;
lpVerb: string;
lpFile: string;
lpParameters: string;
lpDirectory: string;
nShow: Integer;
hInstApp: THandle;
lpIDList: DWORD;
lpClass: string;
hkeyClass: THandle;
dwHotKey: DWORD;
hMonitor: THandle;
hProcess: THandle;
end;
function ShellExecuteAndWait(
Filename : String;
Params : String;
WorkingDir : String;
ShowCmd : Integer
) : Integer;
var
JobObject : Thandle;
ExecInfo : TShellExecuteInfo;
ExitCode : DWORD;
begin
ExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
ExecInfo.hwnd := 0;
ExecInfo.lpVerb := 'open';
ExecInfo.lpFile := Filename;
ExecInfo.lpParameters := Params;
ExecInfo.lpDirectory := WorkingDir;
ExecInfo.nShow := ShowCmd;
ExecInfo.cbSize := SizeOf(ExecInfo);
JobObject := CreateJobObject( '' , 'WaitJob');
ShellExecuteEx(ExecInfo);
if not AssignProcessToJobObject( JobObject , ExecInfo.hProcess ) then
begin
MsgBox( 'Error during the assign. ' + 'Error code: ' + IntToStr( GetLastError() ), mbConfirmation, MB_OK); //Returns: 6 - Invalid handler
end;
WaitForSingleObject( JobObject , INFINITE );
GetExitCodeProcess( ExecInfo.hProcess , ExitCode );
Result := ExitCode;
end;
修改 更多功能原型。
function WaitForSingleObject(
hHandle : THandle;
dwMilliseconds : DWORD
) : DWORD;
external 'WaitForSingleObject@kernel32.dll stdcall';
function ShellExecuteEx(
lpExecInfo: TShellExecuteInfo
): BOOL;
external 'ShellExecuteEx{#AW}@Shell32.dll stdcall';
function AssignProcessToJobObject(
hJob : Thandle;
hProcess : Thandle
): BOOL;
external 'AssignProcessToJobObject@kernel32.dll stdcall';
function CreateJobObject(
lpSecurityAttributes,
lpName: string
): THandle;
external 'CreateJobObject{#AW}@kernel32.dll stdcall';