• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

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

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!


  • 3
  • 2
1 Solution
Try using the /C switch in your command line this should execute your string and then terminate the dos window automatically
weiroblpayAuthor Commented:
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.
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?
Here is the link

How To Create a Process for Reading and Writing to a Pipe
weiroblpayAuthor Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now