PowerBasic Forum
#11
This is the newer version of Stuart's PipeToString function provided in an SLL.

It uses the new AllocConsoleWithOptions that is available since Windows 11 24H2 build 26100. 

This version allows you to use the pipetostring function without without showing the console itself. There are tons of interesting applications for this. Warning: only WINDOWS 11 24H2


Code:
'========================================================================================================
'PipeToString SLL for PBWin
'=========================================================================================================
#COMPILE SLL "PipeToString-PBWin.sll"
#DIM ALL
#INCLUDE "win32api.inc"
DECLARE FUNCTION popen CDECL LIB "msvcrt.dll" ALIAS "_popen" (BYREF cmd AS STRINGZ, BYREF MODE AS STRINGZ) AS DWORD
DECLARE FUNCTION fread CDECL LIB "msvcrt.dll" ALIAS "fread" (BYVAL buffer AS DWORD, BYVAL elemSize AS DWORD, BYVAL elemCount AS DWORD, BYVAL fp AS DWORD) AS DWORD
DECLARE FUNCTION fclose CDECL LIB "msvcrt.dll" ALIAS "fclose" (BYVAL fp AS DWORD) AS LONG

type ALLOC_CONSOLE_OPTIONS
    mode          as long
    nUseShowWindow as long
    wShowWindow    as word
end type

Declare function AllocConsoleWithOptions Import "Kernel32.dll" alias "AllocConsoleWithOptions" _
  (opt ByVal tOptions as ALLOC_CONSOLE_OPTIONS Ptr, Opt Byref nResult as long) as long

enum ALLOC_CONSOLE_RESULT
  ALLOC_CONSOLE_RESULT_NO_CONSOLE      = 0
  ALLOC_CONSOLE_RESULT_NEW_CONSOLE      = 1
  ALLOC_CONSOLE_RESULT_EXISTING_CONSOLE = 2
end enum

enum ALLOC_CONSOLE_MODE
  ALLOC_CONSOLE_MODE_DEFAULT    = 0
  ALLOC_CONSOLE_MODE_NEW_WINDOW = 1
  ALLOC_CONSOLE_MODE_NO_WINDOW  = 2
end enum


FUNCTION PipeToString(cmdIn AS STRING) COMMON AS WSTRING
    LOCAL sbOut AS ISTRINGBUILDERA
    LOCAL szBuf AS STRINGZ * 1048
    LOCAL szCmdIn AS STRINGZ * 256
    LOCAL hP,dwBytesRead  AS DWORD

    local tConsole as ALLOC_CONSOLE_OPTIONS
    local nResult  as long
    local hR        as long

    tConsole.mode = %ALLOC_CONSOLE_MODE.ALLOC_CONSOLE_MODE_NO_WINDOW

    szCmdIn = cmdIn
    hR = AllocConsoleWithOptions(VarPtr(tConsole), nResult)
    if HR <> 0 then exit function
    hP = popen(szCmdIn, "rb")
    IF hP = 0 THEN EXIT FUNCTION
    sbOut = CLASS "StringBuilderA"
    DO
      dwBytesRead = fread(VARPTR(szBuf), 1, SIZEOF(szBuf), hP)
      sbOut.Add(LEFT$(szBuf, dwBytesRead))
    LOOP WHILE dwBytesRead
    fclose(hP)
    Freeconsole
    FUNCTION = OEMTOCHR$(sbOut.string)
END FUNCTION
'
Reply


Messages In This Thread
PowerBasic Forum - by Giuseppe Belziti - 01-27-2025, 03:50 PM
RE: PowerBasic Forum - by Mike Doty - 02-01-2025, 01:47 AM
RE: PowerBasic Forum - by Owen_English - 05-29-2025, 12:07 AM
RE: PowerBasic Forum - by Albert Richheimer - 05-29-2025, 05:58 AM
RE: PowerBasic Forum - by Steven Pringels - 08-28-2025, 04:57 PM
RE: PowerBasic Forum - by Albert Richheimer - 08-28-2025, 06:32 PM
RE: PowerBasic Forum - by Stanley Durham - 08-28-2025, 10:56 PM
RE: PowerBasic Forum - by Stuart McLachlan - 08-29-2025, 02:25 AM
RE: PowerBasic Forum - by Gary Beene - 08-29-2025, 05:27 AM
RE: PowerBasic Forum - by Eric Pearson - 08-29-2025, 06:19 PM
RE: PowerBasic Forum - by Steven Pringels - 08-29-2025, 09:04 PM
RE: PowerBasic Forum - by Stuart McLachlan - 08-29-2025, 10:27 PM
RE: PowerBasic Forum - by Gary Beene - 08-30-2025, 04:36 AM
RE: PowerBasic Forum - by Stuart McLachlan - 08-30-2025, 04:57 AM
RE: PowerBasic Forum - by Dale Yarker - 08-30-2025, 05:56 AM
RE: PowerBasic Forum - by Stuart McLachlan - 08-30-2025, 06:43 AM
RE: PowerBasic Forum - by Dale Yarker - 08-30-2025, 08:12 AM
RE: PowerBasic Forum - by Stuart McLachlan - 08-30-2025, 09:00 AM
RE: PowerBasic Forum - by Rod Macia - 08-30-2025, 08:39 AM
RE: PowerBasic Forum - by Dale Yarker - 08-30-2025, 09:38 AM
RE: PowerBasic Forum - by Stuart McLachlan - 08-30-2025, 10:19 AM
RE: PowerBasic Forum - by Albert Richheimer - 08-30-2025, 10:43 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)