08-29-2025, 09:04 PM
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
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
'