|
-
Mar 14th, 2002, 07:18 PM
#1
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.
-
Mar 14th, 2002, 07:45 PM
#2
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
-
Mar 14th, 2002, 08:56 PM
#3
Frenzied Member
there was a recent sumbistion to www.pscode.com/vb about that
retired member. Thanks for everything 
-
Mar 15th, 2002, 11:17 AM
#4
Black Cat
Usage: Text1.Text = RunCommand("ipconfig")
VB Code:
Option Explicit
Option Base 0
'Code written by JoshT. Use at your own risk :)
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" _
(ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
lpProcessAttributes As SECURITY_ATTRIBUTES, _
lpThreadAttributes As SECURITY_ATTRIBUTES, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
lpEnvironment As Any, _
ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _
lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, _
lpOverlapped As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, _
phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, _
ByVal nSize As Long) As Long
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const NORMAL_PRIORITY_CLASS As Long = &H20&
Private Const STARTF_USESTDHANDLES As Long = &H100&
Private Const STARTF_USESHOWWINDOW As Long = &H1&
Private Const SW_HIDE As Long = 0&
Private Const INFINITE As Long = &HFFFF&
Public Function RunCommand(CommandLine As String) As String
Dim si As STARTUPINFO 'used to send info the CreateProcess
Dim pi As PROCESS_INFORMATION 'used to receive info about the created process
Dim retval As Long 'return value
Dim hRead As Long 'the handle to the read end of the pipe
Dim hWrite As Long 'the handle to the write end of the pipe
Dim sBuffer(0 To 63) As Byte 'the buffer to store data as we read it from the pipe
Dim lgSize As Long 'returned number of bytes read by readfile
Dim sa As SECURITY_ATTRIBUTES
Dim strResult As String 'returned results of the command line
'set up security attributes structure
With sa
.nLength = Len(sa)
.bInheritHandle = 1& 'inherit, needed for this to work
.lpSecurityDescriptor = 0&
End With
'create our anonymous pipe an check for success
' note we use the default buffer size
' this could cause problems if the process tries to write more than this buffer size
retval = CreatePipe(hRead, hWrite, sa, 0&)
If retval = 0 Then
Debug.Print "CreatePipe Failed"
RunCommand = ""
Exit Function
End If
'set up startup info
With si
.cb = Len(si)
.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW 'tell it to use (not ignore) the values below
.wShowWindow = SW_HIDE
' .hStdInput = GetStdHandle(STD_INPUT_HANDLE)
.hStdOutput = hWrite 'pass the write end of the pipe as the processes standard output
' .hStdError = GetStdHandle(STD_ERROR_HANDLE)
End With
'run the command line and check for success
retval = CreateProcess(vbNullString, _
CommandLine & vbNullChar, _
sa, _
sa, _
1&, _
NORMAL_PRIORITY_CLASS, _
ByVal 0&, _
vbNullString, _
si, _
pi)
If retval Then
'wait until the command line finishes
' trouble if the app doesn't end, or waits for user input, etc
WaitForSingleObject pi.hProcess, INFINITE
'read from the pipe until there's no more (bytes actually read is less than what we told it to)
Do While ReadFile(hRead, sBuffer(0), 64, lgSize, ByVal 0&)
'convert byte array to string and append to our result
strResult = strResult & StrConv(sBuffer(), vbUnicode)
'TODO = what's in the tail end of the byte array when lgSize is less than 64???
Erase sBuffer()
If lgSize <> 64 Then Exit Do
Loop
'close the handles of the process
CloseHandle pi.hProcess
CloseHandle pi.hThread
Else
Debug.Print "CreateProcess Failed" & vbCrLf
End If
'close pipe handles
CloseHandle hRead
CloseHandle hWrite
'return the command line output
RunCommand = Replace(strResult, vbNullChar, "")
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.
-
Mar 15th, 2002, 11:37 AM
#5
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.
-
Mar 15th, 2002, 12:01 PM
#6
Black Cat
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.
-
Mar 15th, 2002, 12:07 PM
#7
Retired VBF Adm1nistrator
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]
-
May 21st, 2002, 11:54 AM
#8
Addicted Member
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!
-
Jun 11th, 2002, 10:13 AM
#9
New Member
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
-
Jun 11th, 2002, 10:51 AM
#10
Addicted Member
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
-
Jun 11th, 2002, 11:11 AM
#11
Addicted Member
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).
-
Jun 11th, 2002, 12:20 PM
#12
Black Cat
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.
-
Jun 11th, 2002, 03:54 PM
#13
New Member
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
-
Jun 12th, 2002, 12:09 PM
#14
Black Cat
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.
-
Jun 12th, 2002, 01:44 PM
#15
New Member
Wow
Attrib [path/filename] works great too!
Great code JoshT!
-
Jun 28th, 2002, 05:58 AM
#16
Addicted Member
Looks good thanks for the cool code
-------------------------
My name says it all!
-
Jul 11th, 2002, 09:53 AM
#17
Member
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?
-
Jul 12th, 2002, 04:11 AM
#18
New Member
Great Job!
Very cool programming JoshT
-
Jul 12th, 2002, 11:57 AM
#19
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|