Capture Console Output

Started by Paul Squires, March 05, 2012, 09:18:34 PM

Previous topic - Next topic

Pedro Marquez

#15
Me doy cuenta que si a.txt es de gran tamaño, el programa queda bloqueado.
I realize that if a.txt is large, the program is blocked.



Function ShellConsoleApp( CmdLine As String, _
                          StartDirectory As Asciiz, _
                          Result As String, _
                          ByVal dwMillisecondsWait As Dword _
                          ) As Long
                         
      '**** Set security attributes ****
      Local sa As SECURITY_ATTRIBUTES
      sa.nLength = SizeOf(SECURITY_ATTRIBUTES)
      sa.bInheritHandle = 1 ' TRUE

       '**** Create a new pipe ****
      Local hReadPipe As Dword, hWritePipe As Dword
      If CreatePipe(hReadPipe, hWritePipe, sa, ByVal 0) = 0 Then _
         Function = -2: Exit Function

      '**** Start the program ****
      Local SI As STARTUPINFO, pi As PROCESS_INFORMATION, ExitCode As Long
      SI.cb           = SizeOf(STARTUPINFO)
      SI.dwFlags      = %STARTF_USESHOWWINDOW Or %STARTF_USESTDHANDLES
      SI.wShowWindow  = %SW_HIDE
      SI.hStdOutput   = hWritePipe
      SI.hStdError    = hWritePipe

      If CreateProcess ("", ByVal StrPtr(CmdLine), ByVal 0&, ByVal 0&, 1,  %NORMAL_PRIORITY_CLASS, _  ' // creation flags
         ByVal 0&, StartDirectory, si, pi) = 0 Then Function = -1: Exit Function
      WaitForSingleObject PI.hProcess, dwMillisecondsWait
      GetExitCodeProcess PI.hProcess, ExitCode
      If ExitCode = %STILL_ACTIVE Then TerminateProcess pi.hProcess, 0: Function = -3 Else Function = ExitCode

      '**** Read the pipe anyway ****
      Local BytesRead As Dword, BytesWritten As Dword
      Local chBuf As Asciiz * 1024, chEOF As Asciiz * 12

      chEOF = "End Of Pipe" ' Something unique
      Result = ""
      Do
         If WriteFile (hWritePipe, chEOF, Len(chEOF), BytesWritten, ByVal 0) = 0 Then Function = -4: Exit Do
         If BytesWritten <> Len(chEOF) Then Function = -4: Exit Do
         Do
            If ReadFile(hReadPipe, chBuf, SizeOf(chBuf), BytesRead, ByVal 0) = 0 Then Function = -4: Exit Do
            If BytesRead = 0 Then Function = -4: Exit Do
            Result = Result + Left$(chBuf, BytesRead)
            If Right$(Result, Len(chEOF)) = chEOF Then Result$ = Left$(Result$, Len(Result) - Len(chEOF)): Exit Do
         Loop
         Exit Do
      Loop
      If Len(Result) Then OemToCharBuff ByVal StrPtr(Result), ByVal StrPtr(Result), Len(Result)

      '*** Close process and pipe handles ****
      CloseHandle hReadPipe
      CloseHandle hWritePipe
      CloseHandle pi.hThread
      CloseHandle pi.hProcess
     
End Function



button blocked ----- a.txt 17kb
button Command2 ---- b.txt 1kb




Elias Montoya


What is strange to me is that the line hangs at this line:
Lo que me parece extraño es que el codigo se detiene en esta linea:

If WriteFile (hWritePipe, chEOF, Len(chEOF), BytesWritten, ByVal 0) = 0 Then Function = -4: Exit Do

Para entonces yo aun no veo ninguna diferencia de importancia en la ejecucion de codigo. Yo diria que algo le esta pasando al handle del pipe, pero no estoy seguro.
By then i still can't find anything different in code execution. I would say something is happening to the pipe handle, but im not sure.
Win7, iMac x64 Retina display 5K, i7-5820K 4.4 ghz, 32GB RAM, All updates applied. - Firefly 3.70.

Wilko Verweij

Hi,
Are you sure you are using the right declarations? I have been struggling with this in the past and remember the only way I could get it working was to declare the string as a byte.

  Local TB As Byte
  Local ToWrite$
 
  ToWrite$="abcdefgh"
                                             
  For I=1 To Len(ToWrite$)
    TB=Asc(Mid$(ToWrite$,I,1))
    RetValL=WriteFile(hPipeWrite,TB,1,Written,ByVal 0)
  Next I


See: http://www.powerbasic.com/support/pbforums/showthread.php?t=40600

I only have to write a small string, so I now write them byte by byte (which is clumsy, but it is not a critical part of my program...).

Maybe this helps.
Wilko

Pedro Marquez

#18
 upload the chBuf but same problem
Local chBuf As Asciiz * 2048


master or creator have solution

Pedro Marquez

with 5kb of data stops working.

Pedro Marquez

¿TechSupport can have a solution?

David Kenny

Pedro,

Firefly is probably not the problem.  I would recommend creating a minimal Powerbasic-only version.  I think you will find the problem still exists.  If it turns out that the problem still exists, your best bet is to post your problem on the PB forums.  There will be many more people that have experience with Console output there.  They are just as eager to help as the forum members here.   

David

Pedro Marquez


Roger Garstang

Interesting.  I wrote a program using the exact code Paul used as reference a few months back.  I needed an easy way to call the Android ADB service and make it forward a TCP port over USB for communication with an android app wrote in Java with Eclipse.  Unfortunately it wasn't as automated as I wanted since ADB doesn't use any return codes when ran and all you have is the text to go by, but it works...and was wrote in FF/PB.

Pedro Marquez

you can convert ShellConsoleApp to FreeBasic