是否有可能得到的输出Exec
“编辑可执行?
我想显示用户的信息查询页面,但显示的MAC地址的输入框中的默认值。 是否有任何其他的方式来实现这一目标?
是否有可能得到的输出Exec
“编辑可执行?
我想显示用户的信息查询页面,但显示的MAC地址的输入框中的默认值。 是否有任何其他的方式来实现这一目标?
是的,使用标准输出到文件重定向:
[Code]
function NextButtonClick(CurPage: Integer): Boolean;
var
TmpFileName, ExecStdout: string;
ResultCode: integer;
begin
if CurPage = wpWelcome then begin
TmpFileName := ExpandConstant('{tmp}') + '\ipconfig_results.txt';
Exec('cmd.exe', '/C ipconfig /ALL > "' + TmpFileName + '"', '', SW_HIDE,
ewWaitUntilTerminated, ResultCode);
if LoadStringFromFile(TmpFileName, ExecStdout) then begin
MsgBox(ExecStdout, mbInformation, MB_OK);
{ do something with contents of file... }
end;
DeleteFile(TmpFileName);
end;
Result := True;
end;
请注意,可能有多个网络适配器,因此一些MAC地址选择。
我必须做同样的(执行命令行调用并得到结果),并与更广泛的解决方案上来。
如果引用的路径是通过使用在实际调用中使用它还修正错误怪/S
标志cmd.exe
。
{ Exec with output stored in result. }
{ ResultString will only be altered if True is returned. }
function ExecWithResult(const Filename, Params, WorkingDir: String; const ShowCmd: Integer;
const Wait: TExecWait; var ResultCode: Integer; var ResultString: String): Boolean;
var
TempFilename: String;
Command: String;
begin
TempFilename := ExpandConstant('{tmp}\~execwithresult.txt');
{ Exec via cmd and redirect output to file. Must use special string-behavior to work. }
Command :=
Format('"%s" /S /C ""%s" %s > "%s""', [
ExpandConstant('{cmd}'), Filename, Params, TempFilename]);
Result := Exec(ExpandConstant('{cmd}'), Command, WorkingDir, ShowCmd, Wait, ResultCode);
if not Result then
Exit;
LoadStringFromFile(TempFilename, ResultString); { Cannot fail }
DeleteFile(TempFilename);
{ Remove new-line at the end }
if (Length(ResultString) >= 2) and (ResultString[Length(ResultString) - 1] = #13) and
(ResultString[Length(ResultString)] = #10) then
Delete(ResultString, Length(ResultString) - 1, 2);
end;
用法:
Success :=
ExecWithResult('ipconfig', '/all', '', SW_HIDE, ewWaitUntilTerminated,
ResultCode, ExecStdout) or
(ResultCode <> 0);
该结果也可以加载到TStringList
对象来获取所有行:
Lines := TStringList.Create;
Lines.Text := ExecStdout;
{ ... some code ... }
Lines.Free;