Solved

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

Posted on 2004-03-24
9
986 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:webzila
  • 4
  • 3
  • 2
9 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 10670415
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
0
 
LVL 1

Author Comment

by:webzila
ID: 10670532
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.
0
 
LVL 48

Assisted Solution

by:Mikal613
Mikal613 earned 100 total points
ID: 10670698
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 10671216
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
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 26

Expert Comment

by:EDDYKT
ID: 10671231
Or

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("%comspec% /c ipconfig.exe")
debug.print objExecObject.StdOut.ReadAll()
0
 
LVL 1

Author Comment

by:webzila
ID: 10671940
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
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 400 total points
ID: 10672898
In VB6

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

0
 
LVL 1

Author Comment

by:webzila
ID: 10674514
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
0
 
LVL 1

Author Comment

by:webzila
ID: 10696046
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?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now