Results 1 to 8 of 8

Thread: capture shell output

  1. #1

    Thread Starter
    Junior Member ravi_sekhar's Avatar
    Join Date
    Jan 2002
    Posts
    31

    Thumbs up capture shell output

    Hi,
    Can we by any way capture the output generated by shell command. For eg., if i we run a dos command using shell command which produces a short output, can we capture that in a text pad or something............ The operator ">" which works at command prompt is not working in shell command.
    Cheer's,
    Ravi.

  2. #2

    Thread Starter
    Junior Member ravi_sekhar's Avatar
    Join Date
    Jan 2002
    Posts
    31

    Contd.,

    actually iam working on clearcase a tool for Software Configuration Management. To get the list of all the label types iam running a shell command as ,

    shell("cleartool lstype -kind lbtype -s > c:\test.txt")

    Can i any how capture this output for further use.....
    Cheer's,
    Ravi.

  3. #3
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Try this out:

    Usage:
    strString = RunCommand("ipconfig")

    VB Code:
    1. Option Explicit
    2. Option Base 0
    3. 'code by JoshT
    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.

  4. #4
    Junior Member jolivo's Avatar
    Join Date
    Nov 2001
    Location
    Cincinnati, Ohio
    Posts
    30

    Unhappy

    JoshT,

    Your function always locks up on me when attempting the following line of code:

    ReadFile(hRead, sBuffer(0), 64, lgSize, ByVal 0&)

    I have to kill VB6 and re-open the project. I know your code works because I've tried it with simple batch files, but I can't seem to figure out why ReadFile() keeps locking up on me when attempting to read the output of a particular executable (I'm trying to check-out files from a StarTeam database using the command-line interface and I want to capture the results).

    Is there anything you can tell me about ReadFile and how I might be using it improperly that would be causing the lock-ups? I know I'm on the right track, but I just can't get it to work...Is there another way of capturing the StdOut (aside from piping it into another file) using the techniques in your code?

    Thanks!

  5. #5
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    The Netherlands
    Posts
    403
    But how will it get info from hStdError?

    I mean, I've read that some programs write to that handle and while it is displayed on the DosConsole this function won't give you that output

    Anyone knows this?
    There are 10 types of people, those who understand binary and those who don't

    http://merlijn.beyonix.net

  6. #6
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    The Netherlands
    Posts
    403
    Okay that was simple, I fixed it myself

    I wanted to ask if I could send commands to the Console trrough hstdInput but I guess I'll try myself first
    There are 10 types of people, those who understand binary and those who don't

    http://merlijn.beyonix.net

  7. #7
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Originally posted by Xa0z
    Okay that was simple, I fixed it myself

    I wanted to ask if I could send commands to the Console trrough hstdInput but I guess I'll try myself first
    There is no "Console" to send anything to unless you call Console APIs to make one yourself first. The point of that code is to bypass cmd.exe or Consoles and directly get the output into your program.

    If you want to get a VB program to be able to run from cmd.exe and write output back to cmd.exe, you need to binary patch your compiled executable with a "mkconsole.exe" program found on the net. I think VB compiles as "Win32 GUI", rather than "Win32 Console", as it is in VC++.
    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.

  8. #8
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    The Netherlands
    Posts
    403
    I know that, (that there's no console) but the rest was very informative.

    I just thought, perhaps I could send commands via that pipe, but it seems a little impossible
    Last edited by Xa0z; Nov 6th, 2002 at 11:27 AM.
    There are 10 types of people, those who understand binary and those who don't

    http://merlijn.beyonix.net

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