Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STDIO_BUFFER and CRT File Descriptor Mode Flags? #37

Closed
GCuser99 opened this issue Feb 22, 2025 · 5 comments
Closed

STDIO_BUFFER and CRT File Descriptor Mode Flags? #37

GCuser99 opened this issue Feb 22, 2025 · 5 comments

Comments

@GCuser99
Copy link

Hi Jon,

Not a bug and not sure if it is even an issue... I was implementing named pipes for an app using the following UDT and enum (which work). I always like to normalize api declares against WinDevLib but I couldn't find STDIO_BUFFER type and the CRTFileDescriptorModeFlags enum. Do these look familiar to you? I found the CRT file descriptor mode flags from here. Wondering if these exist in WinDevLib under different names?

'packing alignment set to 1 because crt_flags and os_handle must reside in contiguous memory
[PackingAlignment(1)]
Private Type STDIO_BUFFER
    number_of_fds As Long
    crt_flags(0 To 4) As Byte
    os_handle(0 To 4) As LongPtr
End Type

Private Enum CRTFileDescriptorModeFlags
    FOPEN = &H00000001
    FEOFLAG = &H00000002
    FCRLF = &H00000004
    FPIPE = &H00000008
    FNOINHERIT = &H00000010
    FAPPEND = &H00000020
    FDEV = &H00000040
    FTEXT = &H00000080
End Enum
@fafalone
Copy link
Owner

I'm not seeing those used with any Windows API or defined in the Windows SDK... WDL only covers those; not C++ runtime stuff... could you maybe show better where they're going to to a WinaAPI? The named piped apis and flags are certainly defined as they appear in that file though.

@GCuser99
Copy link
Author

Ok no big deal... here is how STDIO_BUFFER is being used:

        Dim proc As PROCESS_INFORMATION
        Dim start As STARTUPINFO
        Dim sa As SECURITY_ATTRIBUTES
        Dim pipes As STDIO_BUFFER
        Dim result As BOOL
 
       'First we create all 4 pipes
       'We start with stdout of the edge process
       'This pipe is used for stderr, too
        sa.nLength = Len(sa)
        sa.bInheritHandle = 1&
        sa.lpSecurityDescriptor = 0&
        result = CreatePipe(hStdOutRd, hStdOutWr, sa, 0)
        If result = CFALSE Then
            Init = -2
            Exit Function
        End If
    
       'Then stdin
        result = CreatePipe(hStdInRd, hStdInWr, sa, 0)
        If result = CFALSE Then
            Init = -2
            Exit Function
        End If
    
       'Then the out pipe
        result = CreatePipe(hCDPOutRd, hCDPOutWr, sa, 2 ^ 20)
        If result = CFALSE Then
            Init = -2
            Exit Function
        End If
    
       'And finally the in pipe
        result = CreatePipe(hCDPInRd, hCDPInWr, sa, 0)
        If result = CFALSE Then
            Init = -2
            Exit Function
        End If
    
       'Then we fill the special structure for passing arbitrary pipes (i.e. fds) to a process
        pipes.number_of_fds = 5
        pipes.os_handle(0) = hStdInRd
        pipes.os_handle(1) = hStdOutWr
        pipes.os_handle(2) = hStdOutWr
        pipes.os_handle(3) = hCDPInRd
        pipes.os_handle(4) = hCDPOutWr
        pipes.crt_flags(0) = FOPEN Or FPIPE
        pipes.crt_flags(1) = FOPEN Or FPIPE
        pipes.crt_flags(2) = FOPEN Or FPIPE
        pipes.crt_flags(3) = FOPEN Or FPIPE
        pipes.crt_flags(4) = FOPEN Or FPIPE
        
       'Define new instance parameters and start it
        With start
            .cb = Len(start)
            .dwFlags = STARTF_USESHOWWINDOW
            .hStdOutput = hStdOutWr
            .hStdInput = hStdInRd
            .hStdError = hStdOutWr
            .wShowWindow = SW_SHOWMINIMIZED 'minimize the window at start
            .cbReserved2 = Len(pipes) '<------------------- STDIO_BUFFER use here
            .lpReserved2 = VarPtr(pipes) '<------------------- STDIO_BUFFER use here
        End With
    
        result = CreateProcess (0&, StrPtr(strExec), sa, sa, 1&, NORMAL_PRIORITY_CLASS_FLAG, ByVal 0&, 0&, start, proc)

@fafalone
Copy link
Owner

fafalone commented Feb 23, 2025

Definitely seems like some kind of undocumented use I'm not familiar with;

cbReserved2

Reserved for use by the C Run-time; must be zero.

lpReserved2

Reserved for use by the C Run-time; must be NULL.

But they don't conflict with anything else so I'll add your definitions as-is.

PS- Be very careful using Len instead of LenB, that's likely to bite you very quickly in 64bit and eventually in 32bit. In fact have you tried that code on 64bit because Len and LenB differ for STARTUPINFO under x64.

@GCuser99
Copy link
Author

Awesome thanks for adding this one Jon!

Also, thanks for the heads up on use of Len vs LenB!

@fafalone
Copy link
Owner

These are now available with v8.7.500.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants