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
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
991 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How does CurrentUser work? 10 38
Crystal reports - Formula Field code need assistance with code 17 81
TT Auto Dashboard 13 95
VB6 ListBox Question 4 48
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…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

792 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