QB64.org Forum

Active Forums => Programs => Topic started by: SpriggsySpriggs on February 10, 2021, 11:02:46 pm

Title: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: SpriggsySpriggs on February 10, 2021, 11:02:46 pm
This is not the post you are looking for. CLICK the "Best Answer" post above ^^^!
Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: NOVARSEG on February 11, 2021, 12:01:50 am
there is the readfile API!

 Function ReadFile% (ByVal hFile As Long, Byval lpBuffer As _Offset, Byval nNumberOfBytesToRead As Long, Byval lpNumberOfBytesRead As _Offset, Byval lpOverlapped As _Offset)


does pipe need writefile too?

in kernel 32?

where is kernel32 file?

Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: SMcNeill on February 11, 2021, 12:03:23 am
@SpriggsySpriggs — what you’re running into is Data Alignment/Packing.  Here’s a pretty good write up on what’s going on, down at Chapter 17: https://www.viva64.com/en/a/0004/#ID1C7DC5438C



Processors work more efficiently when they deal with data aligned properly. As a rule the 32-bit data item must be aligned at the border multiple of 4 bytes, and the 64-bit item at the border multiple of 8 bytes. An attempt to work with unaligned data on IA-64 (Itanium) processors will cause an exception as shown in the following example,.

#pragma pack (1) // Also set by key /Zp in MSVC
struct AlignSample {
  unsigned size;
  void *pointer;
} object;
void foo(void *p) {
  object.pointer = p; // Alignment fault
}
If you have to work with unaligned data on Itanium you should tell this to the compiler. For example, you may use a special macro UNALIGNED:

#pragma pack (1) // Also set by key /Zp in MSVC
struct AlignSample {
  unsigned size;
  void *pointer;
} object;
void foo(void *p) {
  *(UNALIGNED void *)&object.pointer = p; //Very slow
}
This solution is not efficient, because the access to the unaligned data will be several times slower. A better result may be achieved if you arrange up to 32-bit, 16-bit and 8-bit items in 64-bit data items.

On the x64 architecture during the access to unaligned data, an exception does not occur, but you should avoid them also. Firstly, because of the essential slowdown of the access to this data, and secondly, because of a high probability of porting the program on the IA-64 platform in the future.



Basically, you align your data on 8-byte breakpoints, rather than 4. 
The only point where I get lost in these conversions is how we determine those breakpoints.

dx AS LONG
dy AS LONG
dwSize AS LONG

Now, with the above, would we have EVERY long padded to 8 bytes?  Or would the first two variables (dx and dy) be counted together as 8-bytes, and fit the breakpoint?  Or do we padd the first and join the last two??

I *still* haven’t sat down and sorted out where the BLEEP we’re required to have those 8-byte breakpoints in data types.  All I can do is sit down in front of a keyboard, and furiously attempt to change first one and then the other, until I finally find a combo that works.
Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: SpriggsySpriggs on February 11, 2021, 12:04:12 am
@NOVARSEG ,

Please do not pursue the ReadFile API function. If you do, make a new thread.
Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: SpriggsySpriggs on February 11, 2021, 12:07:18 am
@SMcNeill
@SpriggsySpriggs — what you’re running into is Data Alignment/Packing

I *still* haven’t sat down and sorted out where the BLEEP we’re required to have those 8-byte breakpoints in data types.  All I can do is sit down in front of a keyboard, and furiously attempt to change first one and then the other, until I finally find a combo that works.

Yep! I am aware of the alignment thing in Windows and I have struggled with it many times. It pisses me off to no end. The fact I'm only 8 bytes short in STARTUPINFO pisses me off even more.
Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: SMcNeill on February 11, 2021, 12:09:21 am
Cb as LONG
padding as LONG
.....


AND 4 more bytes padding near the end, would be my guess then.
Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: SMcNeill on February 11, 2021, 12:13:11 am
    Type STARTUPINFO
        cb As Long — 4 bytes + 4 bytes padding

        lpReserved As _Offset —8
        lpDesktop As _Offset — 8
        lpTitle As _Offset — 8
        dwX As Long +     dwY As Long — 8
        dwXSize As Long +   dwYSize As Long — 8
        dwXCountChars As Long + dwYCountChars As Long
        dwFillAttribute As Long +  dwFlags As Long

        wShowWindow As Integer + cbReserved2 As Integer = 4...  Probably need 4 padding

        lpReserved2 As _Offset
        hStdInput As Long +  hStdOutput As Long


        hStdError As Long + 4 padding


    End Type

I’d take a guess based off the above, since you’re only looking for 8-bytes total.
Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: NOVARSEG on February 11, 2021, 12:14:09 am
OK from my experience in assembler, I have encountered alignment problems as well.  The most hideous alignment problem is when an assembler instruction is say 32 bits and you try to load 16 bit data into the instruction.

The same thing happens from 32 bit to 64 bit
Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: NOVARSEG on February 11, 2021, 12:43:53 am
12 longs = 48 bytes
2 integers = 4 bytes
 total = 52 bytes

52 / 8 = 6.5

52  / 4 =  13

change integers to longs
wild  guess

Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: SpriggsySpriggs on February 11, 2021, 01:12:30 am
Still no good on Windows x64. However, the below code works fine in Linux:

Code: QB64: [Select]
  1.  
  2. Print pipecom("ls")
  3.  
  4. Function pipecom$ (cmd As String)
  5.         Function popen%& (cmd As String, readtype As String)
  6.         Function feof& (ByVal stream As _Offset)
  7.         Function fgets$ (str As String, Byval n As Long, Byval stream As _Offset)
  8.         Function pclose& (ByVal stream As _Offset)
  9.     End Declare
  10.  
  11.     Dim As String pipecom_buffer
  12.     Dim As _Offset stream
  13.  
  14.     Dim buffer As String * 4096
  15.     stream = popen(cmd, "r")
  16.     If stream Then
  17.         While feof(stream) = 0
  18.             If fgets(buffer, 4096, stream) <> "" And feof(stream) = 0 Then
  19.                 pipecom_buffer = pipecom_buffer + Mid$(buffer, 1, InStr(buffer, Chr$(0)))
  20.             End If
  21.         Wend
  22.         Dim a As Long
  23.         a = pclose(stream)
  24.     End If
  25.     pipecom = pipecom_buffer

And this code works in 32 bit Windows:
Code: QB64: [Select]
  1.  
  2. Print pipecom("dir")
  3.  
  4. Function pipecom$ (cmd As String)
  5.     Type SECURITY_ATTRIBUTES
  6.         nLength As Long
  7.         lpSecurityDescriptor As _Offset
  8.         bInheritHandle As Long
  9.     End Type
  10.  
  11.     Type STARTUPINFO
  12.         cb As Long
  13.         lpReserved As _Offset
  14.         lpDesktop As _Offset
  15.         lpTitle As _Offset
  16.         dwX As Long
  17.         dwY As Long
  18.         dwXSize As Long
  19.         dwYSize As Long
  20.         dwXCountChars As Long
  21.         dwYCountChars As Long
  22.         dwFillAttribute As Long
  23.         dwFlags As Long
  24.         wShowWindow As Integer
  25.         cbReserved2 As Integer
  26.         lpReserved2 As _Offset
  27.         hStdInput As Long
  28.         hStdOutput As Long
  29.         hStdError As Long
  30.     End Type
  31.  
  32.     Type PROCESS_INFORMATION
  33.         hProcess As Long
  34.         hThread As Long
  35.         dwProcessId As Long
  36.         dwThreadId As Long
  37.     End Type
  38.  
  39.     Const TRUE = 1
  40.     Const FALSE = 0
  41.     Const STARTF_USESTDHANDLES = &H00000100
  42.     Const CREATE_NO_WINDOW = &H8000000
  43.  
  44.     Declare Dynamic Library "Kernel32"
  45.         Function CreatePipe% (ByVal hReadPipe As _Offset, Byval hWritePipe As _Offset, Byval lpPipeAttributes As _Offset, Byval nSize As Long)
  46.         Function CreateProcess% Alias CreateProcessA (ByVal lpApplicationName As _Offset, Byval lpCommandLine As _Offset, Byval lpProcessAttributes As _Offset, Byval lpThreadAttributes As _Offset, Byval bInheritHandles As Integer, Byval dwCreationFlags As Long, Byval lpEnvironment As _Offset, Byval lpCurrentDirectory As _Offset, Byval lpStartupInfor As _Offset, Byval lpProcessInformation As _Offset)
  47.         Function CloseHandle% (ByVal hObject As Long)
  48.         Function ReadFile% (ByVal hFile As Long, Byval lpBuffer As _Offset, Byval nNumberOfBytesToRead As Long, Byval lpNumberOfBytesRead As _Offset, Byval lpOverlapped As _Offset)
  49.         Function GetStdHandle& (ByVal nStdHandle As Long)
  50.         Function GetLastError& ()
  51.     End Declare
  52.  
  53.     Dim As String pipecom_buffer
  54.     Dim As Integer ok: ok = TRUE
  55.     Dim As Long hStdOutPipeRead
  56.     Dim As Long hStdOutPipeWrite
  57.     Dim As SECURITY_ATTRIBUTES sa: sa.nLength = Len(sa): sa.lpSecurityDescriptor = 0: sa.bInheritHandle = TRUE
  58.  
  59.     If CreatePipe(_Offset(hStdOutPipeRead), _Offset(hStdOutPipeWrite), _Offset(sa), 0) = FALSE Then
  60.         pipecom = "oops1"
  61.         Print GetLastError
  62.         Exit Function
  63.     End If
  64.  
  65.     Dim As STARTUPINFO si
  66.     si.cb = Len(si)
  67.     si.dwFlags = STARTF_USESTDHANDLES
  68.     si.hStdError = 0
  69.     si.hStdOutput = hStdOutPipeWrite
  70.     si.hStdInput = 0
  71.     Dim As PROCESS_INFORMATION pi
  72.     Dim As _Offset lpApplicationName
  73.     Dim As String fullcmd: fullcmd = "cmd /c " + cmd + Chr$(0)
  74.     Dim As String lpCommandLine: lpCommandLine = fullcmd
  75.     Dim As Long lpProcessAttributes
  76.     Dim As Long lpThreadAttributes
  77.     Dim As Integer bInheritHandles: bInheritHandles = TRUE
  78.     Dim As Long dwCreationFlags: dwCreationFlags = CREATE_NO_WINDOW
  79.     Dim As _Offset lpEnvironment
  80.     Dim As _Offset lpCurrentDirectory
  81.     ok = CreateProcess(lpApplicationName,_
  82.                        _Offset(lpCommandLine),_
  83.                        lpProcessAttributes,_
  84.                        lpThreadAttributes,_
  85.                        bInheritHandles,_
  86.                        dwCreationFlags,_
  87.                        lpEnvironment,_
  88.                        lpCurrentDirectory,_
  89.                        _Offset(si),_
  90.                        _Offset(pi))
  91.     If ok = FALSE Then
  92.         pipecom = "oops2"
  93.         Print GetLastError
  94.     End If
  95.  
  96.     ok = CloseHandle(hStdOutPipeWrite)
  97.  
  98.     Dim As String buf: buf = Space$(4096 + 1)
  99.     Dim As Long dwRead
  100.     While ReadFile(hStdOutPipeRead, _Offset(buf), 4096, _Offset(dwRead), 0) <> 0 And dwRead
  101.         Mid$(buf, InStr(buf, Chr$(13)), 1) = Chr$(0)
  102.         pipecom_buffer = pipecom_buffer + Mid$(buf, 1, dwRead)
  103.     Wend
  104.     Print GetLastError
  105.     ok = CloseHandle(hStdOutPipeRead)
  106.     pipecom = pipecom_buffer

Only one small hop to go before pipecom becomes only an $INCLUDE rather than a header
Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: NOVARSEG on February 11, 2021, 01:23:21 am
noticed that all the variables that start with lp are as _offset.

104 bytes in  Type STARTUPINFO you say?

12 longs = 48 bytes
2 integers = 4 bytes
 total = 52 bytes

52 * 2 = 104 ???

change the integers to longs and the longs to 64 bit

another wild guess
Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: SpriggsySpriggs on February 11, 2021, 08:52:53 am
@NOVARSEG This has already been tried.
Title: Re: Conversion for pipecom to BAS only (Help, Petr!)
Post by: Petr on February 11, 2021, 10:41:54 am
I try it, thank for your trust :)
Title: Pipecom conversion to BAS only (SOLVED!)
Post by: SpriggsySpriggs on February 11, 2021, 10:44:19 am
@Petr @SMcNeill @NOVARSEG @bplus @doppler

Stack Overflow got it fixed! Now pipecom works without a header! You can simply $INCLUDE the below code and it will work in Windows 32&64, Linux, and Mac!

Code: QB64: [Select]
  1.  
  2. Print pipecom("dir")
  3.  
  4. 'begin include
  5. Function pipecom$ (cmd As String)
  6.     $If WIN Then
  7.         Type SECURITY_ATTRIBUTES
  8.             nLength As Long
  9.             $If 64BIT Then
  10.                 padding As Long
  11.             $End If
  12.             lpSecurityDescriptor As _Offset
  13.             bInheritHandle As Long
  14.             $If 64BIT Then
  15.                 padding2 As Long
  16.             $End If
  17.         End Type
  18.  
  19.         Type STARTUPINFO
  20.             cb As Long
  21.             $If 64BIT Then
  22.                 padding As Long
  23.             $End If
  24.             lpReserved As _Offset
  25.             lpDesktop As _Offset
  26.             lpTitle As _Offset
  27.             dwX As Long
  28.             dwY As Long
  29.             dwXSize As Long
  30.             dwYSize As Long
  31.             dwXCountChars As Long
  32.             dwYCountChars As Long
  33.             dwFillAttribute As Long
  34.             dwFlags As Long
  35.             wShowWindow As Integer
  36.             cbReserved2 As Integer
  37.             $If 64BIT Then
  38.                 padding2 As Long
  39.             $End If
  40.             lpReserved2 As _Offset
  41.             hStdInput As _Offset
  42.             hStdOutput As _Offset
  43.             hStdError As _Offset
  44.         End Type
  45.  
  46.         Type PROCESS_INFORMATION
  47.             hProcess As _Offset
  48.             hThread As _Offset
  49.             dwProcessId As Long
  50.             $If 64BIT Then
  51.                 padding2 As Long
  52.             $End If
  53.         End Type
  54.  
  55.         Const STARTF_USESTDHANDLES = &H00000100
  56.         Const CREATE_NO_WINDOW = &H8000000
  57.  
  58.         Declare Dynamic Library "Kernel32"
  59.             Function CreatePipe% (ByVal hReadPipe As _Offset, Byval hWritePipe As _Offset, Byval lpPipeAttributes As _Offset, Byval nSize As Long)
  60.             Function CreateProcess% Alias CreateProcessA (ByVal lpApplicationName As _Offset, Byval lpCommandLine As _Offset, Byval lpProcessAttributes As _Offset, Byval lpThreadAttributes As _Offset, Byval bInheritHandles As Integer, Byval dwCreationFlags As Long, Byval lpEnvironment As _Offset, Byval lpCurrentDirectory As _Offset, Byval lpStartupInfor As _Offset, Byval lpProcessInformation As _Offset)
  61.             Function CloseHandle% (ByVal hObject As Long)
  62.             Function ReadFile% (ByVal hFile As Long, Byval lpBuffer As _Offset, Byval nNumberOfBytesToRead As Long, Byval lpNumberOfBytesRead As _Offset, Byval lpOverlapped As _Offset)
  63.         End Declare
  64.  
  65.         Dim As String pipecom_buffer
  66.         Dim As Integer ok: ok = 1
  67.         Dim As Long hStdOutPipeRead
  68.         Dim As Long hStdOutPipeWrite
  69.         Dim As SECURITY_ATTRIBUTES sa: sa.nLength = Len(sa): sa.lpSecurityDescriptor = 0: sa.bInheritHandle = 1
  70.  
  71.         If CreatePipe(_Offset(hStdOutPipeRead), _Offset(hStdOutPipeWrite), _Offset(sa), 0) = 0 Then
  72.             pipecom = ""
  73.             Exit Function
  74.         End If
  75.  
  76.         Dim As STARTUPINFO si
  77.         si.cb = Len(si)
  78.         si.dwFlags = STARTF_USESTDHANDLES
  79.         si.hStdError = 0
  80.         si.hStdOutput = hStdOutPipeWrite
  81.         si.hStdInput = 0
  82.         Dim As PROCESS_INFORMATION pi
  83.         Dim As _Offset lpApplicationName
  84.         Dim As String fullcmd: fullcmd = "cmd /c " + cmd + " 2>&1" + Chr$(0)
  85.         Dim As String lpCommandLine: lpCommandLine = fullcmd
  86.         Dim As Long lpProcessAttributes
  87.         Dim As Long lpThreadAttributes
  88.         Dim As Integer bInheritHandles: bInheritHandles = 1
  89.         Dim As Long dwCreationFlags: dwCreationFlags = CREATE_NO_WINDOW
  90.         Dim As _Offset lpEnvironment
  91.         Dim As _Offset lpCurrentDirectory
  92.         ok = CreateProcess(lpApplicationName,_
  93.                            _Offset(lpCommandLine),_
  94.                            lpProcessAttributes,_
  95.                            lpThreadAttributes,_
  96.                            bInheritHandles,_
  97.                            dwCreationFlags,_
  98.                            lpEnvironment,_
  99.                            lpCurrentDirectory,_
  100.                            _Offset(si),_
  101.                            _Offset(pi))
  102.         If ok = 0 Then
  103.             pipecom = ""
  104.             Exit Function
  105.         End If
  106.  
  107.         ok = CloseHandle(hStdOutPipeWrite)
  108.  
  109.         Dim As String buf: buf = Space$(4096 + 1)
  110.         Dim As Long dwRead
  111.         While ReadFile(hStdOutPipeRead, _Offset(buf), 4096, _Offset(dwRead), 0) <> 0 And dwRead > 0
  112.             buf = Mid$(buf, 1, dwRead)
  113.             buf = String.Remove(buf, Chr$(13))
  114.             pipecom_buffer = pipecom_buffer + buf
  115.             buf = Space$(4096 + 1)
  116.         Wend
  117.         ok = CloseHandle(hStdOutPipeRead)
  118.         pipecom = pipecom_buffer
  119.     $Else
  120.         Function popen%& (cmd As String, readtype As String)
  121.         Function feof& (ByVal stream As _Offset)
  122.         Function fgets$ (str As String, Byval n As Long, Byval stream As _Offset)
  123.         Function pclose& (ByVal stream As _Offset)
  124.         End Declare
  125.  
  126.         Dim As String pipecom_buffer
  127.         Dim As _Offset stream
  128.  
  129.         Dim buffer As String * 4096
  130.         stream = popen(cmd+ " 2>&1", "r")
  131.         If stream Then
  132.         While feof(stream) = 0
  133.         If fgets(buffer, 4096, stream) <> "" And feof(stream) = 0 Then
  134.         pipecom_buffer = pipecom_buffer + Mid$(buffer, 1, InStr(buffer, Chr$(0)) - 1)
  135.         End If
  136.         Wend
  137.         Dim As Long a
  138.         a = pclose(stream)
  139.         End If
  140.         pipecom = pipecom_buffer
  141.     $End If
  142.  
  143. $If WIN Then
  144.     Function String.Remove$ (a As String, b As String)
  145.         Dim c As String
  146.         Dim j
  147.         Dim r$
  148.         c = ""
  149.         j = InStr(a, b)
  150.         If j > 0 Then
  151.             r$ = Left$(a, j - 1) + c + String.Remove(Right$(a, Len(a) - j + 1 - Len(b)), b)
  152.         Else
  153.             r$ = a
  154.         End If
  155.         String.Remove = r$
  156. 'end include
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: bplus on February 11, 2021, 03:01:49 pm
@SpriggsySpriggs

Nice! but "di" errors in Windows 10 I think you meant "dir"?

Anyway I tested with my pipecom call that I used in browser, it seems to have returned xtra chars at first char in line, possibly a line delimiter conflict? (see attched)



Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SpriggsySpriggs on February 11, 2021, 03:16:00 pm
The "di" is intentional. I wanted to show getting the error of supplying it with a wrong command. As for any extra characters, did you grab my latest code? Also, feel free to diagnose any bugs!
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: bplus on February 11, 2021, 03:46:15 pm
The "di" is intentional. I wanted to show getting the error of supplying it with a wrong command. As for any extra characters, did you grab my latest code? Also, feel free to diagnose any bugs!

I think I did, your last edit was 12++ PM, my post when checking it 3+PM

I cant follow(understand) your code, do you tell it to use a 1 or 2 char delimiter between (dir) lines for returned string from pipecom function?
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SpriggsySpriggs on February 11, 2021, 06:48:45 pm
@bplus @STxAxTIC

Just pasted in my latest code (in the best answer post) that seems to behave the way the old one did. Please redownload and run at your convenience. Let me know your results. Fellippe and I tested it ourselves and have compared it to the original header version.

bplus, as for understanding the code:
Windows uses a carriage-return & line-feed combination. This causes an extra line to be displayed in the window if you leave it like that. So, in order to avoid that, I'm removing all instances of CHR$(13). It's the same way I did it in the header file I had. CHR$(10) is the way that matches how Linux would display line endings and is the absolute best way.
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: bplus on February 11, 2021, 07:33:22 pm
@bplus @STxAxTIC

Just pasted in my latest code (in the best answer post) that seems to behave the way the old one did. Please redownload and run at your convenience. Let me know your results. Fellippe and I tested it ourselves and have compared it to the original header version.

bplus, as for understanding the code:
Windows uses a carriage-return & line-feed combination. This causes an extra line to be displayed in the window if you leave it like that. So, in order to avoid that, I'm removing all instances of CHR$(13). It's the same way I did it in the header file I had. CHR$(10) is the way that matches how Linux would display line endings and is the absolute best way.

@SpriggsySpriggs

It looks good now, tried different switches on Dir, thanks for this!

Is pipecome your own name for this method? I could not find anything strongly related to that name on Internet.

This is nice as alternate to Shell for some Windows OS reports, I think.
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SpriggsySpriggs on February 11, 2021, 07:36:41 pm
@bplus
Yes, pipecom is my own name for it. I came up with it because the action of reading in commands like that is called opening a pipe. And, since it's running a command, I decided to put the two together. Pipecom. Opening a pipe to a command.
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: Pete on February 11, 2021, 07:42:32 pm
I tried to submit a text server to Linux Red Hat, once; but the CEO had a problem with the name Linux ASCII-Hat. Oh well.

Pete
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: NOVARSEG on February 11, 2021, 08:22:44 pm
Trying to understand what the complier directives do. It looks like 4 bytes are added  Does data get stored in padding and padding2   ?

  $If 64BIT Then
                padding As Long
            $End If

$If 64BIT Then
                padding2 As Long
            $End If

example
Quote
cb As Long
            $If 64BIT Then
             padding As Long   
            $End If

is cb now the  LSB dword and is "padding"  the MSB dword?




Quote
Type STARTUPINFO
            cb As Long
            $If 64BIT Then
                padding As Long
            $End If
            lpReserved As _Offset
            lpDesktop As _Offset
            lpTitle As _Offset
            dwX As Long
            dwY As Long
            dwXSize As Long
            dwYSize As Long
            dwXCountChars As Long
            dwYCountChars As Long
            dwFillAttribute As Long
            dwFlags As Long
            wShowWindow As Integer
            cbReserved2 As Integer
            $If 64BIT Then
                padding2 As Long
            $End If
            lpReserved2 As _Offset
            hStdInput As _Offset
            hStdOutput As _Offset
            hStdError As _Offset
        End Type
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SpriggsySpriggs on February 11, 2021, 08:27:55 pm
@NOVARSEG
The $IF64 BIT is just so I can check if it is 64 bit and then add an extra 4 bytes to make the struct aligned properly. Like you mentioned with your 16 vs 32 bit.

As for LSB and MSB, yes (I think)
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: NOVARSEG on February 11, 2021, 09:40:00 pm

OK

Quote
TYPE STARTUPINFO
    cb AS _INTEGER64
    lpReserved AS _OFFSET
    lpDesktop AS _OFFSET
    lpTitle AS _OFFSET
    dwX AS LONG
    dwY AS LONG
    dwXSize AS LONG
    dwYSize AS LONG
    dwXCountChars AS LONG
    dwYCountChars AS LONG
    dwFillAttribute AS LONG
    dwFlags AS LONG
    wShowWindow AS LONG
    cbReserved2 AS LONG
    lpReserved2 AS _OFFSET
    hStdInput AS _OFFSET
    hStdOutput AS _OFFSET
    hStdError AS _OFFSET
END TYPE
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SpriggsySpriggs on February 11, 2021, 09:43:56 pm
@NOVARSEG
It might work but it is incorrect. You don't want to make cb into an 8 byte variable just to
fill the space. You would want to leave it as is.
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SMcNeill on February 11, 2021, 09:55:01 pm
@NOVARSEG
It might work but it is incorrect. You don't want to make cb into an 8 byte variable just to
fill the space. You would want to leave it as is.

It won’t work.  The 2 integers are merged into a single long, giving one a massive value while the other remains 0.
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SpriggsySpriggs on February 11, 2021, 09:55:55 pm
@SMcNeill
I was only referring to cb. I didn't even bother to read the rest haha
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: NOVARSEG on February 11, 2021, 10:06:43 pm
This should work for 64 bit compiler but will it work for 32 bit

Quote
TYPE STARTUPINFO
    cb AS LONG
    pad1 AS LONG
    lpReserved AS _OFFSET
    lpDesktop AS _OFFSET
    lpTitle AS _OFFSET
    dwX AS LONG
    dwY AS LONG
    dwXSize AS LONG
    dwYSize AS LONG
    dwXCountChars AS LONG
    dwYCountChars AS LONG
    dwFillAttribute AS LONG
    dwFlags AS LONG
    wShowWindow AS INTEGER
    cbReserved2 AS INTEGER
    pad2 AS LONG
    lpReserved2 AS _OFFSET
    hStdInput AS _OFFSET
    hStdOutput AS _OFFSET
    hStdError AS _OFFSET
END TYPE

For 32 bit the total is 68 bytes but with pad1 and pad2  it's 72 bytes. It looks like these TYPES are very strict on number of bytes. 

Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SpriggsySpriggs on February 11, 2021, 10:07:25 pm
No
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: NOVARSEG on February 11, 2021, 10:32:04 pm
So it works for 64 bit and not for 32 bit.  I'm trying to figure out a way without the compiler directives.

Even with the compiler directives will 64 bit EXE work on 32 bit computer?
***

Found a pointer to STARTUPINFO



Quote
BOOL CreateProcessA(
  LPCSTR                lpApplicationName,
  LPSTR                 lpCommandLine,
  LPSECURITY_ATTRIBUTES lpProcessAttributes,
  LPSECURITY_ATTRIBUTES lpThreadAttributes,
  BOOL                  bInheritHandles,
  DWORD                 dwCreationFlags,
  LPVOID                lpEnvironment,
  LPCSTR                lpCurrentDirectory,
  LPSTARTUPINFOA        lpStartupInfo,
  LPPROCESS_INFORMATION lpProcessInformation
);

windows calls them pointers.  a pointer is an memory address.
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SpriggsySpriggs on February 11, 2021, 10:38:02 pm
No. At this point, @NOVARSEG , it would be beneficial to you to try googling this yourself.
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SMcNeill on February 11, 2021, 10:55:37 pm
So it works for 64 bit and not for 32 bit.  I'm trying to figure out a way without the compiler directives.

Even with the compiler directives will 64 bit EXE work on 32 bit computer?

No.
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: NOVARSEG on February 11, 2021, 11:27:26 pm
google is mostly armchair experts

There is much talent on this forum.  I cant seem to tap into it.
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: SpriggsySpriggs on February 11, 2021, 11:35:34 pm
@NOVARSEG

How do you think I learned most of what I know about C++ and WinAPI?

Google

&

 


I implore you.... Do some research on Google and the MSDN. IT WORKS
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: Pete on February 12, 2021, 01:23:36 am
DUCKDUCKGO

This message brought to as a public service message from people who don't want a bunch of communists running the planet.

In regard to talent on this forum, I agree, but a lot of that talent is being used on the pursuit of personal projects and work; so there isn't always the time or inclination to teach or contribute everything needed for a novice in a particular area. What I have always seen is people who make an effort, but get stuck at some point, they can usually just post what they have and more than likely, get the advice and help needed to move forward.

I've only done maybe four or five routines to date in Win API. That's enough to get me started, to know where to look, and if I get stuck on something, I would probably get helped by Zach, Dav, or someone else who has worked with something similar. The other thing is, this is a QB64 forum, and although QB64 can take advantage of other languages, not everyone here is an expert, or experienced, in those other languages, at least not to the degree they are in Qb64.
Title: Re: Pipecom conversion to BAS only (SOLVED!)
Post by: Dav on February 12, 2021, 07:53:59 am
While people are sharing places to learn/research windows api, I thought Id share a resource that has helped me for many years. The site is outdated now, but most api's listed come with great short examples in VB that helped me learn and get them going in QB64: http://allapi.mentalis.org/apilist/apilist.php (http://allapi.mentalis.org/apilist/apilist.php).

Hey,@Pete, ever used startpage? Its been around a long time too, like duckduckgo (which i also use) , doesnt get much mention for some odd reason.  I get good results when searching for programming examples. My browser likes it.

- Dav
Title: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: SpriggsySpriggs on February 12, 2021, 06:55:54 pm
pipecom is a cross-platform utility for directly obtaining console output in Windows, Mac, and Linux. Previously, one would need to use files.

Below is the current state of pipecom and some basic usage:

For those of you who only need the console output for a given command:
Code: QB64: [Select]
  1. Print pipecom_lite("dir")

If you want exit code, stdout (the output one sees on a successful console app run), and stderr (the output one sees when the console command/app has failed execution):
Code: QB64: [Select]
  1. Dim As Long exit_code
  2. Dim As String cmd, stdout, stderr
  3. cmd = "dir /b *.bas"
  4. exit_code = pipecom(cmd, stdout, stderr)
  5. If stderr <> "" Then
  6.     Print stderr
  7.     Print stdout
  8.  

And the full include:
Code: QB64: [Select]
  1. $If PIPECOM = UNDEFINED Then
  2.     $Let PIPECOM = TRUE
  3.     Function pipecom& (cmd As String, stdout As String, stderr As String)
  4.         stdout = "": stderr = ""
  5.         $If WIN Then
  6.             Type SECURITY_ATTRIBUTES
  7.                 As Long nLength
  8.                 $If 64BIT Then
  9.                     As Long padding
  10.                 $End If
  11.                 As _Offset lpSecurityDescriptor
  12.                 As Long bInheritHandle
  13.                 $If 64BIT Then
  14.                     As Long padding2
  15.                 $End If
  16.             End Type
  17.  
  18.             Type STARTUPINFO
  19.                 As Long cb
  20.                 $If 64BIT Then
  21.                     As Long padding
  22.                 $End If
  23.                 As _Offset lpReserved, lpDesktop, lpTitle
  24.                 As Long dwX, dwY, dwXSize, dwYSize, dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags
  25.                 As Integer wShowWindow, cbReserved2
  26.                 $If 64BIT Then
  27.                     As Long padding2
  28.                 $End If
  29.                 As _Offset lpReserved2, hStdInput, hStdOutput, hStdError
  30.             End Type
  31.  
  32.             Type PROCESS_INFORMATION
  33.                 As _Offset hProcess, hThread
  34.                 As Long dwProcessId
  35.                 $If 64BIT Then
  36.                     As Long padding
  37.                 $End If
  38.             End Type
  39.  
  40.             Const STARTF_USESTDHANDLES = &H00000100
  41.             Const CREATE_NO_WINDOW = &H8000000
  42.  
  43.             Const INFINITE = 4294967295
  44.             Const WAIT_FAILED = &HFFFFFFFF
  45.  
  46.             Declare CustomType Library
  47.                 Function CreatePipe%% (ByVal hReadPipe As _Offset, Byval hWritePipe As _Offset, Byval lpPipeAttributes As _Offset, Byval nSize As Long)
  48.                 Function CreateProcess%% Alias "CreateProcessA" (ByVal lpApplicationName As _Offset, Byval lpCommandLine As _Offset, Byval lpProcessAttributes As _Offset, Byval lpThreadAttributes As _Offset, Byval bInheritHandles As Integer, Byval dwCreationFlags As Long, Byval lpEnvironment As _Offset, Byval lpCurrentDirectory As _Offset, Byval lpStartupInfor As _Offset, Byval lpProcessInformation As _Offset)
  49.                 Function GetExitCodeProcess%% (ByVal hProcess As _Offset, Byval lpExitCode As _Offset)
  50.                 Sub HandleClose Alias "CloseHandle" (ByVal hObject As _Offset)
  51.                 Function ReadFile%% (ByVal hFile As _Offset, Byval lpBuffer As _Offset, Byval nNumberOfBytesToRead As Long, Byval lpNumberOfBytesRead As _Offset, Byval lpOverlapped As _Offset)
  52.                 Function WaitForSingleObject& (ByVal hHandle As _Offset, Byval dwMilliseconds As Long)
  53.             End Declare
  54.  
  55.             Dim As _Byte ok: ok = 1
  56.             Dim As _Offset hStdOutPipeRead, hStdOutPipeWrite, hStdReadPipeError, hStdOutPipeError
  57.             Dim As SECURITY_ATTRIBUTES sa: sa.nLength = Len(sa): sa.lpSecurityDescriptor = 0: sa.bInheritHandle = 1
  58.  
  59.             If CreatePipe(_Offset(hStdOutPipeRead), _Offset(hStdOutPipeWrite), _Offset(sa), 0) = 0 Then
  60.                 pipecom = -1
  61.                 Exit Function
  62.             End If
  63.  
  64.             If CreatePipe(_Offset(hStdReadPipeError), _Offset(hStdOutPipeError), _Offset(sa), 0) = 0 Then
  65.                 pipecom = -1
  66.                 Exit Function
  67.             End If
  68.  
  69.             Dim As STARTUPINFO si
  70.             si.cb = Len(si)
  71.             si.dwFlags = STARTF_USESTDHANDLES
  72.             si.hStdError = hStdOutPipeError
  73.             si.hStdOutput = hStdOutPipeWrite
  74.             si.hStdInput = 0
  75.             Dim As PROCESS_INFORMATION procinfo
  76.             Dim As _Offset lpApplicationName
  77.             Dim As String fullcmd: fullcmd = "cmd /c " + cmd + Chr$(0)
  78.             Dim As String lpCommandLine: lpCommandLine = fullcmd
  79.             Dim As _Offset lpProcessAttributes, lpThreadAttributes
  80.             Dim As Integer bInheritHandles: bInheritHandles = 1
  81.             Dim As Long dwCreationFlags: dwCreationFlags = CREATE_NO_WINDOW
  82.             Dim As _Offset lpEnvironment, lpCurrentDirectory
  83.             ok = CreateProcess(lpApplicationName, _Offset(lpCommandLine), lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, _Offset(si), _Offset(procinfo))
  84.  
  85.             If ok = 0 Then
  86.                 pipecom = -1
  87.                 Exit Function
  88.             End If
  89.  
  90.             HandleClose hStdOutPipeWrite
  91.             HandleClose hStdOutPipeError
  92.  
  93.             Dim As String buf: buf = Space$(4096 + 1)
  94.             Dim As Long dwRead
  95.             While ReadFile(hStdOutPipeRead, _Offset(buf), 4096, _Offset(dwRead), 0) <> 0 And dwRead > 0
  96.                 buf = Mid$(buf, 1, dwRead)
  97.                 GoSub RemoveChr13
  98.                 stdout = stdout + buf
  99.                 buf = Space$(4096 + 1)
  100.             Wend
  101.  
  102.             While ReadFile(hStdReadPipeError, _Offset(buf), 4096, _Offset(dwRead), 0) <> 0 And dwRead > 0
  103.                 buf = Mid$(buf, 1, dwRead)
  104.                 GoSub RemoveChr13
  105.                 stderr = stderr + buf
  106.                 buf = Space$(4096 + 1)
  107.             Wend
  108.  
  109.             Dim As Long exit_code, ex_stat
  110.             If WaitForSingleObject(procinfo.hProcess, INFINITE) <> WAIT_FAILED Then
  111.                 If GetExitCodeProcess(procinfo.hProcess, _Offset(exit_code)) Then
  112.                     ex_stat = 1
  113.                 End If
  114.             End If
  115.  
  116.             HandleClose hStdOutPipeRead
  117.             HandleClose hStdReadPipeError
  118.             If ex_stat = 1 Then
  119.                 pipecom = exit_code
  120.             Else
  121.                 pipecom = -1
  122.             End If
  123.  
  124.             Exit Function
  125.  
  126.             RemoveChr13:
  127.             Dim As Long j
  128.             j = InStr(buf, Chr$(13))
  129.             Do While j
  130.                 buf = Left$(buf, j - 1) + Mid$(buf, j + 1)
  131.                 j = InStr(buf, Chr$(13))
  132.             Loop
  133.             Return
  134.         $Else
  135.             Declare CustomType Library
  136.             Function popen%& (cmd As String, readtype As String)
  137.             Function feof& (ByVal stream As _Offset)
  138.             Function fgets$ (str As String, Byval n As Long, Byval stream As _Offset)
  139.             Function pclose& (ByVal stream As _Offset)
  140.             End Declare
  141.  
  142.             Declare Library
  143.             Function WEXITSTATUS& (ByVal stat_val As Long)
  144.             End Declare
  145.  
  146.             Dim As String pipecom_buffer
  147.             Dim As _Offset stream
  148.  
  149.             Dim buffer As String * 4096
  150.             If _FileExists("pipestderr") Then
  151.             Kill "pipestderr"
  152.             End If
  153.             stream = popen(cmd + " 2>pipestderr", "r")
  154.             If stream Then
  155.             While feof(stream) = 0
  156.             If fgets(buffer, 4096, stream) <> "" And feof(stream) = 0 Then
  157.             stdout = stdout + Mid$(buffer, 1, InStr(buffer, Chr$(0)) - 1)
  158.             End If
  159.             Wend
  160.             Dim As Long status, exit_code
  161.             status = pclose(stream)
  162.             exit_code = WEXITSTATUS(status)
  163.             If _FileExists("pipestderr") Then
  164.             Dim As Integer errfile
  165.             errfile = FreeFile
  166.             Open "pipestderr" For Binary As #errfile
  167.             If LOF(errfile) > 0 Then
  168.             stderr = Space$(LOF(errfile))
  169.             Get #errfile, , stderr
  170.             End If
  171.             Close #errfile
  172.             Kill "pipestderr"
  173.             End If
  174.             pipecom = exit_code
  175.             Else
  176.             pipecom = -1
  177.             End If
  178.         $End If
  179.  
  180.     Function pipecom_lite$ (cmd As String)
  181.         Dim As Long a
  182.         Dim As String stdout, stderr
  183.         a = pipecom(cmd, stdout, stderr)
  184.         If stderr <> "" Then
  185.             pipecom_lite = stderr
  186.         Else
  187.             pipecom_lite = stdout
  188.         End If
Title: Re: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: SpriggsySpriggs on February 12, 2021, 08:02:53 pm
Alright, the bug is fixed. Download and run, my minions!
Title: Pipecom update ($NOPREFIX compatible and precompile checks)
Post by: SpriggsySpriggs on June 13, 2021, 12:07:31 pm
I've updated pipecom to be $NOPREFIX compatible and use precompiler checks to make sure you don't have issues including it if it is already $INCLUDEd somewhere else in your code.
Title: Re: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: SpriggsySpriggs on June 17, 2021, 05:00:04 pm
Fixed a bug (from copying and pasting) where a variable wasn't declared.
Title: Re: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: George McGinn on June 20, 2021, 04:17:02 pm
I have this version of pipecom. I am running this on Linux.

I have a program using Zenity as my GUI. I execute pipecom (not the lite one) as I need to know what stderr and stdout is. My program loops and does the Form a second time, based on the selection of the user.

However, pipecom is returning an exit_code of 2, which is causing me headaches.

If I execute pipecom more than once, do I need to kill it before executing it again?

Why would it work correctly the first time through the code and then not any subsequent times?

I have verified that it is not an issue with Zenity (As I executed my commands in the terminal and using SHELL in the program and it works fine).

UPDATE: I've decided to only use pipecom when I am retrieving data from a form. All else I am using the SHELL statement, which works.

Title: Re: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: SpriggsySpriggs on June 20, 2021, 06:45:45 pm
Try using _CLIPBOARD$ to capture the second call and paste it in your terminal
Title: Re: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: George McGinn on June 20, 2021, 06:58:41 pm
I'll take a look at that. Thanks.

Try using _CLIPBOARD$ to capture the second call and paste it in your terminal
Title: Re: Pipecom conversion to BAS only (bug fix)
Post by: SpriggsySpriggs on June 24, 2021, 09:21:41 am
I have fixed a bug, mentioned by @George McGinn , that would cause stdout and stderr to be accumulated if reusing the variables for the call. I'm clearing out stdout and stderr before running the code which shouldn't cause this. I'd never experienced this because I was never making multiple calls per code.
Title: Pipecom minor fixes
Post by: SpriggsySpriggs on August 24, 2021, 02:35:08 pm
Updated pipecom to fix some typos and some incorrect variable declarations. So far, I don't notice any change in behavior so you might not have to download the latest version unless you absolutely want to.
Title: Re: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: Richard Frost on August 24, 2021, 03:52:42 pm
This is amazing, and despite studying the code for an hour I cannot
figure out how it works.  I need a new brain.

Some variable/offset names are exciting - Process?  Thread?  Can you
do multithreading?  If so, please write a demo - calculating and
plotting Mandelbrots would be something most could understand.

Title: Re: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: dbox on December 03, 2021, 10:25:49 am
Thanks for this great utility.  Wanted to let you know about an issue I found on Linux.  If the command I send to pipecom is too long, it causes errors indicating that the command text is being truncated.  If I use an arbitrarily large fixed length string instead it seems to work properly.

Just changed the following line:
Code: QB64: [Select]
  1. stream = popen(cmd + " 2>pipestderr", "r")

to this:
Code: QB64: [Select]
  1. Dim cmdf as String * 200
  2. cmdf = cmd + " 2>pipestderr"
  3. stream = popen(cmdf, "r")
Title: Re: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: George McGinn on December 03, 2021, 12:09:30 pm
I pass in a Linux application I am writing to pipecom a series of SQL commands (where cmd is more than 2,600 characters) with no issues.

@dbox - Is this only an issue with output to stderr?

The code that Linux executes is:
Code: QB64: [Select]
  1. stream = popen(cmd + " 2>pipestderr" + Chr$(0), "r")

Are you running with the current version? Below is the version I am running that I believe is the most current:

Code: QB64: [Select]
  1. $If PIPECOM = UNDEFINED Then
  2.     $Let PIPECOM = TRUE
  3.     Function pipecom& (cmd As String, stdout As String, stderr As String)
  4.         stdout = "": stderr = ""
  5.         $If WIN Then
  6.             Type SECURITY_ATTRIBUTES
  7.                 As _Unsigned Long nLength
  8.                 $If 64BIT Then
  9.                     As String * 4 padding
  10.                 $End If
  11.                 As _Offset lpSecurityDescriptor
  12.                 As Long bInheritHandle
  13.                 $If 64BIT Then
  14.                     As String * 4 padding2
  15.                 $End If
  16.             End Type
  17.  
  18.             Type STARTUPINFO
  19.                 As Long cb
  20.                 $If 64BIT Then
  21.                     As String * 4 padding
  22.                 $End If
  23.                 As _Offset lpReserved, lpDesktop, lpTitle
  24.                 As _Unsigned Long dwX, dwY, dwXSize, dwYSize, dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags
  25.                 As _Unsigned Integer wShowWindow, cbReserved2
  26.                 $If 64BIT Then
  27.                     As String * 4 padding2
  28.                 $End If
  29.                 As _Offset lpReserved2, hStdInput, hStdOutput, hStdError
  30.             End Type
  31.  
  32.             Type PROCESS_INFORMATION
  33.                 As _Offset hProcess, hThread
  34.                 As _Unsigned Long dwProcessId
  35.                 $If 64BIT Then
  36.                     As String * 4 padding
  37.                 $End If
  38.             End Type
  39.  
  40.             Const STARTF_USESTDHANDLES = &H00000100
  41.             Const CREATE_NO_WINDOW = &H8000000
  42.  
  43.             Const INFINITE = 4294967295
  44.             Const WAIT_FAILED = &HFFFFFFFF
  45.  
  46.             Declare CustomType Library
  47.                 Function CreatePipe& (ByVal hReadPipe As _Offset, Byval hWritePipe As _Offset, Byval lpPipeAttributes As _Offset, Byval nSize As _Unsigned Long)
  48.                 Function CreateProcess& (ByVal lpApplicationName As _Offset, Byval lpCommandLine As _Offset, Byval lpProcessAttributes As _Offset, Byval lpThreadAttributes As _Offset, Byval bInheritHandles As Long, Byval dwCreationFlags As _Unsigned Long, Byval lpEnvironment As _Offset, Byval lpCurrentDirectory As _Offset, Byval lpStartupInfo As _Offset, Byval lpProcessInformation As _Offset)
  49.                 Function GetExitCodeProcess& (ByVal hProcess As _Offset, Byval lpExitCode As _Offset)
  50.                 Sub HandleClose Alias "CloseHandle" (ByVal hObject As _Offset)
  51.                 Function ReadFile& (ByVal hFile As _Offset, Byval lpBuffer As _Offset, Byval nNumberOfBytesToRead As _Unsigned Long, Byval lpNumberOfBytesRead As _Offset, Byval lpOverlapped As _Offset)
  52.                 Function WaitForSingleObject~& (ByVal hHandle As _Offset, Byval dwMilliseconds As _Unsigned Long)
  53.             End Declare
  54.  
  55.             Dim As Long ok: ok = 1
  56.             Dim As _Offset hStdOutPipeRead, hStdOutPipeWrite, hStdReadPipeError, hStdOutPipeError
  57.             Dim As SECURITY_ATTRIBUTES sa: sa.nLength = Len(sa): sa.lpSecurityDescriptor = 0: sa.bInheritHandle = 1
  58.  
  59.             If CreatePipe(_Offset(hStdOutPipeRead), _Offset(hStdOutPipeWrite), _Offset(sa), 0) = 0 Then
  60.                 pipecom = -1
  61.                 Exit Function
  62.             End If
  63.  
  64.             If CreatePipe(_Offset(hStdReadPipeError), _Offset(hStdOutPipeError), _Offset(sa), 0) = 0 Then
  65.                 pipecom = -1
  66.                 Exit Function
  67.             End If
  68.  
  69.             Dim As STARTUPINFO si
  70.             si.cb = Len(si)
  71.             si.dwFlags = STARTF_USESTDHANDLES
  72.             si.hStdError = hStdOutPipeError
  73.             si.hStdOutput = hStdOutPipeWrite
  74.             si.hStdInput = 0
  75.             Dim As PROCESS_INFORMATION procinfo
  76.             Dim As _Offset lpApplicationName
  77.             Dim As String lpCommandLine: lpCommandLine = "cmd /c " + cmd + Chr$(0)
  78.             Dim As _Offset lpProcessAttributes, lpThreadAttributes
  79.             Dim As Long bInheritHandles: bInheritHandles = 1
  80.             Dim As _Unsigned Long dwCreationFlags: dwCreationFlags = CREATE_NO_WINDOW
  81.             Dim As _Offset lpEnvironment, lpCurrentDirectory
  82.             ok = CreateProcess(lpApplicationName, _Offset(lpCommandLine), lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, _Offset(si), _Offset(procinfo))
  83.  
  84.             If ok = 0 Then
  85.                 pipecom = -1
  86.                 Exit Function
  87.             End If
  88.  
  89.             HandleClose hStdOutPipeWrite
  90.             HandleClose hStdOutPipeError
  91.  
  92.             Dim As String buf: buf = Space$(4096 + 1)
  93.             Dim As _Unsigned Long dwRead
  94.             While ReadFile(hStdOutPipeRead, _Offset(buf), 4096, _Offset(dwRead), 0) <> 0 And dwRead > 0
  95.                 buf = Mid$(buf, 1, dwRead)
  96.                 GoSub RemoveChr13
  97.                 stdout = stdout + buf
  98.                 buf = Space$(4096 + 1)
  99.             Wend
  100.  
  101.             While ReadFile(hStdReadPipeError, _Offset(buf), 4096, _Offset(dwRead), 0) <> 0 And dwRead > 0
  102.                 buf = Mid$(buf, 1, dwRead)
  103.                 GoSub RemoveChr13
  104.                 stderr = stderr + buf
  105.                 buf = Space$(4096 + 1)
  106.             Wend
  107.  
  108.             Dim As Long exit_code, ex_stat
  109.             If WaitForSingleObject(procinfo.hProcess, INFINITE) <> WAIT_FAILED Then
  110.                 If GetExitCodeProcess(procinfo.hProcess, _Offset(exit_code)) Then
  111.                     ex_stat = 1
  112.                 End If
  113.             End If
  114.  
  115.             HandleClose hStdOutPipeRead
  116.             HandleClose hStdReadPipeError
  117.             If ex_stat = 1 Then
  118.                 pipecom = exit_code
  119.             Else
  120.                 pipecom = -1
  121.             End If
  122.  
  123.             Exit Function
  124.  
  125.             RemoveChr13:
  126.             Dim As Long j
  127.             j = InStr(buf, Chr$(13))
  128.             Do While j
  129.                 buf = Left$(buf, j - 1) + Mid$(buf, j + 1)
  130.                 j = InStr(buf, Chr$(13))
  131.             Loop
  132.             Return
  133.         $Else
  134.             Declare CustomType Library
  135.                                 Function popen%& (cmd As String, readtype As String)
  136.                                 Function feof& (ByVal stream As _Offset)
  137.                                 Function fgets$ (str As String, Byval n As Long, Byval stream As _Offset)
  138.                                 Function pclose& (ByVal stream As _Offset)
  139.             End Declare
  140.  
  141.             Declare Library
  142.                                 Function WEXITSTATUS& (ByVal stat_val As Long)
  143.             End Declare
  144.  
  145.             Dim As _Offset stream
  146.  
  147.             Dim buffer As String * 4096
  148.             If _FileExists("pipestderr") Then
  149.                                 Kill "pipestderr"
  150.             End If
  151.             stream = popen(cmd + " 2>pipestderr" + Chr$(0), "r")
  152.             If stream Then
  153.                                 While feof(stream) = 0
  154.                                         If fgets(buffer, 4096, stream) <> "" And feof(stream) = 0 Then
  155.                                                 stdout = stdout + Mid$(buffer, 1, InStr(buffer, Chr$(0)) - 1)
  156.                                         End If
  157.                                 Wend
  158.                                 Dim As Long status, exit_code
  159.                                 status = pclose(stream)
  160.                                 exit_code = WEXITSTATUS(status)
  161.                                 If _FileExists("pipestderr") Then
  162.                                         Dim As Integer errfile
  163.                                         errfile = FreeFile
  164.                                         Open "pipestderr" For Binary As #errfile
  165.                                         If LOF(errfile) > 0 Then
  166.                                                 stderr = Space$(LOF(errfile))
  167.                                                 Get #errfile, , stderr
  168.                                         End If
  169.                                         Close #errfile
  170.                                         Kill "pipestderr"
  171.                                 End If
  172.                                 pipecom = exit_code
  173.                         Else
  174.                                 pipecom = -1
  175.                         End If
  176.         $End If
  177.  
  178.     Function pipecom_lite$ (cmd As String)
  179.         Dim As Long a
  180.         Dim As String stdout, stderr
  181.         a = pipecom(cmd, stdout, stderr)
  182.         If stderr <> "" Then
  183.             pipecom_lite = stderr
  184.         Else
  185.             pipecom_lite = stdout
  186.         End If
  187.  



Thanks for this great utility.  Wanted to let you know about an issue I found on Linux.  If the command I send to pipecom is too long, it causes errors indicating that the command text is being truncated.  If I use an arbitrarily large fixed length string instead it seems to work properly.

Just changed the following line:
Code: QB64: [Select]
  1. stream = popen(cmd + " 2>pipestderr", "r")

to this:
Code: QB64: [Select]
  1. Dim cmdf as String * 200
  2. cmdf = cmd + " 2>pipestderr"
  3. stream = popen(cmdf, "r")
Title: Re: Pipecom conversion to BAS only (MAJOR UPDATE!)
Post by: dbox on December 03, 2021, 02:06:11 pm
@George McGinn - thanks for the reply.

Looks like the version you have is appending a NUL to the end of the string which resolves the issue for me too.  I guess I did not have the latest version.  When I searched for it I was taken here:  https://www.qb64.org/forum/index.php?topic=3641.msg134943#msg134943.  It does not seem to be the same version you have.  It looks like the latest version in @SpriggsySpriggs github matches what you posted.  I'll get any future updates from there.

Thanks again.