I have adapted dodicat's PipeToString().
Here is the FreeBASIC code compiled as a dll.
Code: Select all
#cmdline "-gen gcc -O 2 "
#include once "win/ole2.bi"
Function StrToBSTR( FB_String As String) As BSTR
Return SysAllocStringByteLen( FB_String, Len(FB_String) )
End Function
Extern "Windows-MS"
Public Function PipeToString( ByRef s As ZString ) As BSTR Export
Dim dummy as string = s
Dim As Bstr bstrCopy0, bstrCopy1
Var f=Freefile
Dim As String tmp
Open Pipe s For Input As #f
dummy = ""
Do Until Eof(f)
Line Input #f,tmp
dummy += tmp + Chr(10)
Loop
Close #f
bstrCopy0 = StrToBSTR(dummy)
bstrCopy1 = bstrCopy0 ' I'm using this so that I can use SysFreeString
SysFreeString(bstrCopy0)
Return bstrCopy1
End Function
End Extern
Code: Select all
#Compile exe
#Dim All
#Include "win32api.inc"
Declare Function PipeToString Lib "PipeToString.dll" Alias "PipeToString"( s As StringZ ) As String
Function PBMain
Print PipeToString("tasklist")
Waitkey$
End Function
Regarding the parameter, ByRef is used in the FB code because that is what PowerBASIC uses by default. FB's ZString equates with PB's StringZ.
However, FB's strings are not the same as PB's strings, and why I am exporting a BSTR in the FB code.
tasklist gets printed in PB, but the formatting is well out. It is as if Chr(10) in the FB code is being ignored.
I am afraid that I have gone sailing past my pay grade on this one. Either that or senile decay is definitely starting to appear.
Any suggestions?