Skip to content

feature reques: additional pipe debug window #783

Description

@rexxitall

When one debug output is not enough ....
I am currently doing some cgi related stuff. (its not that easy to see anything when stdin stdout and stderr are redirected)
my problem is how to watch inside such a running cgi or i want to have a additional output window.
my solution is to have a small terminal program which just dump out strings send over a named pipe.

its very handy cause you also can send ansi chars and scroll for and back in the output window which can also be on a different sreen and is open al the time.

code to play with below:
regards Thomas

`
#include "windows.bi"
#include "win/winnt.bi"
#include "windows.bi"

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' PrintToConsole
' Outputs text to console using Windows Terminal's native scrolling
' Parameters:
' - textToPrint: The string to output
' - addNewLine: Whether to append a newline (default true)
' Returns: True if successful, False otherwise
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function echo _
( _
ByRef textToPrint As String, _
ByVal addNewLine As Boolean = True _
) As Boolean
textToPrint=textToPrint & "*"
' Get standard output handle
Dim hConsoleOutput As HANDLE = GetStdHandle(STD_OUTPUT_HANDLE)
If hConsoleOutput = INVALID_HANDLE_VALUE Then
Return False
End If

' Prepare the output string
Dim outputText As String = textToPrint
If addNewLine Then
    outputText += Chr(13) + Chr(10) ' CR+LF
End If

' Write to console
Dim charsWritten As DWORD
Dim result As BOOL = WriteConsole( _
    hConsoleOutput, _               ' Console handle
    StrPtr(outputText), _           ' Text to write
    Len(outputText), _              ' Text length
    @charsWritten, _                ' Bytes written
    NULL _                          ' Reserved
)

' Return success status
Return (result <> 0) AndAlso (charsWritten = Len(outputText))

End Function

Sub PipeMonitor()
Const BUFFER_SIZE As Integer = 25 * 1024
Dim pipeHandle As HANDLE
Dim pipeName As String = "\.\pipe\testpipe"
Dim success As BOOL
Dim bytesRead As DWORD
Dim messageBuffer As ZString * (BUFFER_SIZE + 1)
Dim connected As BOOL

' Security attributes for the pipe
Dim sa As SECURITY_ATTRIBUTES
sa.nLength = SizeOf(SECURITY_ATTRIBUTES)
sa.lpSecurityDescriptor = NULL
sa.bInheritHandle = True

While True
    ' Create the named pipe
    pipeHandle = CreateNamedPipe( _
        pipeName, _                         ' pipe name
        PIPE_ACCESS_INBOUND, _              ' read-only access
        PIPE_TYPE_MESSAGE Or _              ' message type pipe
        PIPE_READMODE_MESSAGE Or _          ' message-read mode
        PIPE_WAIT, _                       ' blocking mode
        1, _                               ' max. instances
        BUFFER_SIZE, _                      ' output buffer size
        BUFFER_SIZE, _                      ' input buffer size
        0, _                                ' client time-out
        @sa _                               ' security attributes
    )
    
    If pipeHandle = INVALID_HANDLE_VALUE Then
        Print "Error creating pipe: "; GetLastError()
        Sleep(1000)
        Continue While
    End If
    
    ' Wait for client to connect
    'Print "Waiting for client connection..."
    connected = ConnectNamedPipe(pipeHandle, NULL)
    
    If connected = 0 Then
        Dim lastErr As DWORD = GetLastError()
        If lastErr <> ERROR_PIPE_CONNECTED Then
            Print "Connection failed: "; lastErr
            CloseHandle(pipeHandle)
            Sleep(1000)
            Continue While
        End If
    End If
    
   ' Print "Client connected"
    
    ' Read from pipe until client disconnects
    While True
        success = ReadFile( _
            pipeHandle, _                   ' pipe handle
            @messageBuffer, _              ' buffer to receive data
            BUFFER_SIZE, _                 ' size of buffer
            @bytesRead, _                  ' number of bytes read
            NULL _                         ' not overlapped I/O
        )
        
        If success = 0 OrElse bytesRead = 0 Then
            Dim errCode As DWORD = GetLastError()
            If errCode = ERROR_BROKEN_PIPE Then
         '       Print "Client disconnected"
            Else
                Print "ReadFile failed: "; errCode
            End If
            Exit While
        End If
        
        ' Process the received message
        Dim receivedMsg As String = Left(messageBuffer, bytesRead)
        receivedMsg = Trim(receivedMsg, Any !"\0") ' Remove null chars
        
        If Len(receivedMsg) > 0 Then
            echo( receivedMsg)
        End If
    Wend
    
    ' Disconnect and close pipe
    DisconnectNamedPipe(pipeHandle)
    CloseHandle(pipeHandle)
Wend

End Sub

' Start the pipe monitor
PipeMonitor()

and here the code to send to this pipe :

Function SayPipe(message As String) As Long
Print "" & message & ""
Dim hPipe As HANDLE
Dim bytesWritten As DWORD
Dim success As BOOL
Dim pipeName As String = "\.\pipe\testpipe"

' Try to open the named pipe
hPipe = CreateFile( _
    pipeName, _                ' pipe name 
    GENERIC_WRITE, _            ' write access 
    0, _                        ' no sharing 
    NULL, _                     ' default security attributes
    OPEN_EXISTING, _            ' opens existing pipe 
    0, _                        ' default attributes 
    NULL _                      ' no template file
)

If hPipe = INVALID_HANDLE_VALUE Then
    ' Pipe not available
    Return 0
End If

' Write to the pipe
success = WriteFile( _
    hPipe, _                    ' pipe handle 
    StrPtr(message), _          ' message to send
    Len(message), _             ' message length 
    @bytesWritten, _            ' bytes written
    NULL _                     ' not overlapped
)

' Close the pipe
CloseHandle(hPipe)

If success = 0 Or bytesWritten <> Len(message) Then
    Return 0
Else
    Return 1
End If

End Function
`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions