URGENT! How to verify a process finished successfully using Shell or CreateProcess and close the resulting command window?

Posted on 2006-04-15
Last Modified: 2010-04-07
Hi all!

Here's my problem: I need to run a command line program from my VB6 application. In this case, it's sqlcmd.exe that I use to do some backup of a SQL Express 2005 database. I don't have any problem with the command line program working, I just have a problem with knowing if it succeeded or not and closing the resulting window.

The command line is:  
sqlcmd -SmyServer\SQLEXPRESS -UmyUser -PmyPWD -q"BACKUP DATABASE ARMS TO DISK = 'C:\Data\DB-04152006.dat'"

Here's my code that calls the process:

strcmd = "sqlcmd -SmyServer\SQLEXPRESS -UmyUser -PmyPWD -q"BACKUP DATABASE ARMS TO DISK = 'C:\Data\DB-04152006.dat'""

Shell strCmd, vbHide

This works great and the database gets backed up, but I can't find out how to verify that without physically checking it (after waiting long enough for the process to finish). I need to be able to give the user a prompt letting them know that it was backed up and where.

I've also tried this which also works just fine, but this leaves the DOS box open and I still don't know if it succeeded or not from within my VB6 application (just visually).

Call ExecCmd (strcmd)

Public Sub ExecCmd(stCmdline As String)

   Dim start As STARTUPINFO
   Dim ret As Long

   ' Initialize the STARTUPINFO structure:
   start.cb = Len(start)

   ' Start the shelled application:
   ret = CreateProcessA(0&, stCmdline, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

   ' Wait for the shelled application to finish:
   ret = WaitForSingleObject(proc.hProcess, INFINITE)
   ret = CloseHandle(proc.hProcess)
End Sub

What I need is a solution that runs the process and gives me some value that lets me know if it succeeded or not AND also closes the command window (DOS Box).

I've also tried ShellExecute, but can't get it to run the command at all.

Any help would be greatly appreciated!


Question by:weiroblpay
    LVL 29

    Expert Comment

    Try using the /C switch in your command line this should execute your string and then terminate the dos window automatically
    LVL 1

    Author Comment

    Thanks eql1044, but I found I can terminate the dos window OK, I just can't tell if it ran successfully or if it failed. I need my application to be able to tell if the command ran properly or if there were errors.
    LVL 29

    Expert Comment

    I am not familiar with the command line but you might be able to use createpipe to throw back the data to your application but this would only work if that command line actually tells you if it succeeded or failed. Does this happen?
    LVL 29

    Accepted Solution

    Here is the link

    How To Create a Process for Reading and Writing to a Pipe
    LVL 1

    Author Comment

    Thanks. The link and your comments provided me with the help I needed to get confirmation of what was going on. Even better, it works by showing the dos box, then going away and giving my app what I need to verify what happened.

    Thanks for your help.


    Featured Post

    IT, Stop Being Called Into Every Meeting

    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

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    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…
    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…

    728 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

    21 Experts available now in Live!

    Get 1:1 Help Now