Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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,007 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…
Suggested Courses

715 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