Results 1 to 5 of 5

Thread: command line (new send)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    London
    Posts
    17

    Question command line (new send)

    hi ,

    i have created a small interface to interact and call command line calls....thats all fine, which it does, executes then closes the cmd screen...however, i want to dedirect the return message from DOS to my program..

    an example is if i executed a command thru my VB program which executed in DOS, it would then, after completion, display in the DOS wondow
    'command successful' then close....

    i want to somehow get that message into my Vb program as the execution is so fast that there is no other way of telling whether the command was successful

    thanks a lot if anyone has any ideas
    Sssshwwweeeeeeeeeeeet

    DeeMan

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    This doesn't actually have anything to do with DOS, but I think its what you want:

    Usage:
    Text1.Text = RunCommand("ipconfig /all")

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

  3. #3
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Josh...

    I have tried using your code, and I cannot get it to work. I am trying to use xcopy and it simply will not work correctly. I do not understand why. I use RunCommand("xcopy c:\test1 c:\test2"); the code hangs when I wait for output. When I comment out all of the code associated with output, it does not hang, but nothing is ever moved. Any help would be much appreciated.

    Joe

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    London
    Posts
    17
    thanks for that josh will try it out as soon as my workload here simmers down
    Sssshwwweeeeeeeeeeeet

    DeeMan

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    London
    Posts
    17
    hey josh,
    that code worked perfectly! thanks for that! now i can sit back secure in the knowledge that my commands are actually executing and havent disappeared thru a black hole!

    i was previously using a shell call from VB but this code (although slightly more complex) gives more options and if anyone else is reaidng this, use this code instead of VB shell calls!

    thanks again
    Sssshwwweeeeeeeeeeeet

    DeeMan

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