Results 1 to 17 of 17

Thread: Extending CMD

  1. #1

    Thread Starter
    Lively Member Goat Spirit's Avatar
    Join Date
    Aug 2006
    Location
    Goat Heaven
    Posts
    105

    Extending CMD

    Hello, I am trying the new msdn 200 express edition but also got VB6, anyone know if there is a way to make a cmd prompt like inside a form, so when running dos apps, it will show the stuff in prompt on your form and not a new cmd prompt?

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Extending CMD

    You would have to get the list of commands from the system. You would also have to define standard IO, which would display input and output.

  3. #3

    Thread Starter
    Lively Member Goat Spirit's Avatar
    Join Date
    Aug 2006
    Location
    Goat Heaven
    Posts
    105

    Re: Extending CMD

    How might I go about getting the list and defineing the IO. I have no idea. =/

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Extending CMD

    You can get the list of commands in My computer, Properties, Advanced, Environment variables. You also have a lot of IO definitions/samples all over the net. All applications which can be run in a command line, base on standard IO. Are you familiar to, i don't know... C's stdio library?

  5. #5

    Thread Starter
    Lively Member Goat Spirit's Avatar
    Join Date
    Aug 2006
    Location
    Goat Heaven
    Posts
    105

    Re: Extending CMD

    No, just started, well anyway hmm is there a way even if I run app in standard cmd, to minimize it but not showing in tray or taskbar or the tasklist thing [CTRL+ATL+DEL] ?

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Extending CMD

    Few people have done what you're after. However, you can familiarize yourself with CygWIN, which does something similar (implements terminal into Windows command line), and it's also available in open-source... i think so

  7. #7

    Thread Starter
    Lively Member Goat Spirit's Avatar
    Join Date
    Aug 2006
    Location
    Goat Heaven
    Posts
    105

    Re: Extending CMD

    http://www.symbolictools.de/public/p...ticles/vba.htm

    I don't know if that ^ will help but I will try it.

  8. #8
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Extending CMD

    VB Code:
    1. 'Purpose     :  Optionally Synchronously runs a DOS command line and returns the captured screen output.
    2. 'Inputs      :  sCommandLine                The DOS command line to run.
    3. '               [bShowWindow]               If True displays the DOS output window.
    4. 'Outputs     :  Returns the screen output
    5. 'Notes       :  This routine will work only with those program that send their output to
    6. '               the standard output device (stdout).
    7. '               Windows NT ONLY.
    8. 'Revisions   :
    9.  
    10. Function ShellExecuteCapture(sCommandLine As String, Optional bShowWindow As Boolean = False, Optional waitFlag As Boolean = True) As String
    11. Const clReadBytes As Long = 256, INFINITE As Long = &HFFFFFFFF
    12. Const STARTF_USESHOWWINDOW = &H1, STARTF_USESTDHANDLES = &H100&
    13. Const SW_HIDE = 0, SW_NORMAL = 1
    14. Const NORMAL_PRIORITY_CLASS = &H20&
    15.  
    16. Const PIPE_CLIENT_END = &H0     'The handle refers to the client end of a named pipe instance. This is the default.
    17. Const PIPE_SERVER_END = &H1     'The handle refers to the server end of a named pipe instance. If this value is not specified, the handle refers to the client end of a named pipe instance.
    18. Const PIPE_TYPE_BYTE = &H0      'The named pipe is a byte pipe. This is the default.
    19. Const PIPE_TYPE_MESSAGE = &H4   'The named pipe is a message pipe. If this value is not specified, the pipe is a byte pipe
    20.  
    21.  
    22. Dim tProcInfo As PROCESS_INFORMATION, lRetVal As Long, lSuccess As Long
    23. Dim tStartupInf As STARTUPINFO
    24. Dim tSecurAttrib As SECURITY_ATTRIBUTES, lhwndReadPipe As Long, lhwndWritePipe As Long
    25. Dim lBytesRead As Long, sBuffer As String
    26. Dim lPipeOutLen As Long, lPipeInLen As Long, lMaxInst As Long
    27.  
    28. tSecurAttrib.nLength = Len(tSecurAttrib)
    29. tSecurAttrib.bInheritHandle = 1&
    30. tSecurAttrib.lpSecurityDescriptor = 0&
    31.  
    32. lRetVal = CreatePipe(lhwndReadPipe, lhwndWritePipe, tSecurAttrib, 0)
    33. If lRetVal = 0 Then
    34.     'CreatePipe failed
    35.     Exit Function
    36. End If
    37.  
    38. tStartupInf.cb = Len(tStartupInf)
    39. tStartupInf.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
    40. tStartupInf.hStdOutput = lhwndWritePipe
    41. If bShowWindow Then
    42.     'Show the DOS window
    43.     tStartupInf.wShowWindow = SW_NORMAL
    44. Else
    45.     'Hide the DOS window
    46.     tStartupInf.wShowWindow = SW_HIDE
    47. End If
    48.  
    49. lRetVal = CreateProcessA(0&, sCommandLine, tSecurAttrib, tSecurAttrib, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, tStartupInf, tProcInfo)
    50. If lRetVal <> 1 Then
    51.     'CreateProcess failed
    52.     Exit Function
    53. End If
    54.  
    55. 'Process created, wait for completion. Note, this will cause your application
    56. 'to hang indefinately until this process completes.
    57. If waitFlag Then WaitForSingleObject tProcInfo.hProcess, INFINITE
    58.  
    59. 'Determine pipes contents
    60. lSuccess = GetNamedPipeInfo(lhwndReadPipe, PIPE_TYPE_BYTE, lPipeOutLen, lPipeInLen, lMaxInst)
    61. If lSuccess Then
    62.     'Got pipe info, create buffer
    63.     sBuffer = String(lPipeOutLen, 0)
    64.     'Read Output Pipe
    65.     lSuccess = ReadFile(lhwndReadPipe, sBuffer, lPipeOutLen, lBytesRead, 0&)
    66.     If lSuccess = 1 Then
    67.         'Pipe read successfully
    68.         ShellExecuteCapture = Left$(sBuffer, lBytesRead)
    69.     End If
    70. End If
    71.  
    72. 'Close handles
    73. Call CloseHandle(tProcInfo.hProcess)
    74. Call CloseHandle(tProcInfo.hThread)
    75. Call CloseHandle(lhwndReadPipe)
    76. Call CloseHandle(lhwndWritePipe)
    77. End Function
    This is a good function.. Let me know if you need Win9x support and I'll try to dig up one I used to use..

  9. #9

    Thread Starter
    Lively Member Goat Spirit's Avatar
    Join Date
    Aug 2006
    Location
    Goat Heaven
    Posts
    105

    Re: Extending CMD

    I have no idea what this does.

  10. #10
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Extending CMD

    This allows you to issue CMD console type commands in a procedure, and it returns what would be displayed by the console in the string variable ShellExecuteCapture that you can read and write to controls like text boxes etc or do any other string operations.. It has options to display the console window or execute the process 100% invisibly, and the option to pause your app's execution until the process completes, or continue your app's execution immediately.. (Continuing immediately will cause the function to return only the CMD output that has been written by the time the function exits BTW)
    You can just put this in your form or in a module, Add whatever API/Type declarations it demands via errors the first time you execute it and you're good to go..

  11. #11

    Thread Starter
    Lively Member Goat Spirit's Avatar
    Join Date
    Aug 2006
    Location
    Goat Heaven
    Posts
    105

    Re: Extending CMD

    So if I put this entire function somewhere where would I put it exactly, and how would I get it to work, just type in richtextbox1 and press enter and it will display crap just like CMD?
    Last edited by Goat Spirit; Aug 19th, 2006 at 09:47 AM.

  12. #12

    Thread Starter
    Lively Member Goat Spirit's Avatar
    Join Date
    Aug 2006
    Location
    Goat Heaven
    Posts
    105

    Re: Extending CMD

    Oh and I use Windows XP so Would it still work?

  13. #13
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Extending CMD

    You should save yourself alot of grief and hit the tutorials..
    The function can be called with 1, 2, or 3 arguments..
    It needs the commandline you wish to run obviously..
    The other 2 can be added, but if they are not there then they will use the default values.
    Defaults:
    Dont pop up a visible command window
    Wait until process completes before continuing running your app.

    egs:
    text1.text = ShellExecuteCapture("dir")
    text1.text = ShellExecuteCapture("dir",true,false)
    text1.text = ShellExecuteCapture("dir",,false)
    text1.text = ShellExecuteCapture("dir",true)
    This uses text2.text as the command to execute:
    text1.text = ShellExecuteCapture(text2.text)

    Here are the necessary declarations.. Put these at the top of the module/form you add the function to.. (Putting it in a module makes reusing it in other apps simpler)
    VB Code:
    1. Private Type SECURITY_ATTRIBUTES
    2.     nLength    As Long
    3.     lpSecurityDescriptor As Long
    4.     bInheritHandle As Long
    5. End Type
    6.  
    7. Private Type STARTUPINFO
    8.     cb         As Long
    9.     lpReserved As Long
    10.     lpDesktop  As Long
    11.     lpTitle    As Long
    12.     dwX        As Long
    13.     dwY        As Long
    14.     dwXSize    As Long
    15.     dwYSize    As Long
    16.     dwXCountChars As Long
    17.     dwYCountChars As Long
    18.     dwFillAttribute As Long
    19.     dwFlags    As Long
    20.     wShowWindow As Integer
    21.     cbReserved2 As Integer
    22.     lpReserved2 As Long
    23.     hStdInput  As Long
    24.     hStdOutput As Long
    25.     hStdError  As Long
    26. End Type
    27.  
    28. Private Type PROCESS_INFORMATION
    29.     hProcess   As Long
    30.     hThread    As Long
    31.     dwProcessID As Long
    32.     dwThreadID As Long
    33. End Type
    34.  
    35. Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As Any, ByVal nSize As Long) As Long
    36. Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
    37. Private Declare Function GetNamedPipeInfo Lib "kernel32" (ByVal hNamedPipe As Long, lType As Long, lLenOutBuf As Long, lLenInBuf As Long, lMaxInstances As Long) As Long
    38. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    39. Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As Any, lpProcessInformation As Any) As Long
    40. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    41. Private Const STARTF_USESHOWWINDOW As Long = &H1&
    42. Private Const SW_HIDE As Integer = 0&

  14. #14
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Extending CMD

    Yes, Win2K, XP, Server2003 are todays common WINNT's

  15. #15

    Thread Starter
    Lively Member Goat Spirit's Avatar
    Join Date
    Aug 2006
    Location
    Goat Heaven
    Posts
    105

    Re: Extending CMD

    Okay it seems to compile okay, but it won't show theinfo for "dir" in Text1.Text, any ideas?

  16. #16
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Extending CMD

    Post your code..
    Also, add the line:
    debug.print ShellExecuteCapture("dir")
    right above where you used it in your form code.. What you see returned in the immediate window is what text1.text would be assigned.. Perhaps its a size limitation that another user knows more about than I, or maybe I pooched something.. Who knows..

  17. #17

    Thread Starter
    Lively Member Goat Spirit's Avatar
    Join Date
    Aug 2006
    Location
    Goat Heaven
    Posts
    105

    Re: Extending CMD

    What in the hell, I think it's doing something because the default text disippears but wont display the dir output info like on CMD prompt when you type dir it shows the dir's but wont show anything for me. in my Text1.Text...

    VB Code:
    1. Text1.Text = ShellExecuteCapture("dir", False, True)
    2. Debug.Print ShellExecuteCapture("dir")

    Help?

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