It's a fact - APIs have replaced many of the DOS Functions, and yet there are still people who consider dos a powerful tool, after all .net provides the ability to make console applications.

So, i created a simple way, without messing around with console opening and manipulation, of sending and recieving the results of DOS commands in VB6.

Sending a DOS Command:

Now for most vb programmers, sending a command to DOS is easy as pie. However, if you want to receive a command from DOS, you need to wait for the command to finish executing, right? So i use this code to send a command to DOS and wait for it to finish. Some of it can be found on MSDN btw.

VB Code:
  1. Option Explicit
  2. Private Type STARTUPINFO
  3.       cb As Long
  4.       lpReserved As String
  5.       lpDesktop As String
  6.       lpTitle As String
  7.       dwX As Long
  8.       dwY As Long
  9.       dwXSize As Long
  10.       dwYSize As Long
  11.       dwXCountChars As Long
  12.       dwYCountChars As Long
  13.       dwFillAttribute As Long
  14.       dwFlags As Long
  15.       wShowWindow As Integer
  16.       cbReserved2 As Integer
  17.       lpReserved2 As Long
  18.       hStdInput As Long
  19.       hStdOutput As Long
  20.       hStdError As Long
  21.    End Type
  22. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  23.    Private Type PROCESS_INFORMATION
  24.       hProcess As Long
  25.       hThread As Long
  26.       dwProcessID As Long
  27.       dwThreadID As Long
  28.    End Type
  29. Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  30.    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
  31.       hHandle As Long, ByVal dwMilliseconds As Long) As Long
  32.  
  33.    Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
  34.       lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
  35.       lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
  36.       ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
  37.       ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
  38.       lpStartupInfo As STARTUPINFO, lpProcessInformation As _
  39.       PROCESS_INFORMATION) As Long
  40.  
  41.    Private Declare Function CloseHandle Lib "kernel32" _
  42.       (ByVal hObject As Long) As Long
  43.  
  44.    Private Declare Function GetExitCodeProcess Lib "kernel32" _
  45.       (ByVal hProcess As Long, lpExitCode As Long) As Long
  46.  
  47.    Private Const NORMAL_PRIORITY_CLASS = &H20&
  48.    Private Const INFINITE = -1&
  49.  
  50.    Public Function ExecCmd(cmdline$)
  51.       Dim proc As PROCESS_INFORMATION
  52.       Dim start As STARTUPINFO
  53.  
  54.       ' Initialize the STARTUPINFO structure:
  55.       start.cb = Len(start)
  56.  
  57.       ' Start the shelled application:
  58.       ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
  59.          NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)
  60.        DoEvents
  61.         Dim sysbuffer As String
  62.         Dim sysbuffersize As Long
  63.          GetSystemDirectory sysbuffer, sysbuffersize
  64.  
  65.       ' Wait for the shelled application to finish:
  66.          ret& = WaitForSingleObject(proc.hProcess, INFINITE)
  67.          Call GetExitCodeProcess(proc.hProcess, ret&)
  68.          Call CloseHandle(proc.hThread)
  69.          Call CloseHandle(proc.hProcess)
  70.          ExecCmd = ret&
  71.  
  72.    End Function
  73.  
  74. Function SendDOSCommand(command as string, destlog as string)
  75.  
  76. 'i know that using bat files is a strange way to do it, but i find it works better
  77.  
  78.      Open "C:\Dosemu.bat" for output as #1
  79.               Print #1, command + " > " + destlog
  80.  
  81. 'the  > destlog part is the key to the whole thing, since it forces dos to copy it's results to the specifyed file
  82.  
  83.      Close
  84.  
  85.  
  86.  ExecCmd ("C:\dosemu.bat")
  87. 'this line waits for the command to finish executing
  88. Kill ("C:\dosemu.bat")
  89. 'and this gets rid of the temp bat file
  90.  
  91. End Function


So thats that for sending commands, now for retrieving them. In the code above, the dos results have been saved to a file, called destlog.

To get them back is simple. Just Read the File! Create a textbox and call it dosbox, with multiline enabled.

VB Code:
  1. Sub getdosresults(doslog as string)
  2.  
  3. Open doslog For Input As #1
  4.     On Error GoTo skipfile
  5.         Do
  6.         Input #1, buffer
  7.         dosbox.Text = dosbox.Text + buffer + vbCrLf
  8.      'this line gets each line from the file and adds a next line character
  9.         Loop
  10.        
  11. skipfile:
  12. Close
  13.  
  14. End Sub