Code:
' stdout_for_pbwin.bas ' 2.13.04 ' 08.26.04 ' Added test for hStdOut=0 in addition to -1& (INVALID_HANDLE_VALUE) ' Compiler: PB/Windows v 7.02 ' Win32API.INC: 5/9/2002 ' Author : Michael Mattias Racine WI ' Public Domain #COMPILE EXE #DEBUG ERROR ON #REGISTER NONE #DIM ALL #TOOLS OFF #INCLUDE "Win32API.INC" #IF %DEF(%PB_WIN32) FUNCTION STDOUT (Z AS STRING) AS LONG ' returns TRUE (non-zero) on success LOCAL hStdOut AS LONG, nCharsWritten AS LONG LOCAL w AS STRING hStdOut = GetStdHandle (%STD_OUTPUT_HANDLE) IF hSTdOut = -1& or hStdOut = 0& THEN ' invalid handle value, coded in line to avoid ' casting differences in Win32API.INC ' %NULL test added for Win/XP AllocConsole hStdOut = GetStdHandle (%STD_OUTPUT_HANDLE) END IF w = Z & $CRLF FUNCTION = WriteFile(hStdOut, BYVAL STRPTR(W), LEN(W), nCharsWritten, BYVAL %NULL) END FUNCTION #ENDIF FUNCTION TestStdOut () AS LONG LOCAL Z AS LONG 'First, try with a console FOR Z = 1 TO 10 STDOUT "This is line #" & STR$(Z) & " of STDOUT to console" NEXT ' now redirect the output to a disk file... LOCAL hFile AS LONG, hSys AS LONG, szFile AS ASCIIZ * %MAX_PATH szFile = "My_pbwin_stdout.txt" hFile = FREEFILE OPEN szFile FOR OUTPUT AS hFile hSys = FILEATTR(hFile,2) ' get system handle SetStdHandle %STD_OUTPUT_HANDLE, hSys ' make this file the STDOUT for this process STDOUT "Date and Time are " & DATE$ & " " & TIME$ FOR Z = 1 TO 10 STDOUT "This is line #" & STR$(Z) & " of STDOUT as redirected to file " & szFile NEXT STDOUT "End of File" CLOSE hFile ' close the PB handle, which will close the system handle ' show the file to prove success.. Z = SHELL("Notepad.exe " & szFile) END FUNCTION FUNCTION PBMAIN() AS LONG CALL TestStdout MSGBOX "All Done", %MB_ICONINFORMATION OR %MB_TASKMODAL, "STDOUT for PB/Win Demo" END FUNCTION
------------------
Michael Mattias
Tal Systems Inc.
Racine WI USA
mailto:[email protected][email protected]</A>
www.talsystems.com
[This message has been edited by Michael Mattias (edited August 26, 2004).]
Comment