Announcement

Collapse
No announcement yet.

Writing to STDOUT from PBDLL6

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

  • Writing to STDOUT from PBDLL6

    Hello all,

    The following PBDLL6 snippet should (or so I thought) write a text string if you run it
    from a DOS window under Windows (98) -

    Code:
    '
    'Write a simple string to STDOUT
    '
    #compile   exe
    #dim       all
    #register  none
    #include   "c:\pbdll6\winapi\win32api.inc"
    
    function pbmain() as long
    
    	dim mBytes   as long
    	dim sText    as asciiz * 10
    
    	sText = "Hello"
    	WriteFile GetStdHandle(%STD_OUTPUT_HANDLE), sText, Len(sText), mBytes, ByVal %NULL
    
    	msgbox "Wrote " & format$(mBytes) & " bytes"
    
    end function
    Unfortunately it doesn't write anything, and just says 'Wrote 0 bytes'. Well, at least that
    bit works . Can anyone show me where I'm going wrong ?

    Thanks in advance -

    Paul

    ------------------
    Zippety Software, Home of the Lynx Project Explorer
    http://www.zippety.net
    My e-mail

  • #2
    Add AllocConsole
    Code:
    #Compile   Exe
    #Dim       All
    #Register  None
    #Include   "win32api.inc"
    Function PbMain() As Long
       Dim mBytes   As Long
       Dim sText    As Asciiz * 10
       AllocConsole
       sText = "Hello"
       WriteFile GetStdHandle(%STD_OUTPUT_HANDLE), sText, Len(sText), mBytes, ByVal %NULL
       MsgBox "Wrote " & Format$(mBytes) & " bytes"
    End Function
    ------------------

    Comment


    • #3
      Great, that's sorted it.

      Thanks a bunch Semen -

      Regards,

      Paul

      ------------------
      Zippety Software, Home of the Lynx Project Explorer
      http://www.zippety.net
      My e-mail

      Comment


      • #4
        + use the 'mkcons' from my site..

        ------------------
        [email protected]
        hellobasic

        Comment


        • #5
          Thanks Edwin, in fact I don't actually need to use a console... I had assumed that
          if you were running a program from the command line in a DOS window, anything written
          to STDOUT would appear in that window. Obviously not.

          This came about because I was testing my first attempt at a CGI program, written with
          PBDLL6. I had run PHP.EXE (ie: PHP4 as a CGI interpreter) with a file name argument
          from the command line, and it did write the output to the DOS box. So although
          my program now works well, I'm puzzled as to why it doesn't write its output to the
          DOS window as the PHP interpreter does.

          Paul

          ------------------
          Zippety Software, Home of the Lynx Project Explorer
          http://www.zippety.net
          My e-mail

          Comment


          • #6
            Paul --
            Following code demonstrates, how PB/DLL module converts itself to console app.
            Name Bas module, for example, "~Tmp.Bas" and start it in PB/DLL IDE.
            Being started, module tests - there is console window or not.
            If not, it cuts first symbol in Exe-Name (~Tmp.Exe -> Tmp.Exe), marks byte 221 as CHR$(3) - console app, and starts "release" version (Tmp.Exe).
            If there is a wish to start a module in Dos Prompt, set %ExitToDosPrompt = %True.
            In command prompt you can start an app by typing Tmp.

            Note, that Tmp.Exe shares console window with command prompt.

            Code:
               #Dim All
               #Compile Exe
               #Include "Win32Api.Inc"
            
               %ExitToDosPrompt = %False
               
               Sub ConvertToConsole
                  Local Path As Asciiz * %MAX_PATH, i As Long, Tmp As String
                  GetModuleFileName GetModuleHandle(ByVal 0&), Path, SizeOf(Path)
                  i = Instr(-1, Path, "\")
                  Open Path For Binary Shared As #1: Get$ #1, Lof(1), Tmp: Close #1
                  Path = Left$(Path, i) + Mid$(Path, i + 2): Mid$(Tmp, 221, 1) = Chr$(3)
                  Open Path For Output As #1: Print #1, Tmp;: Close #1
                  $If %ExitToDosPrompt
                  i = Shell("Command.com")
                  $Else
                  i = Shell(Chr$(34) + Path + Chr$(34) + " " + Command$)
                  $EndIf
               End Sub
               
               Global hConsoleOutput As Long, hConsoleInput As Long
               
               Function PbMain
                  hConsoleOutput = GetStdHandle(%STD_OUTPUT_HANDLE)
                  If hConsoleOutput = 0 Then ConvertToConsole: Exit Function ' From IDE
                  hConsoleOutput = GetStdHandle(%STD_OUTPUT_HANDLE)
                  hConsoleInput  = GetStdHandle(%STD_INPUT_HANDLE)
                  SetConsoleMode hConsoleInput, %NULL
            
                  Dim mBytes As Long
                  Dim sText As Asciiz * 10
                  sText = "Hello"
                  WriteFile GetStdHandle(%STD_OUTPUT_HANDLE), sText, Len(sText), mBytes, ByVal %NULL
                  MsgBox "Wrote " & Format$(mBytes) & " bytes"
               End Function
            ------------------

            Comment


            • #7
              Semen,

              Thanks very much, I follow that, and it now works perfectly.

              It was necessary to change one line though -

              Code:
                If hConsoleOutput = %INVALID_HANDLE_VALUE Then ConvertToConsole: Exit Function ' From IDE
              instead of

              Code:
                If hConsoleOutput = 0...
              Thanks again -

              Regards,

              Paul


              ------------------
              Zippety Software, Home of the Lynx Project Explorer
              http://www.zippety.net
              My e-mail

              Comment


              • #8
                Paul --
                > If hConsoleOutput = %INVALID_HANDLE_VALUE Then
                Not agree, at least, for Win2000.
                %INVALID_HANDLE_VALUE = -1
                Win2000 returns 0.
                About which OS do you talk ?


                ------------------

                Comment


                • #9
                  Semen,

                  Interesting. I'm using Win98SE. When run from the IDE, the ~Tmp.exe code always returns -1
                  (%INVALID_HANDLE_VALUE) on my machine for hConsoleOutput.

                  I wonder if they have redefined INVALID_HANDLE_VALUE under Win2000, or whether GetStdHandle()
                  is simply behaving differently ?



                  ------------------
                  Zippety Software, Home of the Lynx Project Explorer
                  http://www.zippety.net
                  My e-mail

                  Comment


                  • #10
                    Hmm ...
                    I can speculate only.
                    hStdInput and hStdOutput and parts of STARTUPINFO structure.
                    Explorer can use CreateProcess or ShellExecute.
                    If CreateProcess - different Explorer.Exe are able to fill these fields by different way.
                    If ShellExecute - different code for 9x & NT.

                    So, it's necessary to test results under other OSes and to make double test in program (<= 0)




                    ------------------

                    Comment


                    • #11
                      Semen --

                      > (<= 0)

                      Not all negative handle values are invalid! If you use a LONG to store a handle, you will encounter many negative values.

                      Invalid handle values include -1, 0, and the DWORD equivalent of -1, which is &hFFFFFFFF???.

                      -- Eric

                      ------------------
                      Perfect Sync: Perfect Sync Development Tools
                      Email: mailto:[email protected][email protected]</A>

                      "Not my circus, not my monkeys."

                      Comment


                      • #12
                        Eric --
                        If to talk about handles in general - agree, because hBmp, for example, is address in virtual memory.
                        But what means hFile ? It looks like order no.



                        ------------------

                        Comment


                        • #13
                          (Semen, these comments are not aimed at you, but rather at the others that lurk here).

                          It would be unwise to "assume" that file handles are in some type of sequence or could be an index into a table, or will always be signed (positive) values...

                          ie, Microsoft could revise that behavior in a flash, and any application that makes assumptions on these values could be leaving themselves open to compatibility issues.

                          IOW, it is irrelevent what that actual value of a file handle is, as long as you store the value correctly, and use care when comparing API return values...

                          ------------------
                          Lance
                          PowerBASIC Support
                          mailto:[email protected][email protected]</A>
                          Lance
                          mailto:[email protected]

                          Comment

                          Working...
                          X