Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Simple STDOUT for PB/DLL and PB/Win

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple STDOUT for PB/DLL and PB/Win

    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).]
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

  • #2
    Thread-safe version
    Code:
    #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
       STATIC CSInitialized AS LONG, CS AS CRITICAL_SECTION
    
       IF ISFALSE CSInitialized THEN
           InitializeCriticalSection CS
           CSInitialized  =  1
       END IF
    
       EntercriticalSection Cs
    
       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
                                                     ' test for %NULL added 8.26.04 for Win/XP
         AllocConsole
         hStdOut  = GetStdHandle (%STD_OUTPUT_HANDLE)
       END IF
    
       LeaveCriticalSection CS
    
       w = Z & $CRLF
    
       FUNCTION = WriteFile(hStdOut, BYVAL STRPTR(W), LEN(W),  nCharsWritten, BYVAL %NULL)
    
     END FUNCTION
    #ENDIF


    [This message has been edited by Michael Mattias (edited August 26, 2004).]
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      8/26/04

      Added test for %NULL in addition to -1& (INVALID_HANDLE_VALUE)to test for current hStdout.

      Apparently required on Win/XP (Not tested)

      Did not impair operation n Win/98 or Win/2000

      MCM
      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        [CODE]
        'pbwin_stdout.inc
        'http://-

        Comment

        Working...
        X