In this article, you can run the console program in Delphi and display the console program output in the Memo control. In the work, you need to manually compile the j2's program and start to write a batch processing program. However, it feels very complicated to use. Therefore, you want to use Delphi as an integrated compilation tool, however, all java compilation tools are console programs. How can I capture the output of the console program and display it in Memo? I checked some information on the Internet and tested it repeatedly, I found an implementation method and hope to help you: Procedure TMainForm. RunDosInMemo (const DosApp: string; AMemo: TMemo ); Const {Set the ReadBuffer size} ReadBuffer = 2400; Var Security: TSecurityAttributes; ReadPipe, WritePipe: THandle; Start: TStartUpInfo; ProcessInfo: TProcessInformation; Buffer: PChar; BytesRead: DWord; Buf: string; Begin With Security do Begin Nlength: = SizeOf (TSecurityAttributes ); Binherithandle: = true; Lpsecuritydescriptor: = nil; End; {Create a named pipeline to capture the output of the console program} If Createpipe (ReadPipe, WritePipe, @ Security, 0) then Begin Buffer: = AllocMem (ReadBuffer + 1 ); FillChar (Start, Sizeof (Start), #0) {Set the startup properties of the console program} With start do Begin Cb: = SizeOf (start ); Start. lpReserved: = nil; LpDesktop: = nil; LpTitle: = nil; DwX: = 0; DwY: = 0; DwXSize: = 0; DwYSize: = 0; DwXCountChars: = 0; DwYCountChars: = 0; DwFillAttribute: = 0; CbReserved2: = 0; LpReserved2: = nil; HStdOutput: = WritePipe; // directs the output to the WritePipe we have created. HStdInput: = ReadPipe; // The input is directed to the ReadPipe we created. HStdError: = WritePipe; // directs the error output to the WritePipe we have created. DwFlags: = STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW; WShowWindow: = SW_HIDE; // set the window to hide. End; Try {Create a sub-process and run the console program} If CreateProcess (nil, PChar (DosApp), @ Security, @ Security, true, NORMAL_PRIORITY_CLASS, Nil, nil, start, ProcessInfo) then Begin {Wait for the process to finish running} WaitForSingleObject (ProcessInfo. hProcess, INFINITE ); {Close the output... does not close it at the beginning. If the result is not output, the program will die .} CloseHandle (WritePipe ); Buf: = \'\'; {Read the output of the console program} Repeat Bytesread: = 0; Readfile (readpipe, buffer [0], readbuffer, bytesread, nil ); Buffer [bytesread]: = #0; Oemtoansi (buffer, buffer ); Buf: = BUF + String (buffer ); Until (bytesread <readbuffer ); Senddebug (BUF ); {Split by linefeed and displayed in memo} While pos (#10, Buf)> 0 do Begin AMemo. Lines. Add (Copy (Buf, 1, pos (#10, Buf)-1 )); Delete (Buf, 1, pos (#10, Buf )); End; End; Finally FreeMem (Buffer ); CloseHandle (ProcessInfo. hProcess ); CloseHandle (ProcessInfo. hThread ); CloseHandle (ReadPipe ); End; End; End; |