两个方法
function RunDosCommand(Command: string): string; var hReadPipe: THandle; hWritePipe: THandle; SI: TStartUpInfo; PI: TProcessInformation; SA: TSecurityAttributes; BytesRead: DWORD; Dest: array[0..1023] of AnsiChar; CmdLine: array[0..512] of char; TmpList: TStringList; Avail, ExitCode, wrResult: DWORD; osVer: TOSVERSIONINFO; tmpstr: AnsiString; begin osVer.dwOSVersionInfoSize := Sizeof(TOSVERSIONINFO); GetVersionEX(osVer); if osVer.dwPlatformId = VER_PLATFORM_WIN32_NT then begin SA.nLength := SizeOf(SA); SA.lpSecurityDescriptor := nil; //@SD; SA.bInheritHandle := True; CreatePipe(hReadPipe, hWritePipe, @SA, 0); end else CreatePipe(hReadPipe, hWritePipe, nil, 1024); try FillChar(SI, SizeOf(SI), 0); SI.cb := SizeOf(TStartUpInfo); SI.wShowWindow := SW_HIDE; SI.dwFlags := STARTF_USESHOWWINDOW; SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES; SI.hStdOutput := hWritePipe; SI.hStdError := hWritePipe; StrPCopy(CmdLine, Command); if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then begin ExitCode := 0; while ExitCode = 0 do begin wrResult := WaitForSingleObject(PI.hProcess, 500); if PeekNamedPipe(hReadPipe, @Dest[0], 1024, @Avail, nil, nil) then begin if Avail > 0 then begin TmpList := TStringList.Create; try FillChar(Dest, SizeOf(Dest), 0); ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil); TmpStr := Copy(Dest, 0, BytesRead - 1); TmpList.Text := TmpStr; Result := string(tmpstr); finally TmpList.Free; end; end; end; if wrResult <> WAIT_TIMEOUT then ExitCode := 1; end; GetExitCodeProcess(PI.hProcess, ExitCode); CloseHandle(PI.hProcess); CloseHandle(PI.hThread); end; finally CloseHandle(hReadPipe); CloseHandle(hWritePipe); end; end; function GetURLResult(url: string): string; begin Result := RunDosCommand(Format('http.exe "%s"', [url])); if Result.ToLower.StartsWith('error') then Exit('error'); Result := Copy(Result, 1, Result.IndexOf('}') + 1); end;
调用方式:
Edit1.Text := GetURLResult('http://0.0.0.0/test/getResult/94bdb076dcdb8f6bab77321ece18f8af');