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

Execute a DOS application from a Windows application.

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

  • Execute a DOS application from a Windows application.

    By default PowerBASIC handles synchronious shell's.
    For executing DOS applications, this is really a good feature.
    The following code creates a console window and executes a DOS application and closes the console again.
    When it returns the output of the DOS application is grabbed from the console window wich you may wish to analyse.
    There's no need for PIF files to close the window afterwards.

    Code:
    #Compile Exe
    #Include "win32api.inc"
     
    Declare Function ConsoleToBuffer() As String
     
    Function PbMain()
     
        '// Prepare a console window
        AllocConsole
     
    #If 0   '< Put to 1 if you wish to see the Edit example instead.
     
        '// Run your DOS application now.
        '// Note, we make use of the synchronious Shell.
        Shell "EDIT.COM"
     
        '// For testing purposes only.
        MsgBox "STOP (This message should be shown AFTER closing EDIT)"
     
    #Else
     
        Shell "Attrib"
     
        '// Now let's get the results by grabbing it of the console window.
        MsgBox Left$( ConsoleToBuffer(), 300 )
     
    #EndIf
     
        '// Remove the console.
        FreeConsole
     
    End Function
     
    '// Retrieves all textlines from the current console window.
    Function ConsoleToBuffer() As String
     
        Dim a       As Long
        Dim hCons   As Long
        Dim Result  As Long
        Dim nBytes  As Long
        Dim sScreen As String
        Dim sBuffer As String
        Dim SBI     As CONSOLE_SCREEN_BUFFER_INFO
     
        '// Get handle to console.
        hCons = GetStdHandle( %STD_OUTPUT_HANDLE )
        If hCons = 0 Then Exit Function
     
        '// Get console size
        Call GetConsoleScreenBufferInfo( hCons, SBI )
     
        '// Loop through all lines.
        For a = 0 To SBI.dwSize.Y - 1
     
            '// Prepare buffer for this line.
            sBuffer = Space$( SBI.dwSize.X )
     
            '// Read the line.
            Result = ReadConsoleOutputCharacter( _
              hCons _
            , ByVal StrPtr( sBuffer ) _
            , Len( sBuffer ) _
            , ByVal MakLng( 0, a ) _
            , nBytes )
     
            If Result Then sScreen = sScreen & Rtrim$( sBuffer ) & $CRLF
     
        Next a
     
        '// Return lines.
        Function = sScreen
     
    End Function
    ------------------
    [email protected]
    hellobasic
Working...
X