1,183
社区成员
发帖
与我相关
我的任务
分享
function IsWindowsNT: BOOL;
var
osv : OSVERSIONINFO;
begin
osv.dwOSVersionInfoSize := SizeOf(osv);
GetVersionEx(osv);
if osv.dwPlatformId = VER_PLATFORM_WIN32_NT then
begin
Result := True;
end
else
begin
Result := False;
end;
end;
procedure TfrmBMain.ExecuteBack(const CommandLine : string);
var
sa : TSecurityAttributes;
sd : SECURITY_DESCRIPTOR;
lpsa : PSecurityAttributes;
hReadPipe : THandle;
hWritePipe : THandle;
si : TStartupInfo;
pi : TProcessInformation;
dest : array [0..100] of Char;
BytesRead : DWORD;
strString : string;
begin
lpsa := nil;
try
if (IsWindowsNT) then
begin
InitializeSecurityDescriptor(@sd,SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@sd,True,nil,False);
sa.nLength := SizeOf(SECURITY_ATTRIBUTES);
sa.bInheritHandle := True;
sa.lpSecurityDescriptor := @sd;
lpsa := @sa;
end;
Assert(CreatePipe(hReadPipe,hWritePipe,lpsa,2500000));
GetStartupInfo(si);
si.cb := SizeOf(TStartupInfo);
si.dwFlags := STARTF_USESHOWWINDOW + STARTF_USESTDHANDLES;
si.wShowWindow := SW_HIDE;
si.hStdOutput := hWritePipe;
si.hStdError := hWritePipe;
CreateProcess(
nil,
PChar(CommandLine),
nil, nil, True,
0, nil, nil,
si, pi
);
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess,0);
while True do
begin
if not PeekNamedPipe(hReadPipe,@dest,SizeOf(dest),nil,nil,nil) then Break;
if ReadFile(hReadPipe, dest, SizeOf(dest), BytesRead, nil) = True then
begin
strString := Copy(string(dest),1,BytesRead);
ShowMessage(strString);
if Length(strString) < SizeOf(dest) then Break;
end;
Sleep(200);
Application.ProcessMessages;
end;
CloseHandle(hWritePipe);
CloseHandle(hReadPipe);
Closehandle(pi.hProcess);
except
;
end;
end;