function Console(const Command: String): String; var StartupInfo: TStartupInfo; ProcessInfo: TProcessinformation; SecurityAttr: TSecurityAttributes; OutputPipeRead,OutputPipeWrite: THandle; Res: Boolean; BufSize: Cardinal; Buffer: String; BytesRead: Cardinal; begin FillChar(ProcessInfo,SizeOf(TProcessinformation),0); FillChar(SecurityAttr,SizeOf(TSecurityAttributes),0); SecurityAttr.nLength := SizeOf(SecurityAttr); SecurityAttr.binheritHandle := true; SecurityAttr.lpSecurityDescriptor := nil; CreatePipe(OutputPipeRead,OutputPipeWrite,@SecurityAttr,0); FillChar(StartupInfo,SizeOf(TStartupInfo),0); StartupInfo.cb:=SizeOf(StartupInfo); StartupInfo.hStdinput := 0; StartupInfo.hStdOutput := OutputPipeWrite; StartupInfo.hStdError := OutputPipeWrite; StartupInfo.wShowWindow := SW_HIDE; StartupInfo.DWFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES; Res := CreateProcess(nil,PChar(command),nil,true,CREATE_DEFAulT_ERROR_MODE or CREATE_NEW_CONSolE or norMAL_PRIORITY_CLASS,StartupInfo,ProcessInfo); CloseHandle(OutputPipeWrite); Result := ''; if Res then begin SetLength(Buffer,5000); BufSize := Length(Buffer); repeat Res := Readfile(OutputPipeRead,Buffer[1],BufSize,BytesRead,nil); Result := Result + copy(Buffer,1,BytesRead); until not Res; WaitForSingleObject(ProcessInfo.hProcess,INFINITE); CloseHandle(ProcessInfo.hProcess); end; CloseHandle(OutputPipeRead); end;
样品用法:
ShowMessage(Console('cmd /c dir C:\'));总结
以上是内存溢出为你收集整理的delphi – 有没有办法直接运行“dir”?全部内容,希望文章能够帮你解决delphi – 有没有办法直接运行“dir”?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)