Results 1 to 19 of 19

Thread: DOS in a textbox

  1. #1
    conquerdude
    Guest

    Question DOS in a textbox

    How can I run a dos program and output what it does into a textbox control in my VB app instead of the console window. I've seen porgrams do this before and it would be great if one of you could tell me how.

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526
    I'm not sure that you can get the output directly to VB, but what you can do is re-direct the output to a text file, then read in that file. Something like this:

    Shell "DIR C:\*.* > DIRTEXT.TXT", vbMinimizedNoFocus
    txtTextBox1.Text = vbNullString
    Open "DIRTEXT.TXT" For Input As #1
    Do While Not EOF(1)
    Line Input #1, strTemp1
    txtTextBox1.Text = txtTextBox1.Text & strTemp1 & vbCrLf
    Loop
    Close #1

  3. #3
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    there was a recent sumbistion to www.pscode.com/vb about that
    retired member. Thanks for everything

  4. #4
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Usage: Text1.Text = RunCommand("ipconfig")
    VB Code:
    1. Option Explicit
    2. Option Base 0
    3. 'Code written by JoshT.  Use at your own risk :)
    4.  
    5. Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" _
    6.    (ByVal lpApplicationName As String, _
    7.     ByVal lpCommandLine As String, _
    8.     lpProcessAttributes As SECURITY_ATTRIBUTES, _
    9.     lpThreadAttributes As SECURITY_ATTRIBUTES, _
    10.     ByVal bInheritHandles As Long, _
    11.     ByVal dwCreationFlags As Long, _
    12.     lpEnvironment As Any, _
    13.     ByVal lpCurrentDirectory As String, _
    14.     lpStartupInfo As STARTUPINFO, _
    15.     lpProcessInformation As PROCESS_INFORMATION) As Long
    16.  
    17. Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    18.  
    19. Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _
    20.     lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, _
    21.     lpOverlapped As Long) As Long
    22.  
    23. Private Declare Function WaitForSingleObject Lib "kernel32" _
    24.     (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    25.  
    26. Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, _
    27.     phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, _
    28.     ByVal nSize As Long) As Long
    29.  
    30. Private Type STARTUPINFO
    31.         cb As Long
    32.         lpReserved As String
    33.         lpDesktop As String
    34.         lpTitle As String
    35.         dwX As Long
    36.         dwY As Long
    37.         dwXSize As Long
    38.         dwYSize As Long
    39.         dwXCountChars As Long
    40.         dwYCountChars As Long
    41.         dwFillAttribute As Long
    42.         dwFlags As Long
    43.         wShowWindow As Integer
    44.         cbReserved2 As Integer
    45.         lpReserved2 As Long
    46.         hStdInput As Long
    47.         hStdOutput As Long
    48.         hStdError As Long
    49. End Type
    50.  
    51. Private Type PROCESS_INFORMATION
    52.         hProcess As Long
    53.         hThread As Long
    54.         dwProcessId As Long
    55.         dwThreadId As Long
    56. End Type
    57.  
    58. Private Type SECURITY_ATTRIBUTES
    59.         nLength As Long
    60.         lpSecurityDescriptor As Long
    61.         bInheritHandle As Long
    62. End Type
    63.  
    64. Private Const NORMAL_PRIORITY_CLASS As Long = &H20&
    65.  
    66. Private Const STARTF_USESTDHANDLES As Long = &H100&
    67. Private Const STARTF_USESHOWWINDOW As Long = &H1&
    68. Private Const SW_HIDE As Long = 0&
    69.  
    70. Private Const INFINITE As Long = &HFFFF&
    71.  
    72. Public Function RunCommand(CommandLine As String) As String
    73.     Dim si As STARTUPINFO 'used to send info the CreateProcess
    74.     Dim pi As PROCESS_INFORMATION 'used to receive info about the created process
    75.     Dim retval As Long 'return value
    76.     Dim hRead As Long 'the handle to the read end of the pipe
    77.     Dim hWrite As Long 'the handle to the write end of the pipe
    78.     Dim sBuffer(0 To 63) As Byte 'the buffer to store data as we read it from the pipe
    79.     Dim lgSize As Long 'returned number of bytes read by readfile
    80.     Dim sa As SECURITY_ATTRIBUTES
    81.     Dim strResult As String 'returned results of the command line
    82.    
    83.     'set up security attributes structure
    84.     With sa
    85.         .nLength = Len(sa)
    86.         .bInheritHandle = 1& 'inherit, needed for this to work
    87.         .lpSecurityDescriptor = 0&
    88.     End With
    89.    
    90.     'create our anonymous pipe an check for success
    91.     '   note we use the default buffer size
    92.     '   this could cause problems if the process tries to write more than this buffer size
    93.     retval = CreatePipe(hRead, hWrite, sa, 0&)
    94.     If retval = 0 Then
    95.         Debug.Print "CreatePipe Failed"
    96.         RunCommand = ""
    97.         Exit Function
    98.     End If
    99.    
    100.     'set up startup info
    101.     With si
    102.         .cb = Len(si)
    103.         .dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW 'tell it to use (not ignore) the values below
    104.         .wShowWindow = SW_HIDE
    105. '        .hStdInput = GetStdHandle(STD_INPUT_HANDLE)
    106.         .hStdOutput = hWrite 'pass the write end of the pipe as the processes standard output
    107. '        .hStdError = GetStdHandle(STD_ERROR_HANDLE)
    108.     End With
    109.    
    110.     'run the command line and check for success
    111.     retval = CreateProcess(vbNullString, _
    112.                             CommandLine & vbNullChar, _
    113.                             sa, _
    114.                             sa, _
    115.                             1&, _
    116.                             NORMAL_PRIORITY_CLASS, _
    117.                             ByVal 0&, _
    118.                             vbNullString, _
    119.                             si, _
    120.                             pi)
    121.     If retval Then
    122.         'wait until the command line finishes
    123.         '   trouble if the app doesn't end, or waits for user input, etc
    124.         WaitForSingleObject pi.hProcess, INFINITE
    125.        
    126.         'read from the pipe until there's no more (bytes actually read is less than what we told it to)
    127.         Do While ReadFile(hRead, sBuffer(0), 64, lgSize, ByVal 0&)
    128.             'convert byte array to string and append to our result
    129.             strResult = strResult & StrConv(sBuffer(), vbUnicode)
    130.             'TODO = what's in the tail end of the byte array when lgSize is less than 64???
    131.             Erase sBuffer()
    132.             If lgSize <> 64 Then Exit Do
    133.         Loop
    134.        
    135.         'close the handles of the process
    136.         CloseHandle pi.hProcess
    137.         CloseHandle pi.hThread
    138.     Else
    139.         Debug.Print "CreateProcess Failed" & vbCrLf
    140.     End If
    141.    
    142.     'close pipe handles
    143.     CloseHandle hRead
    144.     CloseHandle hWrite
    145.    
    146.     'return the command line output
    147.     RunCommand = Replace(strResult, vbNullChar, "")
    148. End Function
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Thumbs up

    Kudos Kudos and Bravo to JoshT.

    Thats some slick code dude!!!!!

    Not only did I run it with ipconfig, but I also ran it with ping and it worked like a champ.

    Your routine will make another fine addition to my code library, with said code properly documented with respect to author, of course.

  6. #6
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Yeah, no problem. Spread it to those who need it.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by JoshT
    Yeah, no problem. Spread it to those who need it.
    Wow.
    Just... wow
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8
    Addicted Member cbond's Avatar
    Join Date
    Apr 2001
    Location
    AZ - USA
    Posts
    148

    Thank you.

    Josh, Thank you. Very well written and save me untold hours tyring to map a network that had no documentation at all. Was able to resolve all computer names to IP addresses quickly and easily.

    Thank you!

  9. #9
    New Member
    Join Date
    May 2002
    Location
    London
    Posts
    4

    doesnt work in Win 2000

    that code waits for the whole program to finish and then copies stuff. is there a way of catching the text from DOS shell while the program is running in DOS?

    and also the above code doesnt work with Win 2000. The VB program just hangs. is there a difference in Win 2000?

    thanks
    mohith


    be the change that you want to see in the world

  10. #10
    Addicted Member chicocouk's Avatar
    Join Date
    Sep 2001
    Posts
    207
    The code does work in windows 2000, because that's where I'm using it. And by the by, it's great code, and impresses the hell out of me

    MCSE, Mcp+I, Unicenter Engineer

  11. #11
    Addicted Member cbond's Avatar
    Join Date
    Apr 2001
    Location
    AZ - USA
    Posts
    148

    Works in XP too.

    I'm running in XP, it works great.

    Double check your DOS program. When it completes, to it say "press any key to continue"? If it does they your VB app will way for you DOS app to end, which is waiting for you to press a key... but the window is probably hidden and you can't press a key... (found that one out the hard way).

  12. #12
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032

    Re: Works in XP too.

    Originally posted by cbond
    I'm running in XP, it works great.

    Double check your DOS program. When it completes, to it say "press any key to continue"? If it does they your VB app will way for you DOS app to end, which is waiting for you to press a key... but the window is probably hidden and you can't press a key... (found that one out the hard way).
    I want to explain my code better, as it doesn't work the way people seem to think.

    I wrote the code on a Windows 2000 box. It does not actually have anything to do with DOS - people mistakenly think typing the name of a Win32 *.exe is some kind of command when all Windows is doing is running the app with its standard IO stream set to the command prompt window (created by cmd.exe) - with my code, there is no hidden window, the console window was never created in the first place. Your VB app reads the spawned app's standard output with my code, rather than cmd.exe reading it and then writing it to the console window. So if the app does need user input, it is possible to write input to the spawned app's standard input stream (accessible via File API, like everything else).
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  13. #13
    New Member
    Join Date
    May 2002
    Location
    London
    Posts
    4
    i ran it in win 98 with same input and same program, it worked. but the same thing in Win 2000, just hangs. it doesnt crash, no error. the program doesnt respond at all. any difference?

    it hangs when its calling the ReadFile API function. is it because win 2000 uses Overlapped type as the last argument for ReadFile rather than Long type?

    For some reason, it doesnt work.

    also is there a way to copy it real time for DOS rather than waiting for it to finish?

    cheers
    mohith
    be the change that you want to see in the world

  14. #14
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Try running "ipconfig" or a different *.exe with it - certain apps might output text in different ways. ByVal 0& should be the same as passing NULL for the Overlapped structure.

    To do it in real time - try call ReadFile before using WaitForSingleObject (or let the time less than INFINITE, etc).
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  15. #15
    New Member
    Join Date
    Jul 2000
    Location
    Malvern, PA, USA
    Posts
    12

    Thumbs up Wow

    Attrib [path/filename] works great too!
    Great code JoshT!

  16. #16
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    Looks good thanks for the cool code
    -------------------------
    My name says it all!

  17. #17
    Member FrogBoy666's Avatar
    Join Date
    Aug 2000
    Location
    Columbia, SC
    Posts
    34

    I'm having a bit of a problem with the code...

    When I run the exe, the first few lines are displayed in the textbox immediately, but as the SLOW exe churns away, the lines that appear after the first few aren't added to the textbox.

    Is there any way to correct this?

  18. #18
    New Member
    Join Date
    Apr 2002
    Location
    The Netjerlands
    Posts
    4

    Thumbs up Great Job!



    Very cool programming JoshT

  19. #19
    Member FrogBoy666's Avatar
    Join Date
    Aug 2000
    Location
    Columbia, SC
    Posts
    34

    Possibility...

    Like I said, the code works great for the first few lines, then the console app pauses (with a blinking cursor), not waiting for user input, but just doing some calculations... this is where the above code stops reading the output.

    I guess it could be very useful for other things, but not this one... unless anyone has any suggestions...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width