Solved

Running DOS commands (with arguments) from VB6

Posted on 2001-09-17
20
321 Views
Last Modified: 2006-11-17
I'm building a GUI for some useful DOS commands I often use. They may look like this:

saxon file1.xml file2.xsl > file3.html

The problem is that VB's Shell() function won't eat the arguments, only program name. Is there any way to solve this? I don't need any return information from the DOS program.
0
Comment
Question by:liljegren
  • 8
  • 8
  • 2
  • +2
20 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 200 total points
ID: 6487649
I don't know of a saxon command,but have you tried something like this?
Dim A
A = Shell("c:\command.com /c netstat -an > C:\file.txt", vbMinimizedNoFocus)
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6487651
Capturing the Output of a MS-DOS Program
http://www.vb2themax.com/HtmlDoc.asp?Table=Articles&ID=40
0
 

Author Comment

by:liljegren
ID: 6487661
Oh, sorry all for the copies. Just wanted to refresh this page... I'll delete the others.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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 5

Expert Comment

by:kcm76
ID: 6487668
i have executed a batch file in a specified path from
shell command. what is the exact DOS command(s) u want to execute from VB?
can u give sample commands u often use.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6487670
If you use the link at the top of the page to reload the question you avoid the resubmission of data that results in duplicates.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6487675
The article I listed above describes how to reliably use redirection with DOS commands from VB.
0
 

Author Comment

by:liljegren
ID: 6487678
Thanks Paul. Time to look at the real problem. :-)
0
 
LVL 15

Expert Comment

by:lyonst
ID: 6487688
Can you do it something like this

Example Program
This program shows how you can use the Shell function and the GetNumTasks function to execute an MS-DOS program. This program assumes that you have the PKUNZIP program stored in the UTILS directory and that you have previously created a destination directory called DESTDIR on your hard drive.

Create a new project in Visual Basic. Form1 is created by default.
Add the following statements to the General Declarations section of Form1:
Dim ActiveApps As Integer
Private Declare Function GetNumTasks Lib "Kernel" () As Integer

Add a Command Button control to Form1. Command1 is created by default.
Add the following code to the Click event for Command1:
Private Sub Command1_Click()
    Dim AppDir As String
    Dim Zip As String
    Dim Y As Integer
    Dim X As Integer
   
    AppDir = "c:\destdir"
    ActiveApps = GetNumTasks()
   
    Zip = "c:\utils\pkunzip " & "c:\destdir\" & "test.zip" & " " & AppDir
    X = Shell(Zip, 2)
    SendKeys "%{enter}EXIT%{ }n"
   
    Do While GetNumTasks() <> ActiveApps
        Y = DoEvents()
    Loop
    MsgBox "Pkunzip is finished", 0, "Demo Program"
   
End Sub

http://msdn.microsoft.com/archive/default.asp?url=/ARCHIVE/en-us/dnarvbtips/html/msdn_msdn92.asp
0
 

Author Comment

by:liljegren
ID: 6487692
Kcm76, one real-life example is already shown. Some others include batch files with other commands inside.

The GUI is able to build command from input values, but that's another story. I only need to know how to execute them.
0
 

Author Comment

by:liljegren
ID: 6487700
Paul, thanks but I don't need to capture the output. The DOS commands I use only generate new text files, so I only have to check if the file is there, after a 5 sec delay or something.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6487722
You are definitely using redirection in the example you gave.  The article gives a shell replacement function that should work with any arguments passed to a dos program.
0
 

Author Comment

by:liljegren
ID: 6488035
Paul. Have read the article carefully, but it won't work. I think I have found all the code that was *not* included in the example, but VB says that "user-defined types may not be passed ByVal". This is about the SECURITY_ATTRIBUTES and other Types from the WIN32 API.

Here's the line that fails first:

hFile = CreateFile(sTempFile, GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0&, CREATE_ALWAYS, 0, ByVal 0&)

The user-defined type here is at "ByVal 0&".
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6488053
There is a link to the full source code for the article on the main article page:

http://www.vb2themax.com/Articles/StdOut/Code.zip
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6488064
The declaration for CreateFile will be slightly different in the source code here than the one you are using.
0
 

Author Comment

by:liljegren
ID: 6488198
Okay, I get no errors now, but nothing happens either. The command quoted earlier is supposed to take two files (a document + a stylesheet) and turn into a another file, but no such file appears.

When I run the program step by step, I notice that the call to CreateProcess fails, and so far I have no idea why.

I read about the CreateProcess function in other sources now.
0
 

Author Comment

by:liljegren
ID: 6488206
Lyonst, thanks for that link. I don't find any GetNumTasks function in the WIN32 API. Moreover, VB says there's no file called "Kernel", like in that declaration. I tried "Kernel32" too...
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6488236
Have you tried using shell preceding it with command.com?

Shell "command.com /c saxon file1.xml file2.xsl > file3.html", vbNormalFocus
0
 

Author Comment

by:liljegren
ID: 6488348
Funny, I was just going to report that I succeeded by using the solution vinnyd79 suggested, when Paul suggests the same, even closer to the target. I didn't dare to try it first since I didn't know the /c flag. Sorry and thanks for all effort spent on this matter Paul. Since the solution already was there I can only reward vinnyd79 for it. :-)
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6488360
No prob.  Happy you got it working. :)
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 6488408
The /c will close the dos window.Instead of using vbMinimizedNoFocus like I suggested or vbNormalFocus as Paul suggested,you might want to use vbhide to hide the window.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

830 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