Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
1,009 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 200 total points
ID: 10670698
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 800 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

926 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