Link to home
Start Free TrialLog in
Avatar of webzila
webzila

asked on

How to run MS-DOS Utilities within a VB program and store result in a string (not a text file)?

Language: Visual Basic 6
I am looking for VB code to run MS-DOS commands (such as ipconfig, tracert, etc...) from within the visual basic program and store the result in a string...
Note: I know how to run an MS-DOS command from within VB, write the output to a text file and display those results but that is NOT what I am looking for here.

I have done it before. Here is the program that contains the code to run things like ipconfig, tracert, ping, etc...from within VB without opening up the MS-DOS window and without writing to a text file:
http://www.webzila.com/utilities/iplookup/

I dont remember the code I used and have also lost the source code (my fault) so I am desperately searching for that piece of code that I used to perform those functions.
I remember it was something very short and simple and saved the result of the ms-dos command to a string and then I displayed the string in the text box...

Can anyone help me with that code.

Thank you
Avatar of Mikal613
Mikal613
Flag of United States of America image

Try this. add a command button,and 2 Textboxes. Textbox2 should have the MultiLine Property set to True. Type the command in Textbox 1 and the results should appear in Textbox2 when you click the command button.Hope this helps:

Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400

Private Function ShellWait(PathName, Optional WindowStyle As VbAppWinStyle = vbNormalFocus) As Double
Dim hProcess As Long, RetVal As Long
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(PathName, WindowStyle))
    Do
        GetExitCodeProcess hProcess, RetVal
        DoEvents: Sleep 100
    Loop While RetVal = STILL_ACTIVE
End Function

Private Sub Command1_Click()
Dim TmpFile As String, Ln As String, Result As String
Dim ff As Integer
Result = ""
TmpFile = Environ("Temp") & "\tmpcmd.tmp"
' shell & wait for cmd to complete,redirecting results to a tmp file
ShellWait Environ("Comspec") & " /c " & Text1.Text & " > " & TmpFile, vbHide

' open tmpfile and get data into Result string
ff = FreeFile
Open TmpFile For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
If Result = "" Then
    Result = Ln
Else
    Result = Result & vbCrLf & Ln
End If
Loop
Close #ff

' make sure Text2 has the MultiLine property set to True
Text2.Text = Result

End Sub
Avatar of webzila
webzila

ASKER

Thats basically the way that I know how to do it. Its just instead of writing the command line result to a txt file you are writing it to a tmp file and then reading and storing the results of that file into a string...

The way I have done it before is that the result of the ms-dos utility went straight into a string. I didnt write any results to any file, it went straight to the string which was then displayed in a text box..

Also I recall the command to do that was really short...I think 1 or 2 lines total..

Check out that url I posted to the program which contains that code...as you can see it doesnt save any files to your computer.

Thanks for replying though.
SOLUTION
Avatar of Mikal613
Mikal613
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("%comspec% /c ipconfig.exe")

Do Until objExecObject.StdOut.AtEndOfStream
 strLine = objExecObject.StdOut.ReadLine()
 strIP = Instr(strLine,"Address")
 If strIP <> 0 Then
 Wscript.Echo strLine
 End If
Loop


from

http://www.microsoft.com/resources/documentation/windows/2000/server/scriptguide/en-us/sas_wsh_pkoy.mspx
Or

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("%comspec% /c ipconfig.exe")
debug.print objExecObject.StdOut.ReadAll()
Avatar of webzila

ASKER

EDDYKT: The code that you posted would be perfect if I were using VBScript but it doesnt seem to work with VB 6 - The code that I remember using was similar to what you posted and short like that...

So if anyone else knows what I am talking about please post that code as this is the prefered answer that I am looking for..

Mikal613: The first link that you posted does what I want. I will accept your answer if nobody else is able to come up with what I am truly looking for. The code in that example is very complicated, I dont remember using anything remotely close to that in my program. Like I said earlier my code was only a few lines.

Thanks anyways
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of webzila

ASKER

Cool EDDYKT that works, but is there a way to keep the MS-DOS window from showing up at all (even though it closes automatically later on)?

Thanks
Avatar of webzila

ASKER

Going to try one last time before closing this question :)
So right now, the method that EDDYKT posted works - I am able to run MS-DOS commands and load the result into a string without writing anything to a text file...But this method launches an MS-DOS window (then closes it when is done). Does anyone know a way to perform what I want without hving the MS-DOS window opening up?