Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i read a DOS Window results 3

Status
Not open for further replies.

skynetosiris

Programmer
Jun 2, 2003
15
MX
Hi

I run this command in a DOS Window via visual basic:


shell (ssh apsu0001 /opt/apvadmin/bin/HpAddMySql param1, param2,1)

Everything is ok BUT i would like to use the results that the shell are presenting in the DOS window (i mean take the screen results and put it on a textbox).

is that posible?

thanks in advance



 
What you can do is have the shell call a batch file that does what you want but you can have the batch file pump its output to a text file then you can read that text file while it is being created. Not the best situation.

I'd go for a more intergrated solution, and have 8).

have you looked at PuTTY? It is a free Telnet/SSH client.
God I hate saying this. But I can't give you the code we have as its not based on Open Source and I don't own the IP. But for your project you might be able to use the PuTTY code and write a small ATL Component wrapper around it and then use that in your VB code.
 
Try this, where command is the DOS thing you want...

Set objExecObject = objShell.Exec("cmd /c command")
do until objExecObject.StdOut.AtEndOfStream
strNextLine = split(AobjExecObject.StdOut.ReadAll, vbCrLf)


//Peter
 
I have done this for a shell-ed DOS app before, where the DOS app's output was all to stdout, by re-directing the output to a file and then reading the file and displaying it in multi-line text box on my form. I made the text box white-on black, with CourierNew Bold for the font so it kinda looked like DOS output for the user's benefit.
Code:
shell ("myprog /some /parameters >c:\temp\myprog.out", vbNormalFocus)
This followed by reading the output and displaying it in the text box works for my app.

- Chuck Somerville
 
SemperFiDownUnda:

About this:

<< have you looked at PuTTY? It is a free Telnet/SSH client.
God I hate saying this. But I can't give you the code we have as its not based on Open Source and I don't own the IP. But for your project you might be able to use the PuTTY code and write a small ATL Component wrapper around it and then use that in your VB code.>>

How could you implement this solution?

Thanks in advance

 
If you want full redirection to and from other applications that are using a console, the code I gave (plus full walkthough) in Thread222-255803 may be of interest.
 
TO Peter Westling

I think i don't fully understand your solution, how could you implement that?, i mean i just put the code that you type here in the loadform? or how does that work?

Thanks in advance
 
I have a .exe that I run and retrieve the results by doing the following. I don't know if this soulution will work for you but it may help so here goes...

1) Create a form with a command button (Command1) and a Textbox (Text1)

2) Paste the following into a module:


Option Explicit

Private Declare Function CreatePipe Lib &quot;kernel32&quot; ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As Any, _
ByVal nSize As Long) As Long

Private Declare Function ReadFile Lib &quot;kernel32&quot; ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Any) As Long

Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
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 Declare Function CreateProcessA Lib &quot;kernel32&quot; (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

Private Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal hObject As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Const STARTF_USESHOWWINDOW = &H1
Private Const SW_HIDE = 0

Public Function ExecuteApp(sCmdline As String) As String
Dim proc As PROCESS_INFORMATION, ret As Long
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim hReadPipe As Long 'The handle used to read from the pipe.
Dim hWritePipe As Long 'The pipe where StdOutput and StdErr will be redirected to.
Dim sOutput As String
Dim lngBytesRead As Long, sBuffer As String * 256

sa.nLength = Len(sa)
sa.bInheritHandle = True

ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then
MsgBox &quot;CreatePipe failed. Error: &quot; & Err.LastDllError
Exit Function
End If

start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
' Redirect the standard output and standard error to the same pipe
start.hStdOutput = hWritePipe
start.hStdError = hWritePipe
start.wShowWindow = SW_HIDE

' Start the shelled application:
' if you program has to work only on NT you don't need the &quot;conspawn &quot;
ret = CreateProcessA(0&, sCmdline, sa, sa, True, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
If ret = 0 Then
MsgBox &quot;CreateProcess failed. Error: &quot; & Err.LastDllError
Exit Function
End If

' The handle wWritePipe has been inherited by the shelled application
' so we can close it now
CloseHandle hWritePipe

' Read the characters that the shelled application
' has outputed 256 characters at a time
Do
ret = ReadFile(hReadPipe, sBuffer, 256, lngBytesRead, 0&)
sOutput = sOutput & Left$(sBuffer, lngBytesRead)
Loop While ret <> 0 ' if ret = 0 then there is no more characters to read

CloseHandle proc.hProcess
CloseHandle proc.hThread
CloseHandle hReadPipe

ExecuteApp = sOutput ' Return the output of shelled program.
End Function



3) Insert the following into your form


Private Sub Command1_Click()
Text1 = ExecuteApp(&quot;C:\WhateverFile.exe&quot;)
End Sub
 
Well done ca8msm!
Nice job done.
star.gif
 
Your solution is nice but whenever i run it with this

ssh -root apsu0001 /opt/apvadmin/bin/HpAddMySql JMG pass NT

the application hangs

you know why?

Thanks in advance

Javier
 
How can i add a method to this code to stop the application if the external program hangs?

Thanks in advance
 
skynetosiris, you are asking similar questions in different threads which create confusions for readers.

See thread222-569768 for answer to your question.
Also see faq222-2244 for getting best answers, particulary section 5.
 
Okay, I get pushing the output to a text file, but HOW do you get the TEXT file info into the textboxes in the app??? Please let me know. Thanks.
 
ahoover
Try reading faq222-2244 for some guides on forum etiquette, and on getting the best answers to your questions.

For this question, follow up strongm's link above, where you'll find your answer.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
csomervi

I've read what you posted, tried it and for some reason it seems to execute the file but throws nothing in the directory I tell it to. The command I am using is:

Shell ("C:\Analyser_Output\getmac.exe >C:\Analyser_Output\Output_Data\MAC.txt"), vbNormalFocus

Any suggestions...?


-----------------
Cogito Ergo Sum
-----------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top